【问题标题】:Rails 3 Nested Models unknown attribute ErrorRails 3嵌套模型未知属性错误
【发布时间】:2011-07-27 00:48:36
【问题描述】:
  • 我有一个模型“问题”和一个嵌套模型“关系”
  • 在我提到的 issue.rb 中:

    has_many :relationships, :dependent => :destroy
    accepts_nested_attributes_for :relationships, :allow_destroy => true
    
  • 在relationship.rb中我提到过:

    belongs_to :issue
    
  • 在 Ryan Bates Railcast#196 之后,我的问题控制器中有以下内容:

    relationship = @issue.relationships.build
    

但是,我遇到错误“未知属性:关系”

我在这里做错了吗?我确实在日志中看到将关系属性传递给服务器,但是这个错误不会让创建成功。

我在 Rails 方面的专业知识是初学者水平,所以如果我提出一个可能被认为微不足道的问题,请见谅。

感谢您的帮助。

编辑:相关控制器代码:

    @relationship = @issue.relationships.build
    #@relationship = Relationship.new(params[:relationship])
    if @relationship.issue_id = ''
      @relationship.issue_id = @issueid
    end

    if @relationship.cause_id = ''
      @relationship.cause_id = @issueid
    end

    @relationship.save
    redirect_to(:back, :notice => 'New Relationship was created') 

我在跟踪中看到的:

    ActiveRecord::UnknownAttributeError in IssuesController#create
    unknown attribute: relationship

在问题参数中,我看到关系参数按预期传递:

    "relationship"=>{"issue_id"=>"100",
    "cause_id"=>""}

另一个更新 发布 form_for 代码:

    - form_for Issue.new do |f|

      .field  
        = f.text_field :description, :class=>"formfield", :id=>"frm_descr"

      .field  
        = f.hidden_field :wiki_url, :class=>"formfield", :id=>"frm_wiki_url"

      .field
        = f.hidden_field :short_url, :class=>"formfield", :id=>"frm_img_url"

      .field
        = f.hidden_field :title, :class=>"formfield", :id=>"frm_title"         

      = f.fields_for :relationship do |builder|
        = builder.text_field :issue_id, :class=>"form_field", :id=>"frm_rel_issue_id", :value=>@issue.id 
        = builder.text_field :cause_id, :class=>"form_field", :id=>"frm_rel_cause_id"

      .actions
        = f.submit 'Create', :class=>"save_button", :name=>"save_issue_rel_button", :id=>"val_collector"

【问题讨论】:

  • 请添加您的完整控制器操作,并可能提供堆栈跟踪以显示错误发生的位置。
  • @Thilo:编辑问题以包括相关的控制器代码和跟踪。
  • 应该传递给您的 IssuesController#create 方法的不是“relationship”,而是“relationship_attributes”哈希。您是否使用 fields_for 作为截屏节目?
  • @Thilo:刚刚发布了另一个 EDIT 以包含我正在使用的 Form_for 代码。
  • @Ryan:rails 文档还指出嵌套属性的参数应该作为属性哈希传递给服务器,即在这种情况下,relationship_attributes => { :blah => 'blah' }。我们看到这没有发生。我该如何更正它以作为属性哈希传递?

标签: ruby-on-rails ruby-on-rails-3 nested-forms railscasts


【解决方案1】:

改变这一行

= f.fields_for :relationship do |builder|

到这里:

= f.fields_for :relationships do |builder|

您的问题 has_many 关系 - 复数。这将为您提供正确的关系属性参数。

【讨论】:

    【解决方案2】:

    下面是工作框架代码: 我创建了一个新项目并尝试了其他答案的组合,终于让它工作了。

    这是我的解决方案,之后是需要注意的事项。我正在使用不同的模型,所以请耐心等待:

    • 我的模型是:discussion has_many posts
    • 讨论没有属性。
    • 帖子有 content:text 和 discussion_id:integer。

    工作代码

    (模型)讨论.rb

    has_many :posts
    accepts_nested_attributes_for :posts
    

    (模型)post.rb

    belongs_to :discussion
    

    routes.rb

    resources :discussions do
      resources :posts
    end
    

    (讨论视图)_form.html.erb

    <%= form_for(@discussion) do |f| %>
      <%= f.fields_for :posts, @post do |p| %>
        <%= p.text_area :content %>
      <% end %>
      <%= f.submit %>
    <% end %>
    

    (控制器)讨论控制器.rb

      def new
        @discussion = Discussion.new
        @post = @discussion.posts.build
    
        respond_to do |format|
          format.html # new.html.erb
          format.xml  { render :xml => @discussion }
        end
      end
    
      def create
        @discussion = Discussion.new(params[:discussion])
    
        respond_to do |format|
          if @discussion.save
            format.html { redirect_to(@discussion, :notice => 'Discussion was successfully created.') }
            format.xml  { render :xml => @discussion, :status => :created, :location => @discussion }
          else
            format.html { render :action => "new" }
            format.xml  { render :xml => @discussion.errors, :status => :unprocessable_entity }
          end
        end
      end
    

    可能出错的事情

    首先,Thilo 是对的,如果我这样做,我会得到 unknown attribute: post

    # WRONG!
    f.fields_for :post
    

    其次,我必须在 new 操作中添加@post 实例变量,否则 post.context 文本区域将不会显示。

    # REQUIRED!
    @post = @discussion.posts.build
    

    第三,如果我使用f.fields_for @post,创建动作也会报错unknown attribute: post

    # WRONG!
    f.fields_for @post do |p|
    

    改用这个:

    # RIGHT!
    f.fields_for :posts, @post  do |p|
    

    结束

    是的,我希望我们能看到更多关于此的文档(看不到任何有用的文档)。例如,我看到一些 form_for [@discussion, @post] 的用法,但我永远无法使用它。

    【讨论】:

    • 未删除,等待发帖者将其编辑为更多的答案,而不是讨论尝试过的事情。
    • 谢谢! .build 做到了以相同的形式创建父母和孩子。
    【解决方案3】:

    通过使用accepts_nested_attributes,您创建了一个setter方法relationship_attributes=。

    我注意到有几件事需要改变。

    你不需要设置

    @relationship = @issue.relationships.build
    

    您的表单应如下所示(您有 f.fields_for :relationship)

    = form_for @issue do |f|
    
      # your issue fields here
    
      = f.fields_for :relationships do |r|
    
        # your relationship fields here
    

    这里的美妙之处在于您不必设置任何 id 或任何东西。

    【讨论】:

    • 不,应该是f.fields_for,否则它们将只是公开的字段。
    • @Ryan:所以如果我没有在控制器中设置@relationship = @issue.relationship.build,我会得到“#<0x8228cb8>
    【解决方案4】:

    我假设您正在控制器中构建关系,然后尝试在视图中使用它。为了使其可见,您必须将其设为实例变量。您需要做的就是在 relationship 的名称中添加一个 @ 符号,就像您对 @issue 所做的那样。

    @relationship = @issue.relationships.build
    

    编辑:由于 OP 在提出原始问题后提供了更多信息,因此该答案现在显然不适用。

    【讨论】:

    • 如果关系变量的范围仅限于相关操作,那应该没有区别。
    • @Jeremy:感谢您的回答,但即使这样做了,我也遇到了同样的错误。您能否建议为什么会发生这种情况?
    • 我认为这不是真正的解决方法。 @relationship 没有在任何地方的视图中引用。
    • Thilo:原来没有提供视图逻辑;我假设视图中也使用了“关系”这个名称,在这种情况下它不会被发现。瑞恩:你是对的。我之前的诊断是基于原始信息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多