<% #company.contacts.each do |contactall| %>
<% contactall.each do |contact| %>
<%= contact.name %>
<%= contact.position %>
<%= contact.email%>
<%= contact.telephone%>
<%= contact.source%>
<%= contact.company_id%>
<% end %>
<% end %>
What's wrong with it?
I want to display each contact separately if i do take out <% contactall.each do |contact| %>
and do contact.name i get a bunch of the contacts name's that are associated with the company. help?
I want them all separate.]
The fix:
<% #company.contacts.each do |contact| %>
<b>Name:</b>
<%= contact.name %>
<b>Position:</b>
<%= contact.position %>
<b>Email:</b>
<%= contact.email%>
<b>Telephone:</b>
<%= contact.telephone%>
<b>Source:</b>
<%= contact.source%><br />
<% end %>
What do you mean separate? This?
<% #company.contacts.each do |contact| %>
<div>
<%= contact.name %>
<%= contact.position %>
<%= contact.email%>
<%= contact.telephone%>
<%= contact.source%>
<%= contact.company_id%>
</div>
<% end %>
Related
How can I place an if condition in the HTML, in pseudocode, I'd like to achieve the following:
if #time.status is pending
place edit and delete below the message
else
not just display
end
This is my current code:
<% #value.each do |s| %>
<p><strong>Message:</strong>
<%= s.message %>
</p>
<p><strong>Date</strong>
<%= s.date %>
</p>
<p><strong>Status:</strong>
<%= s.status %>
</p>
<p>
<%= link_to 'Edit', value(), %>
<%= link_to 'Delete', value(), %>
</p>
If you have access to #time and status is an attribute of it, with a value that can be "pending" (thing you didn't add in the question), then you can do:
<% if #time.status == 'pending' %>
<!-- place edit and delete -->
<% else %>
<% #value.each do |s| %>
<p>
<strong>Message:</strong>
<%= s.message %>
</p>
<p>
<strong>Date</strong>
<%= s.date %>
</p>
<p>
<strong>Status:</strong>
<%= s.status %>
</p>
<p>
<%= link_to 'Edit', value(), %>
<%= link_to 'Delete', value(), %>
</p>
<% end %>
<% end %>
It is same as your iteration
<% if #time.status=="PENDING" %>
#your html code for edit in delete
<%end%>
I have the following code in my
new.html.erb
<%= form_tag puppies_path do %>
<% #kennel.each do |puppy| %>
<%= fields_for 'puppies[]', puppy do |p| %>
<div class="field">
<%= p.label :name %><br>
<%= p.text_field :name %>
</div>
<div class="field">
<%= p.label :breed %><br>
<%= p.text_field :breed %>
</div>
<% end %>
<% end %>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>
And puppies[] array variable, which is supposed to post array of objects to controller is posting only single object. Please help to post an array to controller. Thanks in advance!
The usual setup for fields_for is something like this.
<% #kennel.each do |kennel| %>
<%= fields_for :puppies, #kennel.puppies do |p| %>
Yes, I have just found an answer...
In new.html.erb file
<%= form_tag puppies_path do %>
<% 2.times do %>
<%= render 'puppies_group_form' %><br>
<% end %>
<%= submit_tag "Submit" %>
<% end %>
In _puppies_group_form
Name <%= text_field_tag "puppies[][name]" %>
Breed <%= text_field_tag "puppies[][breed]" %>
I'm beginner in Ruby On Rails and I'm reading "Getting Started with Rails" now.
I created models and controllers for Article and Comments, everything ok, I'm able to add comments and they appear on Article view, but besides I see records from database in this format:
[# Comment id: 1, commenter ... updated_at: "2016-05-20 09:26:25"]
Why they appear and how to hide it? Code of Article's view show.html.erb:
<p>
<strong>Title:</strong>
<%= #article.title %>
</p>
<p>
<strong>Text:</strong>
<%= #article.text %>
</p>
<h2>Comments</h2>
<%= #article.comments.each do |comment| %>
<p>
<strong>Commenter:</strong>>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>>
<%= comment.body %>
</p>
<% end %>
<h3>Add a comment</h3>
<%= form_for([#article, #article.comments.build]) do |f| %>
<p>
<%= f.label :commenter %>
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Edit', edit_article_path(#article) %>
<%= link_to 'Back', articles_path %>
Instead of:
<%= #article.comments.each do |comment| %>
It should be:
<% #article.comments.each do |comment| %>
<%= %> used to render something. And you are iterating an object so you need to use <% %>.
Change this:
<% #article.comments.each do |comment| %>
What <%= does is displaying whatever data he's dealing with. If you do <% #article.comments.each do |comment| %> it will deal with the data as well but he won't display anything. And then you can display whatever you want with:
<%= comment.commenter %>
<%= comment.body %>
I have code in file views name: _result.html.erb, this file is rendered from file show.html.erb, both file in one folder
File _result.html.erb
<% if #lesson.answers.at(f.index).is_correct %>
<% if #lesson.answers.at(f.index).id == answer.id %>
<li class="text-success">
<%= f.radio_button :answer_id, answer.id, disabled: true %>
<%= answer.content %>
</li>
<% else %>
<li>
<%= f.radio_button :answer_id, answer.id, disabled: true %>
<%= answer.content %>
</li>
<% end %>
<% else %>
<% if #lesson.answers.at(f.index).id == answer.id %>
<li class="text-danger">
<%= f.radio_button :answer_id, answer.id, disabled: true %>
<%= answer.content %>
</li>
<% else %>
<li>
<%= f.radio_button :answer_id, answer.id, disabled: true %>
<%= answer.content %>
</li>
<% end %>
<% end %>
And I want to improve this code in file _result.html.erb for shorter, help me please!!!
File show.html
<% provide :title, t("start_lesson") %>
<h1><%= #course.name %></h1>
<h2><%= #course.description %></h2>
<h3><%= t "title_question" %></h3>
<% if #lesson.finished.present? %>
<h4>
<%= t "score" %>:
<%= #lesson.results.is_correct_answers.count %> /
<%= #lesson.words.count %>
</h4>
<% end %>
<%= form_for [#course, #lesson] do |f| %>
<%= f.fields_for :results do |builder| %>
<ul class="list-unstyled">
<li>
<%= "#{builder.index + 1}." %>
<%= #words.at(builder.index).content %>
</li>
<ul class="list-unstyled">
<% #words.at(builder.index).answers.each do |answer| %>
<% if #lesson.finished.nil? %>
<li>
<%= answer.content %>
<%= builder.radio_button :answer_id, answer.id %>
<%= builder.hidden_field :word_id, value: answer.word.id %>
</li>
<% else %>
<%= render "result", f: builder, answer: answer %>
<% end %>
<% end %>
</ul>
</ul>
<% end %>
<% if #lesson.finished.nil? %>
<%= f.submit t("submit"), class: "btn btn-primary" %>
<% end %>
<% end %>
This is just a code-rewrite exercise, so here goes:
result.html.erb
<% if #lesson.answers.at(f.index).id == answer.id %>
<% if #lesson.answers.at(f.index).is_correct %>
<li class="text-success">
<% else %>
<li class="text-danger">
<% end %>
<% else %>
<li>
<% end %>
<%= f.radio_button :answer_id, answer.id, disabled: true %>
<%= answer.content %>
</li>
or the shorter (but much harder to read) version:
<li<% if #lesson.answers.at(f.index).id == answer.id %>class="<%= #lesson.answers.at(f.index).is_correct ? "text-success" : "text-danger" %>" <% end%>>
<%= f.radio_button :answer_id, answer.id, disabled: true %>
<%= answer.content %>
</li>
In the first example, the condition was inverted to take advantage of the fact that the else condition in both interior conditions was identical, and then using the conditions only to style the <li> node, since that was the only difference between the 4 blocks.
The second example goes further and does the conditional checks inline with the element, building the class attribute when necessary. This is much harder to read at a glance, but is far more compact.
show.html
<% provide :title, t("start_lesson") %>
<h1><%= #course.name %></h1>
<h2><%= #course.description %></h2>
<h3><%= t "title_question" %></h3>
<% if #lesson.finished.present? %>
<h4><%= "#{t 'score'}: #{#lesson.results.is_correct_answers.count} / #{#lesson.words.count} %></h4>
<% end %>
<%= form_for [#course, #lesson] do |f| %>
<%= f.fields_for :results do |builder| %>
<ul class="list-unstyled">
<li><%= "#{builder.index + 1}.#{#words.at(builder.index).content}" %></li>
<ul class="list-unstyled">
<% #words.at(builder.index).answers.each do |answer| %>
<% if #lesson.finished.nil? %>
<li>
<%= answer.content %>
<%= builder.radio_button :answer_id, answer.id %>
<%= builder.hidden_field :word_id, value: answer.word.id %>
</li>
<% else %>
<%= render "result", f: builder, answer: answer %>
<% end %>
<% end %>
</ul>
</ul>
<% end %>
<% if #lesson.finished.nil? %>
<%= f.submit t("submit"), class: "btn btn-primary" %>
<% end %>
<% end %>
Most of the changes here are just using string interpolation to combine string elements that are otherwise multiple <%= %> blocks.
The larger loop portion that builds the list of answers has some possible issues. For instance, the rendered result (when the lesson is not finished) is missing the <li> and </li> elements, so the answers are simply sitting as a blob of text within the enclosing <ul>. In order to preserve that, in case it was intentional, most of the middle block remained.
However, this is probably not what you actually wanted, so this version fixes that and does a little more reorganization:
<% provide :title, t("start_lesson") %>
<h1><%= #course.name %></h1>
<h2><%= #course.description %></h2>
<h3><%= t "title_question" %></h3>
<% if #lesson.finished.present? %>
<h4><%= "#{t 'score'}: #{#lesson.results.is_correct_answers.count} / #{#lesson.words.count} %></h4>
<% end %>
<%= form_for [#course, #lesson] do |f| %>
<%= f.fields_for :results do |builder| %>
<ul class="list-unstyled">
<li><%= "#{builder.index + 1}.#{#words.at(builder.index).content}" %></li>
<ul class="list-unstyled">
<% #words.at(builder.index).answers.each do |answer| %>
<li>
<% if #lesson.finished.nil? %>
<%= answer.content %>
<%= builder.radio_button :answer_id, answer.id %>
<%= builder.hidden_field :word_id, value: answer.word.id %>
<% else %>
<%= render "result", f: builder, answer: answer %>
<% end %>
</li>
<% end %>
</ul>
</ul>
<% end %>
<% if #lesson.finished.nil? %>
<%= f.submit t("submit"), class: "btn btn-primary" %>
<% end %>
<% end %>
That's about the shortest this can be without collapsing the structure or just removing whitespace. Each remaining element seems to have a purpose and is placed where it should be, without redundancy or over-complication.
What I currently have is:
<%= f.label :attachment_uploader, 'Current Attachments:' %>
<%= f.fields_for :data_files do |attachment| %>
<% if !attachment.object.new_record? %>
<%= attachment.label :attachment_uploader, 'Delete: ' + attachment.object.attachment_uploader_url.split("/").last %>
<%= attachment.check_box :_destroy %>
<% end %>
<% end %>
However, if I don't have any attachments the label is still there. For the sake of aesthetics I'd like it to be hidden unless I have attachments.
I was thinking something akin to:
<%= f.fields_for :data_files do |attachment, index| %>
<% if index == 0 %>
<%= attachment.label :attachment_uploader, 'Current Attachments:' %>
<% end %>
#rest of code
<% end %>
But that doesn't work!
I've read about the f.options[:index] in another post, but I couldn't figure it out.
Add an unless empty? condition before fields_for. #object will be the object for which you created the form
<%= f.label :attachment_uploader, 'Current Attachments:' %>
<% unless #object.data_files.empty? %>
<%= f.fields_for :data_files do |attachment| %>
<% if !attachment.object.new_record? %>
<%= attachment.label :attachment_uploader, 'Delete: ' + attachment.object.attachment_uploader_url.split("/").last %>
<%= attachment.check_box :_destroy %>
<% end %>
<% end %>
<% end %>