在 rails 表单助手中访问模型 ID
Accessing Model ID inside of rails form helper
首先,我是 rails 的新手,我有一个连接 table Menus_orders,我试图在创建时将 ID 加载到其中。
我有这个表单助手,它可以访问 Order.new(:id, :name) 字段和 fields_for Menus_order(:menu_id, :order_id).我可以从 select 中插入 menu_id,但我终其一生都无法弄清楚如何使用表单在 @order 中保存的当前 order_id 来填充隐藏输入。
看ORDER_ID这里
<div class="form">
<h1>Create a new order</h1>
<%= form_for @order do |f| %>
<%= f.label :name%><br>
<%= f.text_field :name%><br>
<h3>Select meals</h3>
<%= fields_for(@menus_order) do |i| %>
<div class="field">
<%= i.select :menu_id, options_for_select(Menu.all.pluck(:id)) %>
<%= i.hidden_field :order_id, :value => ORDER_ID HERE %>
</div>
<% end %>
<br>
<%= f.submit "Create"%><br>
<% end %>
</div>
谢谢,DW
You should use nested attributes for order model just apply
the conventions right & it will be handled, you can achieve this by
following example :
order.rb 应该是这样的:
class Order < ActiveRecord::Base
has_many :menu_orders
accepts_nested_attributes_for :menu_orders
end
menu_order.rb 就像:
class MenuOrder < ActiveRecord::Base
belongs_to :order
end
orders/new.html
<div class="form">
<h1>Create a new order</h1>
<%= form_for @order do |f| %>
<%= f.label :name%><br>
<%= f.text_field :name%><br>
<h3>Select meals</h3>
<%= f.fields_for :menu_orders do |i| %>
<div class="field">
<%= i.select :menu_id, options_for_select(Menu.all.pluck(:id)) %>
</div>
<% end %>
<br>
<%= f.submit "Create"%><br>
<% end %>
</div>
OrdersController
class OrdersController < ApplicationController
def new
@order = Order.new
@order.menu_orders.build
end
def create
@order = Order.new(order_params)
if @order.save
redirect_to orders_path
flash[:success] = "Order created"
else
render 'new'
end
end
private
def order_params
params.require(:order).permit(:name, menu_orders_attributes: [ :menu_id, :order_id ])
end
end
首先,我是 rails 的新手,我有一个连接 table Menus_orders,我试图在创建时将 ID 加载到其中。
我有这个表单助手,它可以访问 Order.new(:id, :name) 字段和 fields_for Menus_order(:menu_id, :order_id).我可以从 select 中插入 menu_id,但我终其一生都无法弄清楚如何使用表单在 @order 中保存的当前 order_id 来填充隐藏输入。
看ORDER_ID这里
<div class="form">
<h1>Create a new order</h1>
<%= form_for @order do |f| %>
<%= f.label :name%><br>
<%= f.text_field :name%><br>
<h3>Select meals</h3>
<%= fields_for(@menus_order) do |i| %>
<div class="field">
<%= i.select :menu_id, options_for_select(Menu.all.pluck(:id)) %>
<%= i.hidden_field :order_id, :value => ORDER_ID HERE %>
</div>
<% end %>
<br>
<%= f.submit "Create"%><br>
<% end %>
</div>
谢谢,DW
You should use nested attributes for order model just apply the conventions right & it will be handled, you can achieve this by following example :
order.rb 应该是这样的:
class Order < ActiveRecord::Base
has_many :menu_orders
accepts_nested_attributes_for :menu_orders
end
menu_order.rb 就像:
class MenuOrder < ActiveRecord::Base
belongs_to :order
end
orders/new.html
<div class="form">
<h1>Create a new order</h1>
<%= form_for @order do |f| %>
<%= f.label :name%><br>
<%= f.text_field :name%><br>
<h3>Select meals</h3>
<%= f.fields_for :menu_orders do |i| %>
<div class="field">
<%= i.select :menu_id, options_for_select(Menu.all.pluck(:id)) %>
</div>
<% end %>
<br>
<%= f.submit "Create"%><br>
<% end %>
</div>
OrdersController
class OrdersController < ApplicationController
def new
@order = Order.new
@order.menu_orders.build
end
def create
@order = Order.new(order_params)
if @order.save
redirect_to orders_path
flash[:success] = "Order created"
else
render 'new'
end
end
private
def order_params
params.require(:order).permit(:name, menu_orders_attributes: [ :menu_id, :order_id ])
end
end