Jump to content

The ultimate community for Ruby on Rails developers.


Adding a link_to to a new action added to my controller, need help.

  • Please log in to reply
1 reply to this topic

#1 jschrock1



  • Members
  • 48 posts
  • LocationIndianapolis

Posted 17 December 2013 - 08:12 PM

for some reason, I've been dreading the whole "routes" thing after going over the tutorial.  Have developed a hand full of web pages with some success, but sticking with the actions that rails provide...  Now I need to branch out and invoke new actions.  I know I need to spend time reading all about routes, but I did want one new action to work ....


I have a typical user mode/controller/views and all I have in my routes file pertaining to users is


resources :users


Now I want to add a link on one of the users page that allows him to perform a search on records of another model.  I tried the following link_to with no avail.

<%= link_to "Show all tasks", :controller => "users", :action => "search_all"  %>

My intent is to provide a link button, and when clicked have my search_all action in my user controller to be invoked.  I know I need to update my routes.rb....


Can someone provide me with the changes I need to make? 

#2 Ohm



  • Moderators
  • 529 posts
  • LocationCopenhagen

Posted 17 December 2013 - 10:48 PM

It really depends. Is the search_all action an action defined on a collection of users or on a single user?


That is, does the action expect to have access to a user via the params?


If it's for a single user, you need to define a member scope on the resource and define the search_all action. Else you'll need to define a collection scope for the search_all action.

resources :users do
  member do
    get :search_all # Will give you a route like /users/:id/search_all


resources :users do
  collection do
    get :search_all # Will give you a route like /users/search_all

Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users