简体   繁体   中英

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 } ) %>

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 (eg, 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

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

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

如果仍然无法正常工作,请检查路径:

$ rake routes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM