Rails 4允许使用嵌套属性的Params

时间:2014-07-10 20:04:01

标签: ruby-on-rails ruby

好吧,到处寻找这个。尝试了所有的解决方案。也许有人可以帮忙解决这个问题。

所以,我有一个WebRequest模型,它有很多网站。每个WebSite都属于WebRequest。我的问题是嵌套的形式。我已经允许了params(至少,根据我的文档),一切正常,直到我进入服务器日志。发布在下面

class WebRequest < ActiveRecord::Base
  has_many :web_sites
  accepts_nested_attributes_for :web_sites
end

这是WebSite模型

 class WebSite < ActiveRecord::Base
   belongs_to :web_request, dependent: :destroy
 end

_form.html.erb

<% 1.times do %>
<%= f.fields_for :web_site do |ff| %>
    <%= ff.input :url %>
    <%= ff.input :description %>
  <% end %>
<% end %>

WebRequests Controller

class WebRequestsController < ApplicationController

 def new
   @web_request = WebRequest.new
   # @web_request.web_sites.build
 end

 def index
   @web_requests = WebRequest.all
  end

 def create
    @web_request = WebRequest.new(web_request_params)

respond_to do |format|
  if @web_request.save
    RequestMailer.web_request_submit(@web_request).deliver
    format.html { render partial: 'success' }
    format.json { render action: 'show', status: :created, location: @web_request }
  else
    format.html { render action: 'new' }
    format.json { render json: @web_request.errors, status: :unprocessable_entity }
  end
 end
end

 def web_request_params
   params.require(:web_request).permit(:web_needs, :primary_goal, :secondary_goal,       :    :call_to_action, :hero_image, :image_count, :existing, :resources, :web_examples, :special_functions, :social_network, web_sites_attributes: [:id, :url, :description])
 end
end

这是服务器日志:

     Started POST "/web_requests" for 127.0.0.1 at 2014-07-10 15:56:12 -0400
     Processing by WebRequestsController#create as HTML
     Parameters: {"utf8"=>"✓",          "authenticity_token"=>"0iisNgGk/AhC4jRrp1cmKWVCBsCcSx5G2dueEI/+p2A=", "web_request"=>{"web_needs"=>"", "primary_goal"=>"", "secondary_goal"=>"", "call_to_action"=>"", "hero_image"=>"", "image_count"=>"", "existing"=>"", "web_site"=>{"url"=>"TROLL", "description"=>"TROLL"}, "resources"=>"", "special_functions"=>"", "social_network"=>""}, "commit"=>"Create Web request"}
     Unpermitted parameters: web_site

请注意表单字段是如何传递的,但是它们会被限制在将其传递给数据库。

谢谢!

更新::

以下是完整的表单路径:

  <%= simple_form_for(@web_request) do |f| %>
    <% if @web_request.errors.any? %>
      <div id="error_explanation">
     <h2><%= pluralize(@web_request.errors.count, "error") %>
    prohibited this Web Request from being saved:</h2>
     <ul>
        <% @web_request.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
          <% end %>
          </ul>
          </div>
        <% end %>
      <div class="container">
         <div class="field">
            <%= f.input :web_needs, label: 'What web needs do you have?' %>
    </div>
   <div class="container"><h5>Please list below 5 URL's and explanations of why you like them.</h5></div>
    <% 1.times do %>
     <%= f.fields_for :web_sites do |ff| %>
        <div class="field">
         <%= ff.input :url, label: "URL of example site" %>
        </div>
        <div class="field">
         <%= ff.input :description, label: "Description of example site" %>
        </div>
       <% end %>
      <% end %>
     <div class="field">
      <%= f.input :resources, label: 'Will you be providing any kind of resource on this page? e.g. chord chart download.' %></br>
      </div>
     </div>
     <div class="actions">
       <%= f.button :submit, :class => "button" %>
     </div>
     </div>
   <% end %>

更新::完整错误日志

   undefined method `url' for #<ActiveRecord::Associations::CollectionProxy []>

这条线在这里

   <% 1.times do %>
   <%= f.simple_fields_for :web_site, @web_request.web_sites do |ff| %>
     <div class="field">
      <%= ff.input :url, label: "URL of example site" %> <--ERROR HERE on ':url'
     </div>
     <div class="field">
      <%= ff.input :description, label: "Description of example site" %>
     </div>
  <% end %>

2 个答案:

答案 0 :(得分:2)

在新操作中添加到控制器:

def new
  @web_request = WebRequest.new
  @web_site = @web_request.web_sites.build
end

并形成:

<%= f.simple_fields_for @web_site do |ff| %>
    <%= ff.input :url %>
    <%= ff.input :description %>
  <% end %>
<% end %>

web_request_params

def web_request_params
   params.require(:web_request).permit(:web_needs,
                                       :primary_goal, 
                                       :secondary_goal, 
                                       :call_to_action,
                                       :hero_image,
                                       :image_count,
                                       :existing,
                                       :resources,
                                       :web_examples,
                                       :special_functions,
                                       :social_network,
                                       { web_sites: [:id, :url, :description] })
 end

答案 1 :(得分:0)

  1. 在web_request_params的控制器中更改web_site_attributes的web_sites_attributes
  2. 在您的控制器中删除@web_request.web_sites.build
  3. 中的评论
  4. 在您的视图中删除1.times do
  5. 使用f.fields_for :web_sites do |ff|
  6. 更改f.simple_fields_for :web_sites do |ff|