按创建按钮回滚事务并获取值字段以将其插入 form_for
Rollback transaction on pressing create button and fetching the value field to insert it into the form_for
错误
Started POST "/products" for 127.0.0.1 at 2017-08-05 01:23:20 -0700
Processing by ProductsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"WcfuzGz2ZaEpFmagKYTm3feGTaZxNFPlTkLu/epw7fWObs+pdO4McXw9cLUNjTguav0i97rJR1sLhL5Fk+mk0g==", "product_attribute"=>{"name"=>"RAM 2355 Ghz", "size"=>"4GB", "description"=>"Its a very gooooooooood Ram"}, "value"=>"1", "commit"=>"Create"}
(0.1ms) begin transaction
(0.1ms) rollback transaction
Rendering products/new.html.erb within layouts/application
Rendered products/new.html.erb within layouts/application (2.2ms)
Completed 200 OK in 68ms (Views: 63.6ms | ActiveRecord: 0.1ms)
描述
每当我在填写表格后点击创建按钮时,我都会收到上述错误。
产品控制器
class ProductsController < ApplicationController
def new
@product = ProductAttribute.new
@value = params[:value]
end
def create
@product = ProductAttribute.new(product_params)
if @product.save
redirect_to statics_url
else
render 'new'
end
end
private
def product_params
params.require(:product_attribute).permit(:name,:value,:size,:description)
end
end
静态控制器
class StaticsController < ApplicationController
def index
@products = Product.all
end
def new
@product = Product.new
end
def show
@product = Product.find(params[:id])
@attributes = ProductAttribute.where(value: @product.value)
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to root_url
else
render 'new'
end
end
def edit
@product = Product.find(params[:id])
end
def update
@product = Product.find(params[:id])
if @product.update(product_params)
redirect_to root_url
else
render 'edit'
end
end
private
def product_params
params.require(:product).permit(:name,:value)
end
end
静态视图show.html.erb
<h1>Product listing now</h1>
<% @attributes.each do |attribute| %>
<li><%= attribute.name%></li>
<li><%= attribute.value%></li>
<li><%= attribute.size%></li>
<li><%= attribute.description%></li>
<% end %>
<%= link_to "Create New Product Attributes", new_product_path(value:
@product.value) %>
静态视图new.html.erb
<h1> New Product Creation </h1>
<%= form_for(@product, url: statics_path) do |f| %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :value %>
<%= f.text_field :value, class: 'form-control' %>
<%= f.submit "Create", class: "btn btn-primary" %>
<% end %>
产品浏览new.html.erb
<h1>Add the New Product Attribute</h1>
<%= form_for(@product, url: products_path) do |f|%>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= hidden_field_tag :value, @value %>
<%= f.label :size %>
<%= f.text_field :size, class: 'form-control' %>
<%= f.label :description %>
<%= f.text_field :description, class: 'form-control' %>
<%= f.submit "Create", class: 'btn btn-primary' %>
<% end %>
描述
我想做的是,我通过 new_static_path(value: @product.value) 传递 value 属性。我正在尝试使用现有值字段(这是主键)创建一个新的 Product 属性字段。
例如:Ram(parent field) -> (many child field with common value attributes)。我在产品视图 new.html.erb 中使用了 hidden_field_tag,这样它将派生自控制器。 (我对此感到困惑)。
我通过对堆栈溢出家伙的深入研究找到了答案,
<%= f.hidden_field :value, :value => params[:value] %>
我必须使用散列选项指定值。
谢谢你,祝你今天愉快。
您也可以尝试这样的操作:
def new
@product = Product.new(value: params[:value])
end
这将允许 Rails 将值参数放入 product_attribute 散列中。您还可以更改表单值名称以匹配预期值
<input ... name="[product][value]" ... >
我会选择第一个选项。
错误
Started POST "/products" for 127.0.0.1 at 2017-08-05 01:23:20 -0700
Processing by ProductsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"WcfuzGz2ZaEpFmagKYTm3feGTaZxNFPlTkLu/epw7fWObs+pdO4McXw9cLUNjTguav0i97rJR1sLhL5Fk+mk0g==", "product_attribute"=>{"name"=>"RAM 2355 Ghz", "size"=>"4GB", "description"=>"Its a very gooooooooood Ram"}, "value"=>"1", "commit"=>"Create"}
(0.1ms) begin transaction
(0.1ms) rollback transaction
Rendering products/new.html.erb within layouts/application
Rendered products/new.html.erb within layouts/application (2.2ms)
Completed 200 OK in 68ms (Views: 63.6ms | ActiveRecord: 0.1ms)
描述
每当我在填写表格后点击创建按钮时,我都会收到上述错误。
产品控制器
class ProductsController < ApplicationController
def new
@product = ProductAttribute.new
@value = params[:value]
end
def create
@product = ProductAttribute.new(product_params)
if @product.save
redirect_to statics_url
else
render 'new'
end
end
private
def product_params
params.require(:product_attribute).permit(:name,:value,:size,:description)
end
end
静态控制器
class StaticsController < ApplicationController
def index
@products = Product.all
end
def new
@product = Product.new
end
def show
@product = Product.find(params[:id])
@attributes = ProductAttribute.where(value: @product.value)
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to root_url
else
render 'new'
end
end
def edit
@product = Product.find(params[:id])
end
def update
@product = Product.find(params[:id])
if @product.update(product_params)
redirect_to root_url
else
render 'edit'
end
end
private
def product_params
params.require(:product).permit(:name,:value)
end
end
静态视图show.html.erb
<h1>Product listing now</h1>
<% @attributes.each do |attribute| %>
<li><%= attribute.name%></li>
<li><%= attribute.value%></li>
<li><%= attribute.size%></li>
<li><%= attribute.description%></li>
<% end %>
<%= link_to "Create New Product Attributes", new_product_path(value:
@product.value) %>
静态视图new.html.erb
<h1> New Product Creation </h1>
<%= form_for(@product, url: statics_path) do |f| %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :value %>
<%= f.text_field :value, class: 'form-control' %>
<%= f.submit "Create", class: "btn btn-primary" %>
<% end %>
产品浏览new.html.erb
<h1>Add the New Product Attribute</h1>
<%= form_for(@product, url: products_path) do |f|%>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= hidden_field_tag :value, @value %>
<%= f.label :size %>
<%= f.text_field :size, class: 'form-control' %>
<%= f.label :description %>
<%= f.text_field :description, class: 'form-control' %>
<%= f.submit "Create", class: 'btn btn-primary' %>
<% end %>
描述
我想做的是,我通过 new_static_path(value: @product.value) 传递 value 属性。我正在尝试使用现有值字段(这是主键)创建一个新的 Product 属性字段。 例如:Ram(parent field) -> (many child field with common value attributes)。我在产品视图 new.html.erb 中使用了 hidden_field_tag,这样它将派生自控制器。 (我对此感到困惑)。
我通过对堆栈溢出家伙的深入研究找到了答案,
<%= f.hidden_field :value, :value => params[:value] %>
我必须使用散列选项指定值。
谢谢你,祝你今天愉快。
您也可以尝试这样的操作:
def new
@product = Product.new(value: params[:value])
end
这将允许 Rails 将值参数放入 product_attribute 散列中。您还可以更改表单值名称以匹配预期值
<input ... name="[product][value]" ... >
我会选择第一个选项。