我在我的Rails 4应用程序中使用Simple Form。
我有一个结构,其中包含每个项目,范围和数据的模型。
数据属于范围,范围属于项目。
如果范围表单中问题的答案为真,我刚刚对项目展示页面中应显示的数据进行了部分查看。
我有:
我的项目中的if语句显示视图,询问项目范围表单中问题的答案是否为真,如下所示:
<% if @project.scope.try(:data) == true %>
<%= render 'data/particulars'%>
<% end %>
我的范围表格中的问题:
<%= f.input :data, :as => :boolean, :label => false, :inline_label => true %>
我的views文件夹的数据文件夹中的数据/ _particulars部分包含:
<div class="containerfluid">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="dataheading">Detail of the data requested</div>
<div class="datasubtext"><%= @project.scope.try(:datum).try(:prim_sec) %>
<%= @project.scope.try(:datum).try(:qual_quant) %>
</div>
<div class="datageneral">
<%= @project.scope.try(:datum).try(:survey_link) %>
<%= @project.scope.try(:datum).try(:other_description) %>
<%= @project.scope.datum.try(:data_description) %>
<% if @project.scope.try(:datum).try(:confidential) == true %>
Your confidential or commercially sensitive data will be protected with a standard Non-Disclosure Agreement, the form of which is here
</div>
<% end %>
</div>
</div>
</div>
当我测试项目显示页面时,如果表单将范围问题回答为true(因此需要显示数据),则不会显示任何内容。谷歌检查员无法识别数据局部视图。
在rails控制台中,我尝试了:
SELECT "projects".* FROM "projects" WHERE "projects"."id" = 25
=> #<ActiveRecord::Relation [#<Project id: 25, title: "asdf ", description: "this is the overview", video_proposal: true, link_to_video_proposal: "IMG_0034.MOV", draft: false, expiry_date_for_sponsor_interest: "2015-05-10 00:00:00", slug: nil, created_at: "2015-04-10 04:53:05", updated_at: "2015-04-10 04:54:37", hero_image: "IMG_1631.JPG", motivation: "asdf asdf", approach: "asdf asdf", results: "asdf adfasdf ", completion_date: "2015-09-10", start_date: "2015-06-10", courses_id: nil, video_id: nil, recurring_project: true, frequency: "true", date_of_student_invitation: nil, date_for_student_interest: "2015-05-10", closed: false, student_id: nil, creator_id: 1, educator_id: nil, project_question_id: nil, project_answer_id: nil, published_date: nil, student_objective: "sdaf safasdf", industry_relevance: "asdf asdfaf", report: true, standard_licence: false, bespoke_licence: false, bespoke_licence_form: "", research_consulting: false, other_outcome: "0", industry_id: 7, limited_disclosure: nil, disclose: true>]>
2.1.1p76 :013 >
2.1.1p76 :008 > Scope.where(project_id: 25)
Scope Load (4.3ms) SELECT "scopes".* FROM "scopes" WHERE "scopes"."project_id" = 25
=> #<ActiveRecord::Relation [#<Scope id: 6, created_at: "2015-04-10 07:40:04", updated_at: "2015-04-10 07:40:04", if_datum: false, if_material: false, if_mentoring: false, if_participant: false, if_funding: false, if_ethic: true, if_group_research: false, project_id: 25, program_id: nil, proposal_id: nil>]>
2.1.1p76 :005 > puts Project.scope.try(:data).inspect
我得到的结果是:
ArgumentError:参数个数错误(0表示1..2)
来自/Users/mm/.rvm/gems/ruby-2.1.1/gems/activerecord-deprecated_finders-1.0.3/lib/active_record/deprecated_finders/base.rb:70:in scope'
from (irb):5
from /Users/mm/.rvm/gems/ruby-2.1.1/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in
start'
来自/Users/mm/.rvm/gems/ruby-2.1.1/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in start'
from /Users/mm/.rvm/gems/ruby-2.1.1/gems/railties-4.0.2/lib/rails/commands.rb:62:in
'
来自bin / rails:4:require'
from bin/rails:4:in
'
但是,如果我尝试:
2.1.1p76 :010 > puts Scope.try(:datum).inspect
nil
=> nil
Taryn的建议:
2.1.1p76 :013 > project = Project.find(25)
Project Load (34.8ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT 1 [["id", 25]]
=> #<Project id: 25, title: "asdf ", description: "this is the overview", video_proposal: true, link_to_video_proposal: "IMG_0034.MOV", draft: false, expiry_date_for_sponsor_interest: "2015-05-10 00:00:00", slug: nil, created_at: "2015-04-10 04:53:05", updated_at: "2015-04-10 04:54:37", hero_image: "IMG_1631.JPG", motivation: "asdf asdf", approach: "asdf asdf", results: "asdf adfasdf ", completion_date: "2015-09-10", start_date: "2015-06-10", courses_id: nil, video_id: nil, recurring_project: true, frequency: "true", date_of_student_invitation: nil, date_for_student_interest: "2015-05-10", closed: false, student_id: nil, creator_id: 1, educator_id: nil, project_question_id: nil, project_answer_id: nil, published_date: nil, student_objective: "sdaf safasdf", industry_relevance: "asdf asdfaf", report: true, standard_licence: false, bespoke_licence: false, bespoke_licence_form: "", research_consulting: false, other_outcome: "0", industry_id: 7, limited_disclosure: nil, disclose: true>
2.1.1p76 :014 > puts project.scope.try(:data)
Scope Load (2.1ms) SELECT "scopes".* FROM "scopes" WHERE "scopes"."project_id" = $1 ORDER BY "scopes"."id" ASC LIMIT 1 [["project_id", 25]]
=> nil
后续步骤:
2.1.1p76 :015 > puts project.inspect
#<Project id: 25, title: "asdf ", description: "this is the overview", video_proposal: true, link_to_video_proposal: "IMG_0034.MOV", draft: false, expiry_date_for_sponsor_interest: "2015-05-10 00:00:00", slug: nil, created_at: "2015-04-10 04:53:05", updated_at: "2015-04-10 04:54:37", hero_image: "IMG_1631.JPG", motivation: "asdf asdf", approach: "asdf asdf", results: "asdf adfasdf ", completion_date: "2015-09-10", start_date: "2015-06-10", courses_id: nil, video_id: nil, recurring_project: true, frequency: "true", date_of_student_invitation: nil, date_for_student_interest: "2015-05-10", closed: false, student_id: nil, creator_id: 1, educator_id: nil, project_question_id: nil, project_answer_id: nil, published_date: nil, student_objective: "sdaf safasdf", industry_relevance: "asdf asdfaf", report: true, standard_licence: false, bespoke_licence: false, bespoke_licence_form: "", research_consulting: false, other_outcome: "0", industry_id: 7, limited_disclosure: nil, disclose: true>
=> nil
2.1.1p76 :016 > puts project.scope.inspect
#<Scope id: 6, created_at: "2015-04-10 07:40:04", updated_at: "2015-04-10 07:40:04", if_datum: false, if_material: false, if_mentoring: false, if_participant: false, if_funding: false, if_ethic: true, if_group_research: false, project_id: 25, program_id: nil, proposal_id: nil>
=> nil
2.1.1p76 :017 > puts project.scope.try(:data).inspect
nil
=> nil
项目表有:
t.string "title"
t.text "description"
t.boolean "video_proposal"
t.string "link_to_video_proposal"
t.boolean "draft", default: true
t.datetime "expiry_date_for_sponsor_interest"
t.string "slug"
t.datetime "created_at"
t.datetime "updated_at"
t.string "hero_image"
t.text "motivation"
t.text "approach"
t.text "results"
t.date "completion_date"
t.date "start_date"
t.integer "courses_id"
t.string "video_id"
t.boolean "recurring_project", default: true
t.string "frequency"
t.date "date_of_student_invitation"
t.date "date_for_student_interest"
t.boolean "closed", default: false
t.integer "student_id"
t.integer "creator_id"
t.integer "educator_id"
t.integer "project_question_id"
t.integer "project_answer_id"
t.date "published_date"
t.text "student_objective"
t.text "industry_relevance"
t.boolean "report"
t.boolean "standard_licence"
t.boolean "bespoke_licence"
t.string "bespoke_licence_form"
t.boolean "research_consulting"
t.text "other_outcome"
t.integer "industry_id"
t.string "limited_disclosure"
t.boolean "disclose", default: true
范围表有:
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "if_datum", default: false
t.boolean "if_material", default: false
t.boolean "if_mentoring", default: false
t.boolean "if_participant", default: false
t.boolean "if_funding", default: false
t.boolean "if_ethic", default: true
t.boolean "if_group_research", default: false
t.integer "project_id"
t.integer "program_id"
t.integer "proposal_id"
基准模型:
class Datum < ActiveRecord::Base
belongs_to :scope
end
范围模型:
class Scope < ActiveRecord::Base
belongs_to :project
has_one :datum
accepts_nested_attributes_for :datum
end
有谁知道我做错了什么?
谢谢
答案 0 :(得分:1)
这是正确的......你的代码明确地说明了范围has_one datum
。它没有data
- 该方法不存在且永远不会(除非你设置它)。所以,对于任何给定的范围,你只能使用datum
- 这就是所有可用的。这就是你应该使用的。如果你想要不同的行为 - 你需要改变你的设置
所以这是有效的代码:
project.scope.datum
这不是:
project.scope.data
这将始终返回false(实际上为零)
project.scope.try(:data)
这个人希望能回到nil以外的东西:
project.scope.try(:datum)
你可能实际上并不是指你在这里尝试的东西:
@project.scope.try(:data) == true
上述情况不会奏效。首先,data
类不存在scope
。只有datum
存在 - 因为这是你在课堂上写的。其次...... Rails工作的方式是在你调用Datum
时设置类scope.datum
的实际实例 - 它不会等于true
...它将存在是否存在......即它是否存在。
如果上面代码背后的意图是“如果这个范围有一个数据......然后做X”那么你应该像这样编写条件:
@project.scope.datum.present?