我在我的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>
当我尝试填写表单并按提交时,我收到一条错误消息,指出标题不能为空。我添加一个标题,按提交,视图不显示标题,控制台显示标题没有保存。
有关如何解决此问题的任何想法?
谢谢
答案 0 :(得分:2)
您正在覆盖允许的参数。你需要写一个像这样的project_params
方法:
def project_params
params.require(:project).permit(
...
...
)
end
注意我使用了params.require(:project)
而不是直接访问hash(params[:project]
)这样的参数,并从此方法中删除了最后一条语句,该语句覆盖了允许的参数。
另一件事,尽量避免使用 public 作为属性/列名。这是ruby的保留关键字。