Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - passing parameters in link_to

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 } ) %> 
like image 811
bdx Avatar asked May 27 '12 11:05

bdx


1 Answers

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

like image 122
Viktor Trón Avatar answered Oct 09 '22 22:10

Viktor Trón