Rails 3 - How to comment in a View? - ruby-on-rails

What is the Rails 3 way to comment out One Line or Multiple lines of code in a View? And so it doesn't show up in the HTML Source

To comment out a single line ob ruby code use
<%# code %>
or for multiple lines
<%
=begin
your code
=end
%>
EDIT:
Here a example to comment out a loop in an view.
The =begin and =end must stand directly at the beginning of the line.
There couldn't be any spaces or tabs.
<h1>Listing posts</h1>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th></th>
<th></th>
<th></th>
</tr>
<%
=begin
%>
<%#posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
<%
=end
%>
</table>

Rails 3 line comment within a view:
The f.label :name line has been commented out:
<%= form_for(#usr) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%#= f.label :name %>
<%= f.text_field :name %>
<%= f.submit "Save changes" %>
<% end %>
Because this is a view, the # must be within <% and %>.
Rails 3 multi-line comment within a view:
Begin multi-line comment:
<%
=begin
%>
End multi-line comment:
<%
=end
%>
Below, the entire form_for block has been commented out:
<%
=begin
%>
<%= form_for(#usr) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit "Save changes" %>
<% end %>
<%
=end
%>
Do take note, for the multi-line comment tags to work, there can be no spaces or tabs in front of =begin or =end. They need to be at the very beginning of a line, or they will not work.

Here's what I do to hide comments from HTML (...whatever, it works!)
in your helpers/application.rb file:
def comment
# use this keyword in the views, to comment-out stuff...
end
in your view:
<h3><% comment = "my Rails-y something something here will not be visible to HTML.
I don't know why I have to comment stuff out in this manner.
It's probably not the 'Rails Way' but ...it works for me.
It would be nice if Rails 3 had some kind of comment-out feature like <%## %> or <%-- --%> or something like that...
Ah, well...
At least they won't be able to 'View Source' and read this comment! ;]
" %></h3>
what 'View Source' shows:
<h3></h3>
C-YA!

Some ways to comment code
<%
=begin
%>
RUBY CODE GOES HERE
<%
=end
%>
<% if false %>
RUBY CODE GOES HERE
<% end %>
<%# RUBY CODE%>
<%#= RUBY CODE%>
<!--
HTML CODE
-->
For RUBY code in.rb files like models/controllers
def xyz
=begin
blah blah code
=end
end
For JS etc
/*
Some code
*/

which "blocks" do you mean?
html? then you can use
ruby code? <%# code %>

Related

Rails: undefined method for nil:NilClass

This might be a simple question, but I can't figure it out.
Model Question has many Model Answer, and they both belong to Model User.
Here is part of index.erb.html:
<tbody>
<% #questions.each do |question| %>
<tr>
<td><%= question.content %></td>
<%= time_ago_in_words(question.created_at) %> ago by <%= question.user.email %>
<td><%= link_to 'Show', question %></td>
<td><%= link_to 'Edit', edit_question_path(question) %></td>
<td><%= link_to 'Destroy', question, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% if question.answers %>
<h4>Answers</h4>
<% #question.answers.each do |answer| %>
<p>
<%= answer.content %>
</p>
<% end %>
<% end %>
<br>
<p>Add Answers</p>ww
<%= form_for([question,question.answers.build]) do |f| %>
<%= f.text_area :content %>
<%= submit_tag "Add" %>
<br>
<% end %>
</tr>
<% end %>
</tbody>
And I used seed.rb to insert data.
Here is seed.rb
User.destroy_all
Question.destroy_all
Answer.destroy_all
user = User.create(email: "123#gmail.com", password: "2wsx1qaz")
q = Question.create(content: "Q1", user: user)
#q2 = Question.create(content: "Q2", user: user)
Answer.create(content: "answer1", question: q, user: user)
Answer.create(content: "answer2", question: q, user: user)
But it threw this error
I also put this project on github
Update
After above error, the render result is weird.
It should show Question, and then check there are any answer for that question. If there are, shows answers.
But, the result on my browser is like that
But, I expected it should be like this
You don't have #question defined, so it's nil and that's why you got: undefined method answers for nil:NilClass when you called this:
#question.answers
You actually have question defined. So, use that :-)
Change this:
<% #question.answers.each do |answer| %>
To:
<% question.answers.each do |answer| %>
I think this line is incorrect
<%= time_ago_in_words(question.created_at) %> ago by <%= question.user.email %>
which is outside table data so put this in your <td> clause. an your answers are also not in any table data so put it in proper format of table.
for more info to render table in html #table. so it is disturbed your UI part.

Form with route direction does not work

I have a form set up with the following code:
<h2>Add collaborators to the wiki </h2>
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Give Access</th>
</tr>
<tr>
<%= form_for (#collaboration) do |f| %>
<% #users.each do |user| %>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td> <%= f.collection_select :user_id, User.all, :id, :name, prompt: true %> </td>
</tr>
<%= f.submit %>
<% end %>
</table>
<%= f.submit %>
<% end %>
And my routes are set up like this:
resources :wikis do
resources :collaborations
end
And in my controller I defined my variables like this:
def new
#wiki = Wiki.find(params[:wiki_id])
#collaboration = #wiki.collaborations.new
end
But when I visit the page clicking on a link I created
<%= link_to 'Add Collaborator', new_wiki_collaboration_path(#wiki) %>
I still get this error:
undefined method `collaborations_path' for #<#<Class:0x007f8b6a5a8a00>:0x007f8b67820c90>
Any thoughts on what goes wrong here?
Your form here just goes to collaboration_path which you've not defined.
<%= form_for (#collaboration) do |f| %>
<% end %>
You need to include the wiki
<%= form_for ([#wiki, #collaboration]) do |f| %>
<% end %>

Rendering a partial inside a block

I'm new to rails. I have this block in my view afrikaans.html.erb
<% #afrikaans.each do |course| %>
<div class="course">
<h3 class="course-name"><%= link_to course.name, course.url %></h3>
<% if I18n.locale == I18n.default_locale %>
<p class="course-description_en"><%= course.description_en %></p>
<% else %>
<p class="course-description_se"><%= course.description_se %></p>
<% end %>
<% if course.youtube_url.blank? == false %>
<p><%= raw ApplicationHelper.youtube_embed(course.youtube_url) %></p>
<% end %>
<% if course.language_id == 1 %>
<p> <%= image_tag("eng.png", :alt => "England", :class =>"round") %></p>
<% else %>
<p> <%= image_tag("swe.png", :alt => "Sweden", :class =>"round") %></p>
<% end %>
<% if ApplicationHelper.active_link?(course.url) == false %>
<td><%= I18n.t('home.broken_link') %></td>
<% end %>
<p><%= course.nbr_of_votes %> <%= I18n.t('home.votes') %></p>
</tr>
<% end %>
I also have another file swahili.html.erb with the same structure. I wanted to implement something like this
afrikaans.html.erb
<% #afrikaans.each do |course| %>
<%= render 'shared/partial' %>
<% end %>
So that I can also have
swahili.html.erb
<% #swahili.each do |course| %>
<%= render 'shared/partial' %>
<% end %>
The partial will contain the part of the block. I've tried this but it's not working. My question is this even possible in rails and if so what could be the problem. What options do I have if it isn't possible so that I can avoid the repetition since the two files have the same structure?
Update. This One worked out for me. I only needed to add :course => course on the block so that my views becomes something like
<% #afrikaans.each do |course| %>
<%= render 'shared/course_body', :course => course %>
<% end %>
Of course I've not named my partial "partial". This was just a matter of asking. Thanks to one #Alexander Panasyuk's answer.
Just create shared directory within your app/views path. And create file _partial.html.erb inside shared:
<div class="course">
<h3 class="course-name"><%= link_to course.name, course.url %></h3>
<% if I18n.locale == I18n.default_locale %>
<p class="course-description_en"><%= course.description_en %></p>
<% else %>
<p class="course-description_se"><%= course.description_se %></p>
<% end %>
<% if course.youtube_url.blank? == false %>
<p><%= raw ApplicationHelper.youtube_embed(course.youtube_url) %></p>
<% end %>
<% if course.language_id == 1 %>
<p> <%= image_tag("eng.png", :alt => "England", :class =>"round") %></p>
<% else %>
<p> <%= image_tag("swe.png", :alt => "Sweden", :class =>"round") %></p>
<% end %>
<% if ApplicationHelper.active_link?(course.url) == false %>
<td><%= I18n.t('home.broken_link') %></td>
<% end %>
<p><%= course.nbr_of_votes %> <%= I18n.t('home.votes') %></p>
</tr>
Then render your partial in afrikaans.html.erb like that:
<% #afrikaans.each do |course| %>
<%= render 'shared/partial', :course => course %>
<% end %>
or in swahili.html.erb:
<% #swahili.each do |course| %>
<%= render 'shared/partial', :course => course %>
<% end %>
It is most definitely possible, and usually a good idea.
In the future it would be nice if you could post the actual results and/or error messages you get, which would help a lot when trying to help you.
That said, I'm guessing you need to pass the course variable to your partial. Change
<%= render 'shared/partial' %>
to
<%= render 'shared/partial', :course => course %>
Partials do not have access to local variables in other partials. In that sense you can think of each partial as separate methods on the same object instance.
<%= render 'shared/partial', locale: 'swahili', course: course %>
You will have local vars 'locale' and 'course' in your partial set to 'swahili' and #course, respectively. Also, I'd advise to name your partials something more meaningful, like 'course'.

Get validation errors on a partial's form

So I have a form in my Rails app that's on a _form.html.erb.
I'm displaying this partial on every page of my site, so it doesn't really have any model logic behind it, and the User created form it is made on the fly in the view:
<%= form_for User.new, :validate => true do |f| %>
<table>
<tr>
<td><%= f.label :invitation_token, "Invite Token" %></td>
<td><%= f.text_field :invitation_token, :placeholder => "From your inbox…".html_safe %></td>
</tr>
<tr>
<td colspan="2"><br><%= f.submit "Sign Up" %></td>
</tr>
</table>
<% end %>
I have some validations in my User model, but how would I display those validation errors in my view after reload?
I've tried User.new.errors.inspect but the messages parameter is always empty, even if there's a validation error.
There are a few ways to do this. Since you will likely want to render these error messages in many places, create a shared folder in your views app/views/shared/_error_messages.html.erb and add this partial:
<% if object.errors.any? %>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
<% end %>
Now in your view add the code to render the error messages in you form just below form for
<%= render 'shared/error_messages', object: f.object %>

Rails 3.1 multiple objects on one form

I've checked out a lot of questions for putting multiple objects on one form, but they seem to be out of date.
I have a bunch of user objects:
def index
#users = User.all
#user = current_user
end
that I need to edit in a form. They all have roles which will be edited on the form. The form is rendered in a partial, and nothing shows up, just 'admin form' plaintext.
users/_admin.html.erb
admin form
<% form_for "user[]", :url => users_path do |f| %>
<ul>
<li class="layout">
<div class="header"><h2>Users</h2></div>
<table>
<thead>
...
</thead>
<tbody>
<% #users.each do |user| %>
<% puts "USER #{user}" %>
<tr>
<td><%= f.check_box(:editor) %></td>
<td><%= f.check_box(:admin) %></td>
<td><%= user.first_name %> <%= user.last_name %></td>
<td><%= user.email %></td>
</tr>
<% end %>
</tbody>
</table>
</li>
</ul>
<%= submit_tag "Save"%>
<% end %>
Just the plaintext is rendered, but no form. Any ideas on how to fix it? I've tried the suggestions in these posts: one two three, but they're out of date.
Thank you.
You need to use <%= .. %> in Rails 3.1:
<%= form_for "user[]", :url => users_path do |f| %>

Resources