簡體   English   中英

如何通過導軌將對象從一個控制器傳遞到另一個控制器?

[英]How do I pass objects from one controller to another in rails?

如何將從一個控制器的show動作構建並提交的對象傳遞給另一個控制器的create動作,同時又保留前者的實例變量?

上述ItemsController:

def show
 @item = Item.friendly.find(params[:id])
 @trade = current_user.requested_trades.build
end

然后,我的show頁面上的form_for使用:wanted_item:trade_requester作為參數,對@trade進行發布請求。

TradesController:

def create
 @item = ???
 @trade = current_user.requested_trades.build
 if @trade.save
  format.html { redirect_to @item, notice: "success" }
 else
  format.html { redirect_to @item, notice "pick another number" }
 end
 etc...
end

Trade.rb:

belongs_to :trade_requester, class_name: "User"
belongs_to :trade_recipient, class_name: "User"
belongs_to :wanted_item, class_name: "Item"
belongs_to :collateral_item, class_name: "Item"

的routes.rb

resources :trades do
 member do
  post :accept
  post :reject
 end
end

這有什么不對勁嗎? 關於這個主題的其他問題似乎與在同一控制器內的不同動作之間傳遞對象有關-我要問的不是那樣。

首先,我想我的路線應該更像:

resources :wanted_items do 
  resources :trades, shallow: true do 
    member do 
      post :accept
      post :reject
    end
  end
end

這會給你:

         accept_trade POST   /trades/:id/accept(.:format)                          trades#accept
         reject_trade POST   /trades/:id/reject(.:format)                          trades#reject
   wanted_item_trades GET    /wanted_items/:wanted_item_id/trades(.:format)        trades#index
                      POST   /wanted_items/:wanted_item_id/trades(.:format)        trades#create
new_wanted_item_trade GET    /wanted_items/:wanted_item_id/trades/new(.:format)    trades#new
           edit_trade GET    /trades/:id/edit(.:format)                            trades#edit
                trade GET    /trades/:id(.:format)                                 trades#show
                      PATCH  /trades/:id(.:format)                                 trades#update
                      PUT    /trades/:id(.:format)                                 trades#update
                      DELETE /trades/:id(.:format)                                 trades#destroy
         wanted_items GET    /wanted_items(.:format)                               wanted_items#index
                      POST   /wanted_items(.:format)                               wanted_items#create
      new_wanted_item GET    /wanted_items/new(.:format)                           wanted_items#new
     edit_wanted_item GET    /wanted_items/:id/edit(.:format)                      wanted_items#edit
          wanted_item GET    /wanted_items/:id(.:format)                           wanted_items#show
                      PATCH  /wanted_items/:id(.:format)                           wanted_items#update
                      PUT    /wanted_items/:id(.:format)                           wanted_items#update
                      DELETE /wanted_items/:id(.:format)                           wanted_items#destroy

然后在您的form_for ,我將執行以下操作:

<% form_for wanted_item_trades_path(wanted_item: @wanted_item, trade: @trade) do |f| %>
  ...
<% end %>

form_for語法可能並不完全正確,因此您可能需要對其付諸實踐。

這將生成類似以下內容的網址:

/wanted_items/3/trades

自然地,“ 3”就構成了。 無論您的@item.id是什么。

發布表單時,您應在params 3一個wanted_item_id 然后,在您的TradesController中,您將執行以下操作:

class TradesController < ApplicationController

  def create
    @wanted_item = Item.find_by(id: params[:wanted_item_id])
    @trade = current_user.requested_trades.build(wanted_item: @wanted_item)
    if @trade.save
      format.html { redirect_to @item, notice: "success" }
    else
      format.html { redirect_to @item, notice "pick another number" }
    end
  end        

  ...

end

順便說一句,看來您正在使用friendly_id。 因此,您可以對上述所有內容進行調整,以使用friendly_id而不是id 我不使用friendly_id,因此您必須自己進行排序。

暫無
暫無

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

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