I'm trying to make a report with predefined questions.
I have made questions from the scaffold and filled it.
Now is to assign answer fields per each questions.
[DATA TYPE]
class Report < ActiveRecord::Base
has_many :ansbwer_singles
end
class AnswerSingle < ActiveRecord::Base
belongs_to :report
end
reports/_form.html.erb
<div class="question">
<% QuestionSingle.all.each_with_index do |question, index| %>
<p><%= index+1 %>. <%= question.content %></p>
<p>
<%= f.fields_for :answer_singles do |answer| %>
<%= answer.text_area :content %>
<% end %>
</p>
<% end %>
it shows well but once submit it makes error
1. Question 1
[text area]
2. Question 2
[text area]
[error when submit]
AnswerSingle(#18194030) expected, got Array(#1133380)
I think the reason is using :answer_singles for fields for.
Is there any better code to implement this?
Related
I need to access the same random shuffling across two different loops on the same ERB page.
Context:
I have questions which have five choices of which one is TRUE for choices.is_correct.
My current ERB (below) successfully displays:
1) Grouped by usage
2) All available questions (shuffled)
3) And all five choices (also shuffled)
What I'd like to add is a separate loop (directly after in the same ERB), which gives:
1) Same grouping by usage
2) Same order of available questions (eg, same shuffle order)
3) The correct answer choice (eg, answer "B" = referencing the same shuffling)
Assigning the question or choice shuffling globally would not work, as I need it to be randomized on each page load.
Current ERB (currently shuffles each loop separately)
<%= #exam.name %>
<br />
<% alpha_numbers = ("A".."Z").to_a %>
<% #book_questions_by_usage.each do |usage, question| %>
<h4><%= usage if usage %></h4>
<% question.shuffle.each_with_index do |question, i| %>
<%= i+1 %>: <%= question.name %>
<ol type="A">
<% question.choices.shuffle.each_with_index do |choice, index| %>
<% choice.alpha_order = alpha_numbers[index] %>
<li><%= choice.name %></li>
<% end %><br />
</ol>
<% end %>
<% end %>
<% #book_questions_by_usage.sort.each do |usage, question| %>
<h4><%= usage if usage %></h4>
<% question.shuffle.each_with_index do |question, i| %>
<%= i+1 %>: <%= question.name %>
<ol type="A">
<% question.choices.select { |choice| choice.correct }.shuffle.each_with_index do |choice, index| %>
<% choice.alpha_order = alpha_numbers[index] %>
Correct Answer: <b><%= choice.alpha_order %>. <%= choice.name %></b>
<% end %><br />
</ol>
<% end %>
<% end %>
question.rb
class Question < ApplicationRecord
before_validation :assign_questionable
belongs_to :questionable, polymorphic: true
has_many :choices, :dependent => :destroy
accepts_nested_attributes_for :choices, allow_destroy: true
choice.rb
class Choice < ApplicationRecord
belongs_to :question
I've spent hours looking into possible solutions with no success so far. Any help is much appreciated!
How about you don't shuffle in the view, but in the controller. Save the shuffled array in an instance method and then use it in the view where you need it.
I'm creating an app that has two sets of content that's unrelated.
The first is Questions and Answers (Q&As) where a user can ask a question and the community can answer.
The second is an RSS like feed where an article is posted and links to a 3rd party site.
I'd like to create a 'Feed' so when the user logs in, they see the latest of the Q&As and the latest news all mixed together. I've got it working now where they aren't mixed together.
So two questions, how do i combine the two data sets? And what is the code to make it viewed given it's different content in each table.
Here is my code:
app>models>feed.rb
class Feed < ActiveRecord::Base
validates :name, :url, :description, :source, presence: true
end
app>models>question.rb
class Feed < ActiveRecord::Base
belongs_to :user
has_many :answers, dependent: :destroy, foreign_key: "id"
end
app>controllers>feeds_controller.rb
def index
#feeds = Feed.where("created_at >= ?", Date.today)
#questions = Question.where("created_at >= ?", Date.today)
end
app>views>feeds>index.html.erb
<% #feeds.each do |feed| %>
<h3><%= feed.name %></h3>
<p><%= feed.source %></p>
<p> <%= feed.created_at.strftime("%b %d, %Y") %> </p>
<%= link_to image_tag(feed.image.url(:medium)), feed.url %><br>
<%= truncate(feed.description, length: 50) %><br>
<% end %>
<% #questions.each do |question| %>
<tr>
<td><h1><%= link_to question.question , question_path(question) %></h1></td>
<td><p>Posted by: <%= question.user.name %></p></td>
<td> <p><%= question.created_at.strftime("%b %d, %Y") %> </p></td>
<td><p>Number of answers: <%= question.answers.count %> </p></td>
</tr>
<% end %>
I don't think join works given they have different data. Any suggestions on how to combine feed and questions into one stream with the most recent at the top? (like a facebook feed).
thanks for your help!
I'd suggest this... which is doing array sorting, but I think you don't have a choice with two unrelated object types.
#combined = (#feeds.to_a + #questions.to_a).sort{|a,b| b.created_at <=> a.created_at}
Then in the view...
<% #combined.each do |combined| %>
<%= render combined %>
<% end %>
The beauty of the render is that it will render a partial appropriate to the type of object. If combined s a Feed object the partial used will be feeds/_feed but if it's a Question object it will use the partial questions/_question
I was able to get this working with a few small changes. Rather than rendering the partial I did the following
<% #combined.each do |combined| %>
<% if combined.is_a?(Feed) %>
<h3><%= combined.name %></h3>
<p><%= combined.source %></p
...
<% else %>
<% end %>
Thank you for your help!
I'm learning RoR and I'm trying to understand associations. I've got two models - Company [name] and Note [company_id, notes]. As shown, the Note model has a company_id field to reference the primary key in the Company model.
Within a Notes view, I'm trying to display the Company name but I can't seem to get this to work.
I want to display
(SELECT name FROM Company WHERE Company.id=Note.company_id)
instead of note.company_id in the code below.
company.rb
class Company < ActiveRecord::Base
has_many :notes
end
note.rb:
class Note < ActiveRecord::Base
belongs_to :company
default_scope -> { order(date: :desc) }
end
notes/index.html.erb
....
<% #notes.each do |note| %>
<% if note.active %>
<p>
<%= note.date %>
</br>
<%= note.company_id %> - <%= note.contact %>
</br>
<%= note.notes %>
<!-- <td><%= note.active %></td> -->
</p>
<% end %>
<% end %>
....
To answer your specific question, try:
note.company.name
I would also recommend reading up on Rails partials, particularly how to render a collection: http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
I have looked on the internet and stared at my screen for hours now and hope that you guys can help me out and can stop me from thinking in circles :) I've already found http://railscasts.com/episodes/196-nested-model-form-part-1 and http://railscasts.com/episodes/197-nested-model-form-part-2 but still can't get my mind around it...
Context
I'm trying to develop a CMS where users can create dynamic forms, with their own specific questions, question help text and possible answers (in case of radio buttons/checkboxes)
I am able to create the questions and the forms and to show the question in the viewer, my next step is to allow other users to answer the questions, but can't figure out how to do that.
What I have so far:
Form model:
has_many :custom_questions
has_many :custom_themes
has_many :form_submissions
has_many :answers
accepts_nested_attributes_for :answers
Question model
belongs_to :custom_form
belongs_to :custom_theme
has_many :possible_answers
accepts_nested_attributes_for :possible_answers
has_many :answers
accepts_nested_attributes_for :answers
Answer model
belongs_to :custom_questions
belongs_to :possible_answers
belongs_to :form_submissions
and the viewer which I use to load the questions and input fields:
<div class='content custom_forms clearfix'>
<%= form_for #customForm do |f| %>
<ul>
<%= #customForm.custom_themes.order(:order).each do |theme| %>
<div class='align_element'>
<section class='form_theme_title clearfix'><%= raw theme.theme_title %></section>
<section class="form_theme_description clearfix"><%= raw theme.theme_description.linkify %></section>
</div>
<ul class='selfce-reviews-table clearfix'>
<%= render "reviews/tabHeader" %>
<li class='row clearfix'>
<ul>
<li class="clearfix">
<% theme.custom_questions.order(:order).each do |question| %>
<li class='column selfc'>
</li>
<li class='column rev'>
<div class='align_element'>
<section class='form_question clearfix'>
<%= raw question.question %>
</section>
<section class="form_description clearfix">
<%= raw question.description.linkify %>
</section>
<%= f.fields_for question.answers.build do |builder| %>
<%= builder.hidden_field :custom_question_id, value: question.id %>
<% if question.q_type == "textfield" or question.q_type.nil? %>
<%= builder.text_field :answer, class:'reviews-inputBox clearfix' %>
<% elsif question.q_type == "textarea" %>
<%= builder.text_area :answer, class:'reviews-inputTextarea clearfix tinymce' %>
<%= tinymce %>
<% elsif question.q_type == "checkbox" %>
<% elsif question.q_type == "radio-button" %>
<% end %>
<% end %>
</div>
</li>
<% end %>
</li>
</ul>
<%= f.submit %>
</li>
<% end %>
</ul>
<% end %>
</div>
What I want is to store each answer in the table "answers" in the fields:
- question_id which refers to the unique id of the question
- answer - the value of the input field
As you can see I use #customForm which is the generated form by the administrator. I assume I use it here in the wrong way, because I don't want to update the form but only the answers... so instead it should be something like form_for :answers but that doesnt seem to work..
So my first question:
The way I see it every answer is unique and should therefore be seen as a unique form, hence my nested forms.. However if I use my code right now every input field has the same id: "custom_form_answer_answer". Is there a way I don't have to use nested forms to load the input fields for each question, e.g. by giving each answer a unique id based on the question_id?
I can't hardcode the inputfield name since this can change any time the user creates a new field..
My second question
If there is a way to do that.. how can I save the answers then..
Please let me know if you need any addition information.. and please keep in mind I'm fairly new to rails... so still learning!
Thanks! =-)
Im new to ruby and rails, however I cant figure out why this doesnt work.
I am doing a simple Blog with posts and its comments, everything works fine but I tried to do my own method inside Post model to get the latest comment inside that post.
class Post < ActiveRecord::Base
attr_accessible :content, :title
has_many :comments
def latestComment(id)
post = Post.find(id)
comment = post.comments.last
end
end
and the index.html.erb
<h1>Hello World!</h1>
<h2>Posts</h2>
<% #posts.each do |post| %>
<h3><%= link_to post.title, post %></h3>
<p><%= post.content %></p>
<%= latestComment = post.latestComment(post) %>
<% end %>
<h3>Add new post</h3>
<%= link_to "Add new post", new_post_path %>
This works, it returns some hexa values, so the object exists, however then I now want to get fields from that object like this
<p><%= latestComment.author %></p>
<p><%= latestComment.content %></p>
It fails and the error is
undefined method `author' for nil:NilClass
which is weird and I dont get it why cant I access comments fields..
///comment.rb
class Comment < ActiveRecord::Base
attr_accessible :author, :content, :post_id
belongs_to :post
end
Since you are looping over multiple posts, it's possible that one of them doesn't have any comments, which makes post.comments.last return nil. You can work around this by checking it before trying to render the comment:
class Post < ActiveRecord::Base
def has_comments?
comments.count > 0
end
def last_comment
comments.last
end
end
Then, on the view:
<% #posts.each do |post| %>
<h3><%= link_to post.title, post %></h3>
<p><%= post.content %></p>
<% if post.has_comments? %>
<p><%= post.last_comment.author %></p>
<p><%= post.last_comment.content %></p>
<% end %>
<% end %>