簡體   English   中英

如何在我指定控制器和操作的link_to調用中傳遞多個參數?

[英]How do I pass more than one parameter into a link_to call where I specify controller and action?

我正在使用link_to初始化一個控制器方法,該方法需要兩個參數才能執行我需要采取的步驟。 我似乎無法正確理解語法,而且我想知道是否是因為在使用這種特定語法時不能傳遞多個參數。 這是我到目前為止的內容:

<%= 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

鏈接無效,並且我從不輸入控制器操作associate_subgroup_with_org 有人可以幫我弄清楚語法嗎?

您可以按照以下方式進行路線:

get '/groups/associate_subgroup_with_org' => 'groups#associate_subgroup_with_org', :as => :associate_subgroup

您可以發送任何號碼。 link_to的參數集:

<%= link_to 'Select',
            {controller: 'groups',
             action: 'associate_subgroup_with_org',
             organization_id: organization.id,
             subgroup_id: @activity.group.id},
            class: 'button' %>

要么,

<%= link_to 'Select',associate_subgroup_path(organization_id: organization.id, subgroup_id: @activity.group.id),class: 'button' %>

您需要在路線中指定它。 像這樣:

get "/groups/:id/subgroup/:state" => "groups#subgroup", :as => :subgroup

並編寫如下鏈接:

subgroup_path(@organization, @subgroup)

無論您使用什么符號。

不建議在link_to / form_url中使用控制器和動作。 我猜你有一個組資源,我的意思是在route.rb中像resources :groups 如果是這樣,則在其中添加一個收集方法,例如:

resources :groups do
  #....
  post :associate_subgroup_with_org
end

現在您可以使用associate_subgroup_with_org_groups_path(p1: v1, p2: v2, .....)

或者,您可以將一個命名的路由定義為:

post 'groups/associate_subgroup_with_org', as: :associate_subgroup_with_org

現在您可以使用associate_subgroup_with_org_path(p1: v1, p2: v2, .....)

希望清楚

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM