Rails 3嵌套模型未知属性错误 [英] Rails 3 Nested Models unknown attribute Error

查看:101
本文介绍了Rails 3嵌套模型未知属性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我有一个模型问题"和一个嵌套模型关系"
  • 在issue.rb中,我提到:

  • I have a model "Issue" and a nested Model "Relationship"
  • In the issue.rb I have mentioned:

has_many :relationships, :dependent => :destroy
accepts_nested_attributes_for :relationships, :allow_destroy => true

  • 在Relationship.rb中,我提到过:

  • In relationship.rb I have mentioned:

    belongs_to :issue
    

  • 跟随Ryan Bates Railcast#196,我的issue_controller中包含以下内容:

  • Following Ryan Bates Railcast#196 I have the following in my issues_controller:

    relationship = @issue.relationships.build
    

  • 但是,我遇到一个错误未知属性:关系"

    However, I am encountering an error "unknown attribute: relationship"

    我在这里做错了吗?我确实在日志中看到了关系属性传递给服务器的信息,但是,此错误并未使创建成功.

    Am I doing something incorrectly here? I do see the Relationships Attributes being passed to the server in the log however, this error does not let the create to be successful.

    我对Rails的专长是初学者,所以请问一个可能不重要的问题,对不起.

    My expertise with rails is beginners level so kindly excuse me if I am asking a question which maybe deemed trivial.

    感谢您的帮助.

    编辑:相关的控制器代码:

    EDIT: The relevant Controller code:

        @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') 
    

    我在轨迹上看到的东西:

    What I see on the trace:

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

    在Issue参数中,我看到Relationship参数按预期方式传递:

    Among the Issue parameters, I see the Relationship params being passed as expected:

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

    另一个更新 发布form_for代码:

    ANOTHER UPDATE Posting the form_for code:

        - 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"
    

    推荐答案

    更改此行

    = f.fields_for :relationship do |builder|
    

    对此:

    = f.fields_for :relationships do |builder|
    

    您的问题有很多关系-复数.这样可以为您提供正确的Relationships_attributes参数.

    Your issue has_many relationships - plural. That will give you the correct relationships_attributes parameters.

    这篇关于Rails 3嵌套模型未知属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆