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
<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) %>
<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 %>
<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)。我正在尝试使用现有值字段(这是主键)创建新的Product属性字段。 例如:Ram(父字段) - &gt; (许多具有共同值属性的子字段)。我在产品的视图new.html.erb中使用hidden_field_tag,以便它将从控制器派生。 (我对此感到困惑)。
答案 0 :(得分:0)
I found out the answer by doing thorough research on stack overflow guys,
<%= f.hidden_field :value, :value => params[:value] %>
i had to specify the value using hash for options.
Thank you have a nice day.
答案 1 :(得分:0)
你也可以尝试这样的事情:
def new
@product = Product.new(value: params[:value])
end
这将允许Rails将值param放在product_attribute哈希中。您还可以更改表单值名称以匹配预期值
<input ... name="[product][value]" ... >
我会选择第一个选项,即。