I am using a link_to to initiate a controller method that requires two parameters in order to perform the steps I need it to take. I can't seem to get the syntax right, and I'm wondering if it is because you can't pass more than one parameter in using this particular syntax. Here is what I have so far:
<%= link_to 'Select',
{controller: 'groups',
action: 'associate_subgroup_with_org',
organization_id: organization.id,
subgroup_id: @activity.group.id},
class: 'button' %>
def associate_subgroup_with_org
@organization = Group.find(params[:organization_id])
@subgroup = Group.find(params[:subgroup_id])
@subgroup.parent_group_id = @organization.id
respond_to do |format|
format.js
end
end
The link is not working and I never enter my controller action associate_subgroup_with_org
. Can someone help me get the syntax right?
You can make a route like this:
get '/groups/associate_subgroup_with_org' => 'groups#associate_subgroup_with_org', :as => :associate_subgroup
And you can send any no. of parameters with link_to:
<%= link_to 'Select',
{controller: 'groups',
action: 'associate_subgroup_with_org',
organization_id: organization.id,
subgroup_id: @activity.group.id},
class: 'button' %>
Or,
<%= link_to 'Select',associate_subgroup_path(organization_id: organization.id, subgroup_id: @activity.group.id),class: 'button' %>
You need to specify it in your routes. Something like this:
get "/groups/:id/subgroup/:state" => "groups#subgroup", :as => :subgroup
And write the link like:
subgroup_path(@organization, @subgroup)
With whatever symbols you're using.
Using controller and actions in link_to/form_url is not recommended. I guess you have a groups resources, I mean in routes.rb something like resources :groups
. if so then add a collection method there like:
resources :groups do
#....
post :associate_subgroup_with_org
end
Now you can use associate_subgroup_with_org_groups_path(p1: v1, p2: v2, .....)
Or you can define one named route as:
post 'groups/associate_subgroup_with_org', as: :associate_subgroup_with_org
Now you can use associate_subgroup_with_org_path(p1: v1, p2: v2, .....)
Hope its clear
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.