【问题标题】:Rails Partials and Simple Form AnswersRails 部分和简单形式的答案
【发布时间】:2015-07-05 07:25:26
【问题描述】:

我在 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  %>

我的视图文件夹的数据文件夹中的数据/_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:instart' 来自 /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:in 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 

Projects 表有:

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

有谁知道我做错了什么?

谢谢

【问题讨论】:

    标签: ruby-on-rails partial-views


    【解决方案1】:

    是的...您的代码明确表示范围has_one datum。它没有data - 该方法不存在并且永远不会存在(除非您设置了它)。因此,对于任何给定的范围,您只能使用datum - 这就是所有可用的。这就是你应该使用的。如果你想要不同的行为 - 你需要改变你的设置

    所以这是有效的代码:

    project.scope.datum
    

    这不是:

    project.scope.data
    

    这将始终返回 false(实际上是 nil)

    project.scope.try(:data)
    

    这个有希望返回 nil 以外的东西:

    project.scope.try(:datum)
    

    你可能并不是真的要在这里尝试什么:

    @project.scope.try(:data) == true
    

    以上将永远不会工作。首先,data 不存在于 scope 类中。只有datum 存在——因为那是你在课堂上写的。其次...Rails 的工作方式是在您调用scope.datum 时设置类Datum 的实际实例 - 它永远不会等于true... 它存在或不存在...即它要么存在,要么不存在。

    如果上述代码背后的意图是“如果这个范围有一个数据......然后做X”,那么你可能应该这样写条件:

    @project.scope.datum.present?
    

    【讨论】:

    • 感谢 Taryn,但它有一个数据的原因是数据对于数据来说是单数的。
    • 当我尝试时:
    • 我会试试@project.scope.datum.present?建议
    • 太棒了。塔林,非常感谢。做到了。非常感谢。
    • 非常感谢。我有它们并且我经常提到它们,但我发现很难将它们读入我的问题的背景中。我认为阅读它们需要一层假定的知识(就像我阅读过的关于rails的其他每一本书的问题一样)。尽管如此,我还是会一直回到他们身边。再次非常感谢。
    猜你喜欢
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多