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.
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.
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
I got the following deprecation warning on the rails console:
DEPRECATION WARNING: Having additional attributes on the join table of a
has_and_belongs_to_many association is deprecated and will be removed in Rails 3.1.
Please use a has_many :through association instead.
The issue lies with the roles_users table that I created following an online step-by-step tutorial.
How do I implement a has_many :through association for acl9? It's beyond me, especially since the user and role models each only use helper methods and no actual has_and_belongs_to_many.
This is how they look like:
class User < ActiveRecord::Base
acts_as_authentic
acts_as_authorization_subject :association_name => :roles
end
class Role < ActiveRecord::Base
acts_as_authorization_role
end
The answer was later discussed in the comments to this GitHub issue.
User model:
acts_as_authorization_subject :association_name => :roles, :join_table_name => :roles_users
Role model:
acts_as_authorization_role :join_table_name => :roles_users
Also, for the record, Rails decided not to deprecate the :join_table option for habtm after all, so this went away with a subsequent patch release of Rails - ie. you shouldn't need the options mentioned in the issue if you just upgrade your Rails.
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.