How do I determine the link_to arguments for show, edit, and destroy? I know "new" is "new_user_post_path". Nothing else seems to follow this convention though?
My relevant routes.rb snippet:
resources :users do
resources :posts
end
Running
rake routes
from your rails root should tell you what you need to know in terms of the name of the routes and the parameters expected.
Related
I am working on a basic rails app and wondering how do I show /users/:id/posts/:id as /posts/:id. Right now my routes is
resources :users do
resources :post
end
Which i need to have so that every post gets associated by the user. But also at the same time for ease of use, i need to be able to show the post at /posts/:id
Any help of suggestion is appreciated.
Thanks
You can declare a second route to the same controller:
resources :users do
resources :posts
end
resources :posts
You just need to handle not having a user_id parameter in your posts controller gracefully.
you probably looking for shallow option in Rails
resources :users, :shallow => true do
resources :post
end
http://rails-bestpractices.com/posts/11-needless-deep-nesting
I have a User model which has_one :blog. In my routes.rb file I have resource :blog. Why doesn't it create a path for blog#show? Do I need to create one myself? Otherwise how do I link_to blog#show?
It does create a path for blog#show - see the output of your routes by running rake routes. The route missing is blog#index, because there is only one to show.
See http://guides.rubyonrails.org/routing.html#singular-resources for more information on singular routes.
In my application, a User has many Posts, and a Post has many Comments. How should I configure the routing? I have configured it like this:
resources :users do
resources :post do
resources :comments
end
end
Some articles say this isn't recommended because it will be confusing.
Yes, It is not recommened. But Allowed in rails
You should use something like this
resources :users do
resources :posts
end
resources : posts do
resources :comments
end
As Deep Nesting Will create problems like, long path names in rails, and long urls in urls.
This question stems from Hartl's Rails Tutorial (progressed in chapter 9) - sorry if it seems particularly noobish...
Currently, I understand that in the routes.rb file, when a page was defined using, for example:
match '/help', to: 'static_pages#help'
a link generated in an embedded ruby:
<li><%= link_to "Help", help_path %></li>
would function. The help_path, and specifically the word "path", would correspond to the "#help" defined in routes.
However, the routes file did not define links for items such as users_path (used to show all users), or edit_user_path(current user) (used to edit settings of the current user) - I was confused as to where they were defined, and how they are able to correctly function and link me the desired places.
Thanks!
The routes file does, in fact, get an entry determining the behavior of users (shown in listing 2.2). It gets created through the scaffolding described here.
resources :users
By convention a "resource" entry in your routes.rb file sets up a number of paths that will be associated with users. Refer to the Rails Guide for a good overview.
I am running into an issue when I delete any resource. Before I delete something, current_user returns the correct User; however, after I delete something, current_user returns nill. I tried this with multiple resources and it does the same thing. Has anyone ever encountered a similar problem?
EDIT: I also created a UsersController to allow me to manage the users. My routes file contains:
resources :users
devise_for :users
Found solution: my layout did not have <%= csrf_meta_tag %>. Found answer in this question