从自定义控制器向模型插入数据 Rails
Inserting data to a model from a custom controller Rails
我为名为 "Charge" 的模型创建了脚手架,但我想从我创建的自定义控制器向模型插入数据 "Checkout"。
我有以下代码:
#in the routes.rb
get "checkout/pay", to: 'checkout#index'
#in the app/controllers/checkout_controller.rb
def index
@charge = Charge.new
end
#in the app/views/checkout/index.html.erb
<%= form_for(@charge, url: charges_path, method: :post ) do |f| %>
<% if @charge.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@charge.errors.count, "error") %> prohibited this charge from being saved:</h2>
<ul>
<% @charge.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="title">
<h1>shipment</h1>
</div>
<div class="large-12 columns">
<%= f.text_field :name, placeholder: "name", maxlength: "20", class: "radius" %>
</div>
<div class="large-12 columns">
<%= f.text_field :address, placeholder: "address", class: "radius" %>
</div>
<div class="large-12 columns">
<%= f.text_field :email, placeholder: "email", class: "radius" %>
</div>
<hr>
<%= f.submit "buy", class: "button green radius small expand" %>
<% end %>
#in the app/controllers/charges_controller.rb
def create
@charge = Charge.new(id: "523dd8f6aef8784386000001", amount: 60000, livemode: false, created_at: 1379784950, status: "paid", currency: "MXN", description: "Pizza", reference_id: "9839-wolf_pack", failure_code: "none", failure_message: "none", name: params[:name], address: params[:address], email: params[:email], ship_number: "5678sdf7sd5f6")
@charge.save
respond_with(@charge)
end
#in the app/models/charge.rb
validates :name, :address, :email, presence: true
为了测试,我在创建方法中用默认值填充了一些数据。
在表格 ('/checkout/pay') 中,我手动填写了 :name, :address, :email 数据,但将我重定向到 new_charge_path 并告诉我:
名字不能为空
地址不能为空
邮箱不能为空
尝试从 html 视图中的表单获取参数时出现错误?
您正在使用 form_for
视图助手,根据 rails 约定,控制器获得一个嵌套哈希 params[:charg]
,其中包含在表单中设置的收费属性。
所以,在你的控制器上你必须调用:
@charge = Charge.new(name: params[:charge][:name], address: params[:charge][:address], email: params[:charge][:email])
我为名为 "Charge" 的模型创建了脚手架,但我想从我创建的自定义控制器向模型插入数据 "Checkout"。
我有以下代码:
#in the routes.rb
get "checkout/pay", to: 'checkout#index'
#in the app/controllers/checkout_controller.rb
def index
@charge = Charge.new
end
#in the app/views/checkout/index.html.erb
<%= form_for(@charge, url: charges_path, method: :post ) do |f| %>
<% if @charge.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@charge.errors.count, "error") %> prohibited this charge from being saved:</h2>
<ul>
<% @charge.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="title">
<h1>shipment</h1>
</div>
<div class="large-12 columns">
<%= f.text_field :name, placeholder: "name", maxlength: "20", class: "radius" %>
</div>
<div class="large-12 columns">
<%= f.text_field :address, placeholder: "address", class: "radius" %>
</div>
<div class="large-12 columns">
<%= f.text_field :email, placeholder: "email", class: "radius" %>
</div>
<hr>
<%= f.submit "buy", class: "button green radius small expand" %>
<% end %>
#in the app/controllers/charges_controller.rb
def create
@charge = Charge.new(id: "523dd8f6aef8784386000001", amount: 60000, livemode: false, created_at: 1379784950, status: "paid", currency: "MXN", description: "Pizza", reference_id: "9839-wolf_pack", failure_code: "none", failure_message: "none", name: params[:name], address: params[:address], email: params[:email], ship_number: "5678sdf7sd5f6")
@charge.save
respond_with(@charge)
end
#in the app/models/charge.rb
validates :name, :address, :email, presence: true
为了测试,我在创建方法中用默认值填充了一些数据。 在表格 ('/checkout/pay') 中,我手动填写了 :name, :address, :email 数据,但将我重定向到 new_charge_path 并告诉我: 名字不能为空 地址不能为空 邮箱不能为空
尝试从 html 视图中的表单获取参数时出现错误?
您正在使用 form_for
视图助手,根据 rails 约定,控制器获得一个嵌套哈希 params[:charg]
,其中包含在表单中设置的收费属性。
所以,在你的控制器上你必须调用:
@charge = Charge.new(name: params[:charge][:name], address: params[:charge][:address], email: params[:charge][:email])