[英]How do I pass more than one parameter into a link_to call where I specify controller and action?
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. 我正在使用link_to初始化一个控制器方法,该方法需要两个参数才能执行我需要采取的步骤。 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
. 链接无效,并且我从不输入控制器操作
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的参数集:
<%= 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. 不建议在link_to / form_url中使用控制器和动作。 I guess you have a groups resources, I mean in routes.rb something like
resources :groups
. 我猜你有一个组资源,我的意思是在route.rb中像
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, .....)
现在您可以使用
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, .....)
现在您可以使用
associate_subgroup_with_org_path(p1: v1, p2: v2, .....)
Hope its clear 希望清楚
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.