Rails - passing parameters in link_to

Ruby on-Rails

Ruby on-Rails Problem Overview


My Accounts index page lists all accounts, and per account has a link to "+ Service"; this should direct the user to the /my_services/new page and have pre-populated the account_id field with the appropriate ID, depending on which link was clicked on the Accounts index page.

I have debug(params) at the bottom of every page, and with what I'm trying, I'm not getting anything other than :controller and :action showing up in my parameters for the /my_services/new page.

The link I've been trying is this:

link_to "+ Service", "my_services/new", :account_id => acct.id

I then also have logic in the services controller:

def new
  @my_service = MyService.new
  if params[:account_id]
    @my_service.account_id = params[:account_id]
  end
end

Can someone help with the proper way of doing this? I haven't yet been able to get it going with with a few nasty little hacky things I tried.

EDIT

Turns out, if someone is looking at this answer in future, nested resources (possibly with the shallow: true option in routes.rb) seem to be the way to go. My routes.rb for this part now looks like this:

resources :accounts, shallow: true do
  resources :services
end

My links now look like this:

<%= link_to "+ Service", new_service_path(:service => { :account_id => @account.id } ) %>

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

First of all, link_to is a html tag helper, its second argument is the url, followed by html_options. What you would like is to pass account_id as a url parameter to the path. If you have set up named routes correctly in routes.rb, you can use path helpers.

link_to "+ Service", new_my_service_path(:account_id => acct.id)

I think the best practice is to pass model values as a param nested within :

link_to "+ Service", new_my_service_path(:my_service => { :account_id => acct.id })

# my_services_controller.rb
def new
  @my_service = MyService.new(params[:my_service])
end

And you need to control that account_id is allowed for 'mass assignment'. In rails 3 you can use powerful controls to filter valid params within the controller where it belongs. I highly recommend.

http://apidock.com/rails/ActiveModel/MassAssignmentSecurity/ClassMethods

Also note that if account_id is not freely set by the user (e.g., a user can only submit a service for the own single account_id, then it is better practice not to send it via the request, but set it within the controller by adding something like:

@my_service.account_id = current_user.account_id 

You can surely combine the two if you only allow users to create service on their own account, but allow admin to create anyone's by using roles in attr_accessible.

hope this helps

Solution 2 - Ruby on-Rails

Try this

link_to "+ Service", my_services_new_path(:account_id => acct.id)

it will pass the account_id as you want.

For more details on link_to use this http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

Solution 3 - Ruby on-Rails

link_to "+ Service", controller_action_path(:account_id => acct.id)

If it is still not working check the path:

$ rake routes

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbdxView Question on Stackoverflow
Solution 1 - Ruby on-RailsViktor TrónView Answer on Stackoverflow
Solution 2 - Ruby on-RailsabhasView Answer on Stackoverflow
Solution 3 - Ruby on-RailsVishView Answer on Stackoverflow