Rails无法将嵌套值保存到表中

时间:2016-04-12 02:55:55

标签: ruby-on-rails nested-forms strong-parameters

我正在尝试从嵌套表单中保存值

 <%= form_for(@project, :html => { :multipart => true }) do |f| %>

                <h4>
                    <label for = "projectName">Title(required)</label>

                    <%= f.text_field :projectName, :maxlength => 50 %>
                </h4>

                <h3>Attach Project Images </h3>
                <%= f.fields_for :projectImages do |pi| %>
                <h4>    <%= pi.label :name %>
                        <%= pi.text_field :name %>

                </h4>
                <h4>
                    <%= pi.label :attachment %>
                    <%= pi.file_field :attachment %>
                </h4>
                <%end%>
                <%= f.submit %>
                </h4>

    <% end -%>

在控制器中

    def new
 @allTags = Tag.all
 @allBenefits = Benefit.all
 @project = Project.new
end

def create
 # ProjectImage.build

 @project = Project.new(project_params)

 p params[:project][:projectImages][:name] #passes the correct value  

if @project.save
   @project.tags.build
   @project.benefits.build

   #@project.project_images.build
  redirect_to :action => 'index'
 else
  render :action => 'new'
 end
end
private 
 def project_params
    params.require(:project).permit(:projectName,
       project_images_attributes: [:name,:attachment])
  end

在项目模型中

 has_many :project_images
    accepts_nested_attributes_for :project_images

在ProjectImage中

mount_uploader:attachment,AttachmentUploader

belongs_to :project

在project_images表中

 #<ProjectImage id: 2, project_id: 11, name: nil, attachment: nil, created_at: "2016-04-12 02:48:23", updated_at: "2016-04-12 02:48:23">

PARAMS

{"utf8"=>"✓",
 "authenticity_token"=>"eUDPM3gCqAAUXGTfs8hBP/LGXKK5ebL8ZPhVM3YUr0xrQyOA4LSXTH/B1AE/0S96AQC8XHm0bqCouxbhgA+8Fw==",
 "project"=>{"projectName"=>"",
 "briefDesc"=>"",
 "whatSection"=>"",
 "challengers"=>"",
 "status"=>"",
 "valueProposal"=>"",
 "maturityLevel"=>"fullyFunctional",
 "projectImage"=>{"name"=>"ProjectImage",
 "attachment"=>#<ActionDispatch::Http::UploadedFile:0x0000000d276b88 @tempfile=#<Tempfile:C:/Users/STUART~1.MIN/AppData/Local/Temp/RackMultipart20160412-1976-prqk3i.jpg>,
 @original_filename="jet.jpg",
 @content_type="image/jpeg",
 @headers="Content-Disposition: form-data; name=\"project[projectImage][attachment]\"; filename=\"jet.jpg\"\r\nContent-Type: image/jpeg\r\n">}},
 "commit"=>"Create Project"}



Processing by ProjectsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"kAGww+dlZ0i0DyAaIwUufG2Ol7Qo8KAEXPAAtczZETiCAlxwf9NYBN+SkMSvHEA5nkh3Sug9fFiQs0NnOsICYw==", "project"=>{"projectName"=>"z", "briefDesc"=>"", "whatSection"=>"", "challengers"=>"", "status"=>"", "valueProposal"=>"", "maturityLevel"=>"fullyFunctional", "projectImages"=>{"name"=>"ProjectImage", "attachment"=>#<ActionDispatch::Http::UploadedFile:0x000000073e0880 @tempfile=#<Tempfile:C:/Users/STUART~1.MIN/AppData/Local/Temp/RackMultipart20160412-3412-nbp63h.jpg>, @original_filename="jet.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"project[projectImages][attachment]\"; filename=\"jet.jpg\"\r\nContent-Type: image/jpeg\r\n">}}, "commit"=>"Create Project"}
**Unpermitted parameter: projectImages**

它没有保存任何projectimage的属性。任何帮助都是值得赞赏的。

干杯

1 个答案:

答案 0 :(得分:0)

问题在于

project_images_attributes: [:name,:attachment]) and <%= f.fields_for projectImages do |pi| %>
  

&#34;特别是,如果一个人没有地址,它什么都不呈现。一个   常见模式是控制器构建一个或多个空   子项,以便向用户显示至少一组字段。该   下面的示例将导致呈现2组地址字段   在新的人员表格上。&#34;

http://guides.rubyonrails.org/form_helpers.html

在构建新方法之后,我设法缓解了上述问题。我还将我的模型重命名为图像,以缓解snake_casecamelCase混淆。

现在,

def new
    @project = Project.new
    @project.images.build
  end 

params.require(:project).permit(:projectName, images_attributes [:name,:attachment],

我根据许多评论解决了这个问题。谢谢大家