我正在开发购物车应用并拥有订单模型。我向我的订单模型添加了送货详情,以包含carrier
,tracking_number
和ship_date
。我创建了一个名为updateshipping
的方法,以便在更新订单后进行不同的重定向。
我是rails的新手,所以不确定是否可以在保存后在rails中进行条件重定向,或者我是否应该创建自定义路径来访问updateshipping
方法(我尝试过的方法),或者如果正确答案是我应该创建一个新模型来保存运输细节。
我尝试通过方法updateshipping
将表单部分重定向到更新,但提交按钮不起作用。当我将鼠标悬停在提交按钮上时,网址不会显示。当我点击提交时,似乎重新加载表单部分。如果我在@order
form_for
之后删除了所有内容,它会正确转到更新顺序/更新方法,因此我假设我的问题出在form_for
行(或者是订单的一部分)模型首先。)
<%= form_for(@order, :url => shipment_path, :html => { :method => "put" }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :carrier %>
<%= f.text_field :carrier %>
<%= f.label :tracking_number, "Tracking #" %>
<%= f.text_field :tracking_number %>
<div class="form-actions">
<%= f.submit "Save changes", class: "btn btn-warning" %>
</div>
<% end %>
订单控制器
def updateshipping
raise params.inspect (code to update the order will come later)
end
Rake Routes
shipment /shipmentdetails/:id(.:format) orders#shipment_details
shipment PUT /shipmentdetails/:id(.:format) orders#updateshipping
的routes.rb
match '/shipmentdetails/:id' => 'orders#shipment_details', :as => :shipment
match '/shipmentdetails/:id' => 'orders#updateshipping', :as => :shipment, via: :put
订单模型
class Order < ActiveRecord::Base
attr_accessible :name, :address, :carrier, :tracking_number, :ship_date
has_many :line_items, dependent: :destroy
has_one :user
has_one :shop
validates :name, :address, presence: true
end
答案 0 :(得分:1)
我不确定为什么会这样,但我改变了与表格部分相关联的路线。我改变了路线:
match '/shipmentdetails/:id' => 'orders#shipment_details', :as => :shipment
为:
match '/shipmentdetails/:id/edit' => 'orders#shipment_details', :as => :edit_shipment
也许它在shipment_path之间感到困惑,但我认为form_for的路径有“put”方法,应该知道它使用以下路由:
match '/shipmentdetails/:id' => 'orders#updateshipping', :as => :shipment, via: :put
至于我原来尝试的form_for
form_for(@order, :url => shipment_path, :html => { :method => "put" })
它似乎与推荐的
一样有用form_for(@order, :url => shipment_path(@order), :html => { :method => "put" })
但我不确定使用shipment_path(@order)而不仅仅是shipment_path更好的做法。
答案 1 :(得分:0)
而不是:html => { :method => "put" }
,请尝试:method => "put"
答案 2 :(得分:0)
将form_for行更改为:
form_for(@order, :url => shipment_path(@order), :html => { :method => "put" })