简体   繁体   English

Rails属性未保存到数据库

[英]Rails Attributes not saving to Database

I'm using psql with my rails 4 app. 我在Rails 4应用程序中使用了psql。

I have had a working projects model for quite some time, but with a recent change to a model that is entirely unrelated, I now have a problem that means that my project form attributes are not being created. 我已经有一个工作项目模型很长时间了,但是由于最近对一个完全不相关的模型所做的更改,我现在遇到了一个问题,那就是我的项目表单属性没有被创建。 I look in the console and the attributes are marked 'nil' even though I complete them in the new project form. 我在控制台中查看,即使我以新项目形式完成了属性,这些属性也被标记为“ nil”。

The form elements are not saving. 表单元素未保存。

I have a projects model controller with permitted params as follows: 我有一个带有允许的参数的项目模型控制器,如下所示:

  def new
    #authorise @project
    @project = Project.new
    @project.scope = Scope.new
    @project.scope.datum = Datum.new
    @project.scope.material = Material.new
    @project.scope.mentoring = Mentoring.new
    @project.scope.participant = Participant.new
    @project.scope.funding = Funding.new
    @project.scope.ethic = Ethic.new
    @project.scope.group_research = GroupResearch.new
    @project.scope.backgroundip = Backgroundip.new

  end

I have some nested attributes from other models. 我有一些来自其他模型的嵌套属性。 Project params include both project attributes and nested attributes. 项目参数包括项目属性和嵌套属性。

def project_params
      params[:project].permit(
      :title, :description, :video_proposal, :link_to_video_proposal, :draft, 
      :expiry_date_for_sponsor_interest,  :motivation, :approach,
      :results, :completion_date, :start_date, :industry_id, :public, :recurring_project,
      :frequency, :date_for_student_invitation, :date_for_student_interest, :closed, :student_objective, 
      :industry_relevance, :hero_image, :report, :standard_licence, :bespoke_licence, :bespoke_licence_form,
      :research_consulting, :other_outcome,
      scope_attributes: [:data, :materials, :mentoring, :participants, :participants, :funding, :ethics, :group, :project_id,
                         datum_attributes: [:prim_sec, :qual_quant, :survey, :survey_link, :experiment, :other_type, :other_description,
                                           :confidential, :data_description, :scope_id],
                         material_attributes: [:mattype, :description, :scope_id],
                         mentoring_attributes: [:frequency, :description, :scope_id],
                         funding_attributes: [ :expenses, :honorarium, :financing, :currency, :size, :amount_expenses, :amount_honorarium,
                                  :amount_principal_financing, :return_on_finance, :period_of_return, :expense_description, :scope_id],
                         participant_attributes: [ :title, :description, :location, :costs, :participation_cost, :eligibility, :eligibility_criteria,
                            :location_specific, :scope_id, :currency ],
                         group_research_attributes: [:number_of_group_members, :scope_id],
                         ethic_attributes: [:obtained, :date_expected, :ethics_comment, :ethics_policy_link, :scope_id],
                         backgroundip_attributes: [:scope_id, :copyright, :design, :patent, :trademark, :geographical_indication,
                                                   :trade_secret, :other, :identifier_copyright, :identifier_design, :identifier_patent,
                                                   :identifier_trademark, :identifier_geographical_indication, :identifier_trade_secret,
                                                   :identifier_other, :description, :registered_owner, :unregistered_interest, :conditions ]
                        ]
      )

      params.require(:project).permit(:link_to_video_proposal)

    end

I have a project new form which has: 我有一个新的项目表格,其中包括:

      <%= simple_form_for @project do |f| %>
      <% if @project.errors.any? %>
      <div id="error_explanation">
        <h2>Looks like there might be <%= pluralize(@project.errors.count, "error") %> with this project proposal. Take a look and let us know if we can help to sort them out.</h2>
        <% end %>
        <ul>
          <% @project.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
          <% end %>
        </ul>
      </div>
    </div>
  </div>


  <div class="row">
    <div class="col-md-10 col-md-offset-1">
      <div class="headerquestion-project">Project Overview
      </div>
    </div>
  </div>

  <div class="row">
    <div class="col-md-3 col-md-offset-1">
      <%= f.label  'Project title',   :class => 'question-project' %>
    </div>
    <div class="col-md-7">
      <%= f.input :title, label: false,  placeholder: 'A catchy, relevant title for your project', :input_html => {:style=> 'width: 100%', class: 'response-project'} %>
    </div>
  </div>

When I try to complete the form and press submit, I get an error saying that title cannot be blank. 当我尝试填写表格并按Submit时,出现错误,提示标题不能为空。 I add a title, press submit and the view does not show the title and the console shows that the title did not save. 我添加了一个标题,按提交,该视图不显示该标题,控制台显示该标题未保存。

Any ideas as to how to fix this issue? 关于如何解决此问题的任何想法?

Thank you 谢谢

You are overriding permitted parameters. 您正在覆盖允许的参数。 You need to write you project_params method something like this: 您需要编写类似于以下内容的project_params方法:

def project_params
    params.require(:project).permit( 
      ...
      ...
    )
end

Note that I have used params.require(:project) instead of directly accessing params like hash ( params[:project] ) and have removed your last statement from this method which was overriding the permitted parameters. 请注意 ,我使用params.require(:project)而不是直接访问诸如hash( params[:project] )之类的参数,并且已从该方法中删除了最后一个覆盖了允许参数的语句。

Another thing, try to avoid using public as an attribute/column name. 另一件事,请尝试避免将public用作属性/列名称。 This is a reserved keyword of ruby. 这是ruby的保留关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM