has_many 关系的嵌套属性
Nested attributes for has_many relationship
我的模特是
class History < ActiveRecord::Base
belongs_to :projects_lkp
has_many :pictures
accepts_nested_attributes_for :pictures
end
class Picture < ActiveRecord::Base
belongs_to :history
validates_presence_of :pics
end
history/new.html.erb
<%= form_for(:history, :url => {:action => 'create'}) do |d| %>
<%= render(:partial =>"form",:locals => {:d => d}) %>
<% end %>
history/_form.html.erb
<%= d.fields_for :pictures do |dd| %>
<div class="row form-group"></div>
<div><class='col-md-5 form-label'>Photo</div>
<%= dd.file_field :pic, multiple: true, :class => 'col-md-15' %>
</div>
<% end %>
history_controller
def create
@history = History.new(history_params)
@history.projects_lkp_id = params[:projects_lkps_id]
respond_to do |format|
if @history.save
format.html{ redirect_to histories_path(id: @history.id), notice:"History added" }
else
@history = History.where(item: @history.item).all
format.html { render 'index' }
end
end
end
def history_params
params.require(:history).permit(:history,:date,:pic)
end
但是当我尝试提交表单时,它说 unpermitted parameter :pictures
传递的参数是:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xE8d/mD1pXh/2AIKrZlRoL552iEBVZ7jkzLJB1kNZyE=", "history"=>{"history"=>" jhafjkaalkds", "date"=>"20/10/2014", "pictures"=>{"pic"=>[#<ActionDispatch::Http::UploadedFile:0x0000000143d1f8 @tempfile=#<Tempfile:/tmp/RackMultipart20150910-2753-1ml2hmf>, @original_filename="Firefox_wallpaper.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"history[pictures][pic][]\"; filename=\"Firefox_wallpaper.png\"\r\nContent-Type: image/png\r\n">]}}, "projects_lkps_id"=>"", "commit"=>"Submit"}
不允许的参数:图片
这里的:pic是使用回形针创建的附件字段gem
试试这个:
def history_params
params.require(:history).permit(:history, :date, pictures_attributes: [:pic])
end
我看到的另一个问题是您的表单。应该是:
<%= form_for(:history, :url => {:action => 'create'}, multipart: true) do |d| %>
<%= render(:partial =>"form",:locals => {:d => d}) %>
<%= d.fields_for :pictures do |dd| %>
<div class="row form-group"></div>
<div><class='col-md-5 form-label'>Photo</div>
<%= dd.file_field :pic, multiple: true, :class => 'col-md-15' %>
</div>
<% end %>
<% end %>
参数应采用以下形式:
"history" => { ... , "photos_attributes" => { "0" => { ... } } }
我找到了答案:)
历史控制器
def new
@histories = History.new
@histories.pictures.build
end
history/new
<%= form_for(@histories, :url => {:action => 'create'}, html: {multipart:true}) do |d| %>
<%= render(:partial =>"form",:locals => {:d => d}) %>
<% end %>
有效:)
我的模特是
class History < ActiveRecord::Base
belongs_to :projects_lkp
has_many :pictures
accepts_nested_attributes_for :pictures
end
class Picture < ActiveRecord::Base
belongs_to :history
validates_presence_of :pics
end
history/new.html.erb
<%= form_for(:history, :url => {:action => 'create'}) do |d| %>
<%= render(:partial =>"form",:locals => {:d => d}) %>
<% end %>
history/_form.html.erb
<%= d.fields_for :pictures do |dd| %>
<div class="row form-group"></div>
<div><class='col-md-5 form-label'>Photo</div>
<%= dd.file_field :pic, multiple: true, :class => 'col-md-15' %>
</div>
<% end %>
history_controller
def create
@history = History.new(history_params)
@history.projects_lkp_id = params[:projects_lkps_id]
respond_to do |format|
if @history.save
format.html{ redirect_to histories_path(id: @history.id), notice:"History added" }
else
@history = History.where(item: @history.item).all
format.html { render 'index' }
end
end
end
def history_params
params.require(:history).permit(:history,:date,:pic)
end
但是当我尝试提交表单时,它说 unpermitted parameter :pictures
传递的参数是:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xE8d/mD1pXh/2AIKrZlRoL552iEBVZ7jkzLJB1kNZyE=", "history"=>{"history"=>" jhafjkaalkds", "date"=>"20/10/2014", "pictures"=>{"pic"=>[#<ActionDispatch::Http::UploadedFile:0x0000000143d1f8 @tempfile=#<Tempfile:/tmp/RackMultipart20150910-2753-1ml2hmf>, @original_filename="Firefox_wallpaper.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"history[pictures][pic][]\"; filename=\"Firefox_wallpaper.png\"\r\nContent-Type: image/png\r\n">]}}, "projects_lkps_id"=>"", "commit"=>"Submit"}
不允许的参数:图片
这里的:pic是使用回形针创建的附件字段gem
试试这个:
def history_params
params.require(:history).permit(:history, :date, pictures_attributes: [:pic])
end
我看到的另一个问题是您的表单。应该是:
<%= form_for(:history, :url => {:action => 'create'}, multipart: true) do |d| %>
<%= render(:partial =>"form",:locals => {:d => d}) %>
<%= d.fields_for :pictures do |dd| %>
<div class="row form-group"></div>
<div><class='col-md-5 form-label'>Photo</div>
<%= dd.file_field :pic, multiple: true, :class => 'col-md-15' %>
</div>
<% end %>
<% end %>
参数应采用以下形式:
"history" => { ... , "photos_attributes" => { "0" => { ... } } }
我找到了答案:)
历史控制器
def new
@histories = History.new
@histories.pictures.build
end
history/new
<%= form_for(@histories, :url => {:action => 'create'}, html: {multipart:true}) do |d| %>
<%= render(:partial =>"form",:locals => {:d => d}) %>
<% end %>
有效:)