导轨 - 显示关联的属性 - 未经许可参数 [英] Rails - Displaying associated attributes - unpermitted parameters

查看:191
本文介绍了导轨 - 显示关联的属性 - 未经许可参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Rails的一个应用程序4。

我有一个剖面模型,资质模型和视觉模型。

的关联是:

profile.rb

 的has_many:资格
 accepts_nested_attributes_for:资格  HAS_ONE:愿景
    accepts_nested_attributes_for:愿景

qualification.rb

  belongs_to的:简介

vision.rb

  belongs_to的:简介

我有一个类似的结构等几款车型的资格和视觉(即它们属于配置文件)。

所有相关属性已经被列入模型,并在那里强PARAMS它们嵌套,也在轮廓强PARAMS。

 高清profile_params
      PARAMS [:资料] .permit(:USER_ID,:标题:英雄:概述,:research_interest,:职业:external_profile,
        :working_languages​​,:tag_list,
          personality_attributes:[:average_day,:fantasy_project,:preferred_style]
          vision_attributes:[:long_term,:immediate_challenge]
          qualifications_attributes:[:ID,:水平,:标题:year_earned,:机构:_destroy])
    结束

的形式设置为:

 <%= simple_form_for(@profile)做| F | %GT;
            &所述;%= f.error_notification%GT;              < D​​IV CLASS =表单输入>< D​​IV CLASS =intpol2>
              你的专业资格
            < / DIV>              <%=渲染资格/表,F:F%>          < / DIV> < D​​IV CLASS =intpol2>
              你的研究视野
            < / DIV>            <%=呈现的愿景/表,F:F%>
          < / DIV>< D​​IV CLASS =表单行动>
                <%= f.button:提交,提交,:类=> formsubmit'%GT;
              < / DIV>
        <%结束%GT;

然后在资格形式,我有:

 <%= simple_fields_for:资格做| F | %GT;  < D​​IV CLASS =表单输入>
    < D​​IV CLASS =行>
        < D​​IV CLASS =COL-MD-6>
            <%= f.input:标题:标签=> 你奖%GT;
        < / DIV>        < D​​IV CLASS =COL-MD-6>
        < / DIV>
    < / DIV>    < D​​IV CLASS =行>
        < D​​IV CLASS =COL-MD-6>
            <%= f.input:水平,收藏:[学士学位,硕士,博士,博士后奖]%GT;
        < / DIV>
        < D​​IV CLASS =COL-MD-6>
        <%= f.input:year_earned,:标签=> 你什么时候毕业?,集合:(Date.today.year - 50)。(Date.today.year)%GT;
        < / DIV>  < / DIV>
  < / DIV>
<%结束%GT;

和,同样,在视觉形式,我有:

 <%= simple_fields_for:愿景做| F | %GT;
  &所述;%= f.error_notification%GT;  < D​​IV CLASS =表单输入>
    <%= f.input:long_term,如:文字:标签=> 你有什么长期的研究视野?:input_html => {:行数=> 10}%GT;    <%= f.input:immediate_challenge,如:文字:标签=> 你看到了什么是最迫切的挑战,以追求你长远的眼光来解决?:input_html => {:行数=> 10}%GT;  < / DIV><%结束%GT;

问题是,这些属性不保存,当我preSS的配置文件形式提交。控制台显示没有输入在任一这些表

是否有其他步骤这个过程?

我觉得问题更深了。这是因为我有一个名为address.rb另一种模式。这是多态的。

的关联是:

address.rb

  belongs_to的:寻址,多态=>真正

profile.rb

 的has_many:地址,如:寻址

在我的个人资料显示,我有:

 <%=渲染的个人资料/地理'%GT;

在我的地理部分,我有:

 <%@如果profile.addresses.any? %GT;
        &所述;%= @ p​​rofile.addresses.first.country_name.titlecase%GT;
    <%其他%GT;
        <跨度类=profileeditlink>
            <%=的link_to添加您的位置,new_address_path%GT;
        < / SPAN>
    <%结束%GT;

控制台显示我有4个保存的地址。我只有一个用户。

的个人资料显示页会显示其他状态(即添加一个地址),即使有一个地址保存。

这是我的个人资料显示页面上显示成功的唯一属性,是那些在配置文件表。所有关联的属性只是显示为空白。那些在资格/愿景,都没有保存到数据库中。

我想不通这是为什么全乱了。

这个问题的另一个角度是,我的资历形式已保存的条目。它示出了在导轨控制台。然而,我的学历部分是应该显示该条目,只显示标题。

 < D​​IV CLASS =profilequalificationstitle>
                    资格
                < / DIV>
            < / DIV>
        < / DIV>
        <%Qualification.all.sort_by(安培;:year_earned)。每个做|资格赛| %GT;        < D​​IV CLASS =行>                  < D​​IV CLASS =COL-MD-12>
                    < D​​IV CLASS =profilequalifications>
                        <%= @ p​​rofile.try(:资质)。尝试(:名称)%GT;
                    < / DIV>
                  < / DIV>
        < / DIV>        < D​​IV CLASS =行>
                < D​​IV CLASS =COL-MD-4>
                    < D​​IV CLASS =intpolmin>
                        <%= @ p​​rofile.try(:资质)。尝试(:year_earned)%计算值:
                    < / DIV>
                < / DIV>
                < D​​IV CLASS =COL-MD-8>
                    < D​​IV CLASS =intpolmin>
                        <%= @ p​​rofile.try(:资质)。尝试(:机构)%GT;
                    < / DIV>
                < / DIV>
        < / DIV>          <%结束%GT;    < / DIV>
< / DIV>

TAKING RICH PECK的建议下图:

我改变我的强烈PARAMS定义的格式,在配置文件控制器,以便它不使用由导轨支架提供的格式,而是显示为:

 高清profile_params
     params.require(:配置文件).permit(:USER_ID,:标题:英雄:概述,:research_interest,:职业:external_profile,
        :working_languages​​,:tag_list,
          user_attributes:[:头像]
          personality_attributes:[:average_day,:fantasy_project,:preferred_style]
          vision_attributes:[:long_term,:immediate_challenge]
          qualifications_attributes:[:ID,:水平,:标题:year_earned,:机构:_destroy])
    结束

所有相关车型仍采用滑轨脚手架格式 - 即:

 高清vision_params
      PARAMS [:视力] .permit(:PROFILE_ID,:long_term,:immediate_challenge)
    结束

在配置文件控制的新方法有:

 高清新
    @profile = Profile.new
    @ profile.qualifications.build
    @ profile.vision.build
    @ profile.personality.build    授权@profile
  结束

至于其中的关联是前pressed的方式,其个人preference保持与关联嵌套的属性。我可以看到,这个建议使用较少的线条来达到同样的事情,但我的preference是有嵌套属性声明的关联。

我的剖面模型关联是:

  belongs_to的:用户
  的has_many:地址,如:寻址  HAS_ONE:个性
    accepts_nested_attributes_for:个性  的has_many:资格
    accepts_nested_attributes_for:学历,reject_if:all_blank,allow_destroy:真  HAS_ONE:愿景
    accepts_nested_attributes_for:愿景

我的个人资料显示页面然后尝试使用其他型号的属性在许多方面。它的工作原理成功,当我尝试使用来自用户模型属性(配置文件所属的用户)。

当我尝试使用属于配置文件从模型属性它不工作。

我在使用这些属性通过以下方式 - 其中没有一个工作:


  1. 渲染的部分:
                        <%=渲染的个人资料/学历%>
                    

 资格

 < D​​IV CLASS =行>              < D​​IV CLASS =COL-MD-12>
                < D​​IV CLASS =profilequalifications>
                     <! - @ qualification.award.profile - >
                < / DIV>
              < / DIV>
    < / DIV>

<醇开始=2>

  • 直接引用属性:LT;%= @ p​​rofile.try(:视觉)。尝试(:long_term)%>

  • 然后 - 我的视力和个性嵌套表格格式为:

     &LT;%= f.simple_fields_for:配置文件来执行| F | %GT;
          &LT;%= f.simple_fields_for:愿景做| FF | %GT;
      &LT; D​​IV CLASS =表单输入&GT;
        &LT;%= ff.input:long_term,如:文字:标签=&GT; 你有什么长期的研究视野?:input_html =&GT; {:行数=&GT; 10}%GT;    &LT;%= ff.input:immediate_challenge,如:文字:标签=&GT; 你看到了什么是最迫切的挑战,以追求你长远的眼光来解决?:input_html =&GT; {:行数=&GT; 10}%GT;  &LT; / DIV&GT;&LT;%结束%GT;
    &LT;%结束%GT;

    我的资质形式略有不同,因为它使用的宝石茧增加功能重复这种形式好资格。

    进一步调查

    我已经找到了线索。

    在我的终端,我可以将补丁创建表单属性后看到,有它说的错误:

     未经许可的参数:配置文件

    展望的是,我发现每个职位的,这表明了以下解决方案:

    解决方案1:更改配置文件控制的强烈参数,包括母公司的id(在我的情况:PROFILE_ID):

    <一个href=\"http://stackoverflow.com/questions/25770268/unpermitted-parameters-profile-nestedattributes-rails-4\">Unpermitted参数:配置文件(NestedAttributes) - 轨道4

    我尝试改变:

      vision_attributes:[:ID,:long_term,:immediate_challenge]

      vision_attributes:[:ID,:PROFILE_ID,:long_term,:immediate_challenge]

    结果:没有任何区别 - 生成相同的错误

    解决方案2:更改表单

    <一个href=\"http://stackoverflow.com/questions/18474599/nested-attributes-unpermitted-parameters-rails-4\">Nested属性 - 未经许可参数轨道4

    采取这一建议,我试图改变视觉形式:

     &LT;%= f.simple_fields_for:profile_attributes做| F | %GT;
          &LT;%= f.simple_fields_for:愿景,@ profile.vision做| FF | %GT;
      &LT; D​​IV CLASS =表单输入&GT;
        &LT;%= ff.input:long_term,如:文字:标签=&GT; 你有什么长期的研究视野?:input_html =&GT; {:行数=&GT; 10}%GT;    &LT;%= ff.input:immediate_challenge,如:文字:标签=&GT; 你看到了什么是最迫切的挑战,以追求你长远的眼光来解决?:input_html =&GT; {:行数=&GT; 10}%GT;  &LT; / DIV&GT;&LT;%结束%GT;
    &LT;%结束%GT;

    结果:没有任何区别 - 同样的错误结果。

    解决方法3:在路上的另一变型较强的参数在配置文件控制器pssed前$ P $:

    <一个href=\"http://stackoverflow.com/questions/15919761/rails-4-nested-attributes-unpermitted-parameters\">Rails 4嵌套属性未经许可参数

    改变方式嵌套强PARAMS是从型材控制器pssed前$ P $:

      vision_attributes:[:ID,:PROFILE_ID,:long_term,:immediate_challenge]

     :vision_attributes [:ID,:PROFILE_ID,:long_term,:immediate_challenge]

    结果:

    一个新的错误:结果
    语法错误,意外',',期待=> ... ng_term,:immediate_challenge] ... ^ /Users/mem/cl/cf3/app/controllers/profiles_controller.rb:95:语法错误,意外',' ,预计keyword_end /Users/mem/cl/cf3/app/controllers/profiles_controller.rb:96:语法错误,意外',',期待keyword_end /Users/mem/cl/cf3/app/controllers/profiles_controller.rb:97 :语法错误,意外')',预计keyword_end

    林卡住,失落和沮丧。刚刚下降了$$比我能在codementor.io买得起的成果是导师犯规知道如何去帮助。喜欢一些想法什么尝试下一个。

    另一种解决方案

    我发现这篇文章:的http://www.sitepoint.com/complex-rails-forms-with-nested-attributes/#adding-an-address

    拍摄该教程中所示的方法,我尝试创建具有形式帮助:

     模块表单助手
      高清setup_profile(配置文件)
        profile.vision || = Vision.new
        轮廓
      结束
    结束

    (我不知道是什么的定义体的第二行字纹意思),但它是在本教程为用户/地址所采取的做法是一致的。

    然后我改变我的个人资料的形式:

     &LT;%= f.simple_fields_for(setup_profile(配置文件))做| F | %GT;              &LT;%=呈现的愿景/表,F:F%&GT;
                &LT;%结束%GT;

    和改变我的愿景形成偏:

     &LT; D​​IV CLASS =表单输入&GT;
        &LT;%= ff.input:long_term,如:文字:标签=&GT; 你有什么长期的研究视野?:input_html =&GT; {:行数=&GT; 10}%GT;    &LT;%= ff.input:immediate_challenge,如:文字:标签=&GT; 你看到了什么是最迫切的挑战,以追求你长远的眼光来解决?:input_html =&GT; {:行数=&GT; 10}%GT;  &LT; / DIV&GT;

    当我保存这些更改,然后再试一次,我得到这个错误:

     未定义的局部变量或方法`个人资料'的#&LT;#&LT;班级:0x007fe113fcd680&GT;:&0x007fe11f2769d0 GT;

    该错误信息指向该行的轮廓形成:

     &LT;%= f.simple_fields_for(setup_profile(配置文件))做| F | %GT;

    我不知道这是什么错误消息意味着,但我认为这是因为配置文件应为'@profile'或':个人资料 - 我不知道为什么,它只是经常出现这样

    如果我此行更改为:

     &LT;%= f.simple_fields_for(setup_profile(@profile))做| F | %GT;

    我得到这个错误:

     为#&LT未定义的方法`long_term';简介:0x007fe11c241c38&GT;

    我觉得这个错误表示形式看,而不是视力表堪称简介表long_term的属性。

    如果我此行更改为:

     &LT;%= f.simple_fields_for(setup_profile(@ profile.vision))做| F | %GT;

    我得到这个错误在我的表单辅助:

     未定义的方法`愿景为#&LT;愿景:0x007fe117e95278&GT;

    我一直用随意尝试的东西去之前,任何人都可以查看是否有与我试图实施现场点文章中介绍的解决方案的方式有问题?


    解决方案

    首先,您的PARAMS方法需要固定:

     高清profile_params
          params.require(:轮廓).permit(...

    有关澄清,你应该阅读上涨约 href=\"https://github.com/rails/strong_parameters\" rel=\"nofollow\">强PARAMS。


    您已经写了太多;这是应该的样子:

     #应用程序/模型/ profile.rb
    类档案&LT; ActiveRecord的::基地
       的has_many:资格
       HAS_ONE:愿景
       accepts_nested_attributes_for:资格:视力# - &GT;必须匹配协会名称
    结束

    当你创建一个简介,你需要建立关联的对象(除非它们已经存在):

     #应用程序/控制器/ profiles_controller.rb
    类ProfilesController&LT; ApplicationController中
       高清新
          @profile = Profile.new
          @ profile.qualifications.build
          @ profile.build_vision
       结束
    结束

    这将允许您使用以下形式:

     #应用程序/视图/型材/ new.html.erb
    &LT;%=的form_for @profile做| F | %GT;
       &LT;%= f.fields_for:资格做| Q | %GT;
          &LT;%= q.text_field:标题%GT;
          ...等...
       &LT;%结束%GT;
       &LT;%= f.fields_for:愿景做| V | %GT;
          &所述;%= v.text_field:long_term%GT;
          ...等...
       &LT;%结束%GT;
       &所述;%= f.submit%GT;
    &LT;%结束%GT;

    这应该创建一个新的简介&安培;相关资格 / 愿景的对象的要求。


    寻址态关联不应该有上述任何影响。

    虽然在第一混乱,多态关联基本上给你引用其可在许多不同的情况下使用的关联的能力。这是多态的事实无关紧要。

    IE Rails会仅仅使用的has_many:地址无论是为::寻址与否

    I am trying to make an app in Rails 4.

    I have a profile model, a qualification model, and a vision model.

    The associations are:

    profile.rb

    has_many :qualifications  
     accepts_nested_attributes_for :qualification 
    
      has_one :vision
        accepts_nested_attributes_for :vision    
    

    qualification.rb

    belongs_to :profile
    

    vision.rb

    belongs_to :profile
    

    I have several other models in a similar construct as qualification and vision (ie. they belong to profile).

    All relevant attributes have been included in the strong params for the model and where they are nested, also in the profile strong params.

    def profile_params
          params[:profile].permit(:user_id, :title, :hero, :overview, :research_interest, :occupation, :external_profile, 
            :working_languages, :tag_list,
              personality_attributes: [:average_day, :fantasy_project, :preferred_style],
              vision_attributes: [:long_term, :immediate_challenge], 
              qualifications_attributes: [:id, :level, :title, :year_earned, :institution, :_destroy] )
        end
    

    The forms are set up as:

    <%= simple_form_for(@profile) do |f| %>
                <%= f.error_notification %>
    
                  <div class="form-inputs">
    
    <div class="intpol2">
                  Your professional qualifications
                </div>
    
                  <%= render 'qualifications/form', f: f %>     
    
              </div>
    
     <div class="intpol2">
                  Your research vision
                </div>
    
                <%= render 'visions/form', f: f %>
              </div>
    
    <div class="form-actions">
                    <%= f.button :submit, "Submit", :class => 'formsubmit' %>
                  </div>
            <% end %>
    

    Then in the qualifications form, I have:

    <%= simple_fields_for :qualification do |f| %>
    
      <div class="form-inputs">
    
    
        <div class="row">
            <div class="col-md-6">
                <%= f.input :title, :label => "Your award" %> 
            </div>
    
            <div class="col-md-6">
    
    
            </div>
    
    
        </div>
    
        <div class="row">
            <div class="col-md-6">
                <%= f.input :level,   collection: [ "Bachelor's degree", "Master's degree", "Ph.D", "Post Doctoral award"] %>
            </div>
    
    
            <div class="col-md-6">
            <%= f.input :year_earned, :label => "When did you graduate?", collection: (Date.today.year - 50)..(Date.today.year) %>
            </div>
    
      </div>
    
    
    
    
      </div>
    <% end %>
    

    And, similarly, in the vision form, I have:

    <%= simple_fields_for :vision do |f| %>
      <%= f.error_notification %>
    
      <div class="form-inputs">
        <%= f.input :long_term, as: :text, :label => "What is your long term research vision?", :input_html => {:rows => 10} %>
    
        <%= f.input :immediate_challenge, as: :text, :label => "What do you see as the immediate challenge to be addressed in pursuit of your long-term vision?",  :input_html => {:rows => 10} %>
    
      </div>
    
    <% end %>
    

    The problem is, these attributes are not saving when I press submit on the profile form. The console shows that there are no inputs in either of these tables.

    Is there another step to this process?

    I think the problem runs deeper. This is because I have another model called address.rb. It's polymorphic.

    The associations are:

    address.rb

     belongs_to :addressable, :polymorphic => true
    

    profile.rb

    has_many :addresses, as: :addressable
    

    In my profiles show, I have:

    <%= render 'profiles/geography'  %>
    

    In my geography partial, I have:

    <% if @profile.addresses.any? %>
            <%= @profile.addresses.first.country_name.titlecase %> 
        <% else %>
            <span class="profileeditlink">
                <%= link_to "Add your location", new_address_path %>
            </span>
        <% end %>   
    

    The console shows I have 4 saved addresses. I only have one user.

    The profile show page displays the else condition (i.e. add an address), even when there is an address saved.

    The only attributes that successfully display on my profile show page, are those that are in the profiles table. All associated attributes just appear blank. Those in qualifications/vision, aren't saving to the db.

    I can't figure out why this is all messed up.

    Another angle to this problem is that my qualifications form has now saved an entry. It shows in the rails console. However, my qualifications partial which is supposed to display that entry, only shows the title.

    <div class="profilequalificationstitle">
                        Qualifications
                    </div>  
                </div>
            </div>
    
    
            <% Qualification.all.sort_by(&:year_earned).each do |qual| %>
    
            <div class="row">
    
                      <div class="col-md-12">
                        <div class="profilequalifications">
                            <%= @profile.try(:qualification).try(:title) %>
                        </div>  
                      </div>    
            </div>
    
            <div class="row">
                    <div class="col-md-4">
                        <div class="intpolmin">
                            <%= @profile.try(:qualification).try(:year_earned) %>: 
                        </div>  
                    </div>
                    <div class="col-md-8">
                        <div class="intpolmin">
                            <%= @profile.try(:qualification).try(:institution) %>
                        </div>  
                    </div>
            </div>  
    
              <% end %>
    
        </div>
    </div>               
    

    TAKING RICH PECK'S SUGGESTION BELOW:

    I change the format of my strong params definition in the profiles controller so that it doesnt use the format provided by rails scaffolding and instead appears as:

    def profile_params
         params.require(:profile).permit(:user_id, :title, :hero, :overview, :research_interest, :occupation, :external_profile, 
            :working_languages, :tag_list,
              user_attributes: [:avatar],
              personality_attributes: [:average_day, :fantasy_project, :preferred_style],
              vision_attributes: [:long_term, :immediate_challenge], 
              qualifications_attributes: [:id, :level, :title, :year_earned, :institution, :_destroy] )
        end
    

    All of the associated models still use the rails scaffolding format - i.e:

    def vision_params
          params[:vision].permit(:profile_id, :long_term, :immediate_challenge)
        end
    

    The new method in the profiles controller has:

    def new
        @profile = Profile.new
        @profile.qualifications.build
        @profile.vision.build
        @profile.personality.build
    
        authorize @profile
      end
    

    As for the manner in which the associations are expressed, its a personal preference to keep the nested attributes with the association. I can see that this suggestion uses less lines to achieve the same thing, but my preference is to have the association with the nested attribute declaration.

    My profile model associations are:

    belongs_to :user
      has_many :addresses, as: :addressable
    
      has_one :personality
        accepts_nested_attributes_for :personality
    
      has_many :qualifications  
        accepts_nested_attributes_for :qualifications,  reject_if: :all_blank, allow_destroy: true
    
      has_one :vision
        accepts_nested_attributes_for :vision    
    

    My profile show page then tries to use attributes from other models in a number of ways. It works successfully when I try to use attributes from the user model (profile belongs to user).

    It does not work when I try to use attributes from models that belong to profile.

    I have the following ways of using those attributes - none of which are working:

    1. render a partial: <%= render 'profiles/qualifications' %>

    Qualifications 
    

        <div class="row">
    
                  <div class="col-md-12">
                    <div class="profilequalifications">
                         <!-- @qualification.award.profile  -->
                    </div>  
                  </div>    
        </div>
    

    1. directly refer to the attribute: <%= @profile.try(:vision).try(:long_term) %>

    Then - my nested forms for vision and personality are in this format:

    <%= f.simple_fields_for :profile do |f| %>
          <%= f.simple_fields_for :vision do |ff| %>
    
    
      <div class="form-inputs">
        <%= ff.input :long_term, as: :text, :label => "What is your long term research vision?", :input_html => {:rows => 10} %>
    
        <%= ff.input :immediate_challenge, as: :text, :label => "What do you see as the immediate challenge to be addressed in pursuit of your long-term vision?",  :input_html => {:rows => 10} %>
    
      </div>
    
    <% end %>
    <% end %>
    

    My qualifications form is slightly different because it uses cocoon gem to add functionality to repeat that form for several qualifications.

    FURTHER INVESTIGATION

    I've found a clue.

    In my terminal, I can see after the patch for creating the attribute in the form, there is an error which says:

    Unpermitted parameter: profile
    

    Looking into that, I've found each of these posts, suggesting the following solutions:

    Solution 1: change the strong parameters in the profiles controller to include the id of the parent (in my case: profile_id):

    Unpermitted Parameters: profile (NestedAttributes) - RAILS 4

    I try changing:

    vision_attributes: [:id, :long_term, :immediate_challenge], 
    

    to

    vision_attributes: [:id, :profile_id, :long_term, :immediate_challenge], 
    

    Outcome: no difference - the same error is generated.

    Solution 2: change the form

    Nested attributes - Unpermitted parameters Rails 4

    Taking this suggestion, I tried changing the vision form to:

    <%= f.simple_fields_for :profile_attributes do |f| %>
          <%= f.simple_fields_for :vision, @profile.vision do |ff| %>
    
    
      <div class="form-inputs">
        <%= ff.input :long_term, as: :text, :label => "What is your long term research vision?", :input_html => {:rows => 10} %>
    
        <%= ff.input :immediate_challenge, as: :text, :label => "What do you see as the immediate challenge to be addressed in pursuit of your long-term vision?",  :input_html => {:rows => 10} %>
    
      </div>
    
    <% end %>
    <% end %>
    

    Outcome: no difference - the same error results.

    Solution 3: a further variation on the way strong parameters are expressed in the profiles controller:

    Rails 4 Nested Attributes Unpermitted Parameters

    change the way the nested strong params are expressed in the profiles controller from:

          vision_attributes: [:id, :profile_id, :long_term, :immediate_challenge], 
    

    to

          :vision_attributes[:id, :profile_id, :long_term, :immediate_challenge], 
    

    Outcome:

    A new error:
    syntax error, unexpected ',', expecting => ...ng_term, :immediate_challenge], ... ^ /Users/mem/cl/cf3/app/controllers/profiles_controller.rb:95: syntax error, unexpected ',', expecting keyword_end /Users/mem/cl/cf3/app/controllers/profiles_controller.rb:96: syntax error, unexpected ',', expecting keyword_end /Users/mem/cl/cf3/app/controllers/profiles_controller.rb:97: syntax error, unexpected ')', expecting keyword_end

    Im stuck, lost and frustrated. Have just dropped more $$ than I can afford on codementor.io for the outcome to be that the mentor doesnt know how to help. Would love some ideas for what to try next.

    ANOTHER SOLUTION

    I found this article: http://www.sitepoint.com/complex-rails-forms-with-nested-attributes/#adding-an-address

    Taking the approach shown in that tutorial, I tried creating a form helper which has:

    module FormHelper
      def setup_profile(profile)
        profile.vision ||= Vision.new
        profile
      end
    end
    

    (I don't know what the word profile on the second line of the definition body means) but it is consistent with the approach taken in the tutorial for user/address.

    I then change my profile form to:

    <%= f.simple_fields_for (setup_profile(profile)) do |f| %>
    
                  <%= render 'visions/form', f: f %>
                <% end %>  
    

    And change my visions form partial to:

      <div class="form-inputs">
        <%= ff.input :long_term, as: :text, :label => "What is your long term research vision?", :input_html => {:rows => 10} %>
    
        <%= ff.input :immediate_challenge, as: :text, :label => "What do you see as the immediate challenge to be addressed in pursuit of your long-term vision?",  :input_html => {:rows => 10} %>
    
      </div>
    

    When I save these changes and try again, I get this error:

    undefined local variable or method `profile' for #<#<Class:0x007fe113fcd680>:0x007fe11f2769d0>
    

    The error message points to this line of the profiles form:

        <%= f.simple_fields_for (setup_profile(profile)) do |f| %>
    

    I don't know what this error message means, but I think it's because profile should either be '@profile' or ':profile' - I don't know why, it just often appears that way.

    If i change this line to:

    <%= f.simple_fields_for (setup_profile(@profile)) do |f| %>
    

    I get this error:

    undefined method `long_term' for #<Profile:0x007fe11c241c38>
    

    I think this error means that the form is looking for an attribute called "long_term" in the profile table, instead of the vision table.

    If i change this line to:

    <%= f.simple_fields_for (setup_profile(@profile.vision)) do |f| %>
    

    I get this error in my form helper:

    undefined method `vision' for #<Vision:0x007fe117e95278>
    

    Before I keep going with randomly trying things, can anyone see if there is a problem with the way I have tried to implement the solution shown in the site point article?

    解决方案

    Firstly, your params method needs fixing:

    def profile_params
          params.require(:profile).permit(...
    

    For clarification, you should read up about strong params on github.


    You've written far too much; this is how it should look:

    #app/models/profile.rb
    class Profile < ActiveRecord::Base
       has_many :qualifications
       has_one :vision
       accepts_nested_attributes_for :qualifications, :vision #-> has to match association name
    end
    

    When you create a profile, you'll need to build the associative objects (unless they exist already):

    #app/controllers/profiles_controller.rb
    class ProfilesController < ApplicationController
       def new
          @profile = Profile.new
          @profile.qualifications.build
          @profile.build_vision
       end
    end
    

    This will allow you to use the following form:

    #app/views/profiles/new.html.erb
    <%= form_for @profile do |f| %>
       <%= f.fields_for :qualifications do |q| %>
          <%= q.text_field :title %>
          ... etc ...
       <% end %>
       <%= f.fields_for :vision do |v| %>
          <%= v.text_field :long_term %>
          ... etc ...
       <% end %>
       <%= f.submit %>
    <% end %>
    

    This should create a new profile & associated qualification / vision objects as required.


    Your addressable polymorphic association shouldn't have any bearing on the above.

    Although confusing at first, polymorphic associations basically give you the ability to reference associations which can be used in many different situations. The fact it's polymorphic doesn't matter.

    IE Rails will just be using has_many :addresses whether it's as: :addressable or not.

    这篇关于导轨 - 显示关联的属性 - 未经许可参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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