Ruby on Rails Syntax <% vs <%= [duplicate] - ruby-on-rails

This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 8 years ago.
I am new to ruby and rails altogether. The tutorial I am following doesn't explain the difference between <% and <%= tag. For exmaple:
<% #statuses.each do |status| %>
<tr>
<td><%= status.name %></td>
<td><%= status.content %></td>
<td><%= link_to 'Show', status %></td>
<td><%= link_to 'Edit', edit_status_path(status) %></td>
<td><%= link_to 'Destroy', status, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
The loop opens up with just <% and within it the tags open up with <%= .
So what's the difference?
Thanks

<% %> and <%= %> both execute Ruby code.
<% %> will execute Ruby code, but will not render the return value into html.
<%= %> will execute Ruby code, and will render the return value into html.

Related

Views in ruby on rails

I am learning to code in ruby on rails environment. I created two models cars and products. I want to have a main page which have a link to cars and products. On clicking each of them they should display their own view pages. Below are view pages for cars and users respectively :
app/views/cars/index.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing Cars</h1>
<table>
<thead>
<tr>
<th>Make</th>
<th>Color</th>
<th>Year</th>
<th colspan="24"></th>
</tr>
</thead>
<tbody>
<% #cars.each do |car| %>
<tr>
<td><%= car.make %></td>
<td><%= car.color %></td>
<td><%= car.year %></td>
<td><%= link_to 'Show', car %></td>
<td><%= link_to 'Edit', edit_car_path(car) %></td>
<td><%= link_to 'Destroy', car, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Car', new_car_path %>
<h4>Import that data!</h4>
<%= form_tag import_users_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import CSV" %>
<% end %>
</div>
app/views/users/index.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing Users</h1>
<table>
<thead>
<tr>
<th>User</th>
<th>Steps</th>
<th>Distance</th>
<th>Minutes Exercised</th>
<th>Hours of Sleep</th>
<th>Calories Burned</th>
<th colspan="24"></th>
</tr>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
<td><%= user.user %></td>
<td><%= user.steps %></td>
<td><%= user.distance %></td>
<td><%= user.exercise %></td>
<td><%= user.sleep %></td>
<td><%= user.calories %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New User', new_user_path %>
<div>
<h4>Import that data!</h4>
<%= form_tag import_users_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import CSV" %>
<% end %>
</div>
<%= form_tag import_users_path, multipart: true, class: 'form-inline' do %>
<div class="form-group">
<%= link_to "Export CSV", users_path(format: "csv"), class: 'btn btn-primary' %>
</div>
<% end %>
I don't know how to create single main page and provide links to these views. One example would help me. Thanks in advance guys.
Say suppose you have a main page named home
In your home.html.erb write
<%= link_to "cars" cars_path %>
<%= link_to "users" users_path %>
You probably should use rails scaffold to have a look at the functional pages generated by it at first.
Try this .......
<%=link_to "cars" cars_path(car) %>
<%=link_to "users" users_path(user) %>
This link will send you to show method of the controller (users/cars). Then you need to create a show page in your views/cars/show.html.erb and views/users/show.html.erb then you can display their own view/show pages.
Hope this will work for you.
You need to understand concept of routes in rails.
Run command rake routes in your project folder. You will see mapping like this.
Prefix Verb URI Pattern Controller#Action
companies GET /companies(.:format) companies#index
So according to routes, following link will lead to index action of companies controller.
<%= link_to 'Companies', companies_path%>
As you can observe, in companies_path, companies is prefix shown in routes.
For your specific case, following links will work. In your
home.html.erb
<%=link_to "All Cars", cars_path%>
<%=link_to "All Users", users_path%>
Remember there is comma between "All Users" and users_path. Because link_to is just a method with multiple arguments.
For more info,
http://apidock.com/rails/v4.0.2/ActionView/Helpers/UrlHelper/link_to

Rails Error comparing object with parameter inside view

I have my index.html like this:
<% #alumno_inscriptos.each do |alumno_inscripto| %>
<tr>
<% if (alumno_inscripto.clase_id) == (params[:id]) %>
<td><%= alumno_inscripto.clase_id %><td>
<td><%= params[:id] %><td>
<td><%= buscarNombre(alumno_inscripto.alumno_id).name %> <%= buscarNombre(alumno_inscripto.alumno_id).lastname %> </td>
<td> <%= alumno_inscripto.presencia %></td>
<td> <%= alumno_inscripto.pago %></td>
<td><%= link_to 'Destroy', alumno_inscripto, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% else %>
<td><%= "no va. solo para testear" %><td>
<td><%= alumno_inscripto.clase_id %><td>
<td><%= params[:id] %><td>
<% end %>
</tr>
<% end %>
The problem is that inside the 'if', if the 2 parameters are the same, it always goes to the else part. I leave you an image of what it print. As you can see in the last row the values are the same. Do you have a solution for this?
Thank you!

Can find the bug in SyntaxError ROR

I ran in to this error and I have tried to debug it, but it seems impossible. Im working with Authorization. I'm following One month rails and I'm trapped at "Authorization: Who can? Who can't?"
I'm getting this error SyntaxError in Pins#index, which I can't conclude much form.
I would be really happy if any of you could find the bug!
Link to my github:
https://github.com/SillasPoulsen/Pinteresting
The image clearly says the error
<% #pins.each do |pin| %>
<tr>
<td><%= pin.description %></td>
<td><%= pin.user.email if pin.user %></td>
<td><%= link_to 'Show', pin %></td>
<% if pin.user == current_user %>
<%= link_to 'Edit', edit_pin_path(pin) %>
<%= link_to 'Destroy', pin, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
<% end %> # remove this line
</tr>
<% end %>

How to displaying an specific attributes with a many to many association in rails

I have a two way many-to-many assoication between 3 models: work.rb, category.rb, categorywork.rb
Within the work#index using <%= work.categories %> renders some wonky looking html markup
<% #works.each do |work| %>
<tr>
<td><%= work.name %></td>
<td><%= work.subtitle %></td>
<td><%= work.categories %></td>
<td><%= link_to 'Show', work %></td>
<td><%= link_to 'Edit', edit_work_path(work) %></td>
<td><%= link_to 'Destroy', work, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
I'm trying to target speific attributes of the association like "name".
Unfortunately when using <%= work.categories.name %> it gets weirder with:
How do i target just the name or just the description?
Try this out:
<%= work.categories.pluck(:name) %>

Proper arguments for initializing a model in a view?

My question template lists answers, and lets somone add a new answer for a question.
But I'm not sure where and how to initialize a new answer for this line:
<%= link_to 'New answer', new_question_answer_path(#question, Answer.new) %>
from the context below:
<p>
<b>Body:</b>
<%=h #question.body %>
</p>
<h1>Listing answers</h1>
<table>
<tr>
<th>Body</th>
</tr>
<% #question.answers.each do |answer| %>
<tr>
<td><%=h answer.body %></td>
<td><%= link_to 'Show', answer %></td>
<td><%= link_to 'Edit', edit_answer_path(answer) %></td>
<td><%= link_to 'Destroy', answer, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New answer', new_question_answer_path(#question, Answer.new) %>
<%= link_to 'Edit', edit_question_path(#question) %> |
<%= link_to 'Back', questions_path %>
If I initialize the new Answer like this:
<%= link_to 'New answer', new_question_answer_path(#question, Answer.new) %>
the URL of the next page is garbled with an inspect call to the new Answer.
Try this:
<%= link_to 'New answer', new_question_answer_path(#question) %>

Resources