I'm using Simple Form with my Rails 4 app.
I have a structure which has models for each of projects, scopes, and data.
Data belongs to scopes, scopes belong to projects.
I have just made a partial view for data which should be displayed in the projects show page if the answer to the question in the scopes form is true.
I have:
An if statement in my projects show view to ask if the answer to the question in project scope form is true, as follows:
<% if @project.scope.try(:data) == true %>
<%= render 'data/particulars'%>
<% end %>
A question in my scope form for:
<%= f.input :data, :as => :boolean, :label => false, :inline_label => true %>
A data/_particulars partial in the data folder of my views folder with:
<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>
When I test the projects show page, with a form that answers the scope question as true (so that data is required to show), nothing is displayed. The google inspector doesn't recognise the data partial view.
In rails console, I tried:
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
The result I get to this query is: ArgumentError: wrong number of arguments (0 for 1..2) from /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' from /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
' from bin/rails:4:in require' from bin/rails:4:in
'
However, if I try:
2.1.1p76 :010 > puts Scope.try(:datum).inspect
nil
=> nil
Taryn's suggestion:
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
Next Steps:
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 table has:
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
Scopes table has:
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"
Datum model:
class Datum < ActiveRecord::Base
belongs_to :scope
end
Scope model:
class Scope < ActiveRecord::Base
belongs_to :project
has_one :datum
accepts_nested_attributes_for :datum
end
Does anyone know what I've done wrong?
Thank you
Right so... your code says definitively that a scope has_one datum
. it does not have data
- that method doesn't exist and never will (unless you set that up). so, for any given scope, you can only ever use datum
- that's all there is available. that's what you should use. if you want different behaviour - you need to change your setup
So this is valid code:
project.scope.datum
this is not:
project.scope.data
this will always return false (actually nil)
project.scope.try(:data)
this one has a hope of returning something other than nil:
project.scope.try(:datum)
you probably don't actually mean what you're trying here:
@project.scope.try(:data) == true
The above will not work ever. Firstly, data
does not exist on the scope
class. only datum
exists - because that's what you wrote in your class. Secondly... the way that Rails works is to set up an actual instance of class Datum
when you call scope.datum
- it won't ever equal true
... it will either exist or not... ie it will either be present or not.
if the intent behind the above code is "if there is one datum for this scope... then do X" then you should probably write the conditional like this:
@project.scope.datum.present?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.