Damir Nurgaliev Damir Nurgaliev - 24 days ago 10
Ruby Question

Single routing in rails

I create a projects with Users and Posts. I have a posts on user profile. To declare @posts i created UserPosts controller and put this

def index
@posts = current_user.posts
end


Now i need to write routs to this controller. I did:

get 'users/profile', as: 'user_root', controllers: { profile: "userposts" }


And got nilNil error. How should I correctly do this according to CRUD?

Answer

You do not say that the method shall take the action "index".

Try it like this:

 get "user_posts/index" => "user_posts#index", :as => 'user_root'

The first string is the link you will have to insert to reach this line, the second is the controller and the action, seperated by #. Controller is here 'UserPosts', action 'index'.

Or if you want to pass your user_profile as a param and have another link:

get "user_root/:profile" => "user_posts#index"

There is also a good guide on routing for rails: http://guides.rubyonrails.org/routing.html

Comments