Rails 属性未保存到数据库

Rails Attributes not saving to Database

我在 rails 4 应用程序中使用 psql。

我有一个工作项目模型已经有一段时间了,但是最近对一个完全不相关的模型进行了更改,我现在遇到了一个问题,这意味着我的项目表单属性没有被创建。我在控制台中查看,属性被标记为 'nil',即使我在新项目表单中完成它们也是如此。

表单元素未保存。

我有一个项目模型控制器,允许的参数如下:

  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

我有一些来自其他模型的嵌套属性。项目参数包括项目属性和嵌套属性。

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

我有一个项目新表单,其中包含:

      <%= 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>

当我尝试填写表格并按提交时,出现标题不能为空的错误消息。我添加了一个标题,按提交,但视图不显示标题,控制台显示标题未保存。

关于如何解决这个问题有什么想法吗?

谢谢

您正在覆盖允许的参数。你需要像这样写你的 project_params 方法:

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

请注意 我使用了 params.require(:project) 而不是直接访问像 hash (params[:project]) 这样的参数,并且已经从这个方法中删除了你的最后一条语句覆盖允许的参数。

另一件事,尽量避免使用 public 作为 attribute/column 名称。这是ruby.

的保留关键字