Can find the bug in SyntaxError ROR - ruby-on-rails

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 %>

Related

What is the cleanest way of implementing cancancan on my index.html.erb in Ruby on Rails?

I'm just wondering what everyone recommends doing for using cancan in their .html pages?
Just a little info on what I currently have set up...
My /app/models/ability.rb:
user ||= User.new # guest user (not logged in)
if user.admin?
can :manage, :all
else
can :read, :all
end
I am using an articles_controller.rb and I set up an article model.
I am using the load_and_authorize_resource method in my articles_controller.rb
So onto my index.html.erb.
I have some CRUD options on my page and I only want some links and options such as edit and delete to be visible to the admin user.
In one part of my index.html.erb I have this link which can only be viewed by the admin.
<% if can? :create, #article %>
<%= link_to 'Post New Load Data!', new_article_path %>
<% end %>
^ This is perfect for what I want.
But I have another area on my index.html that confuses me. It goes something like this...
<% if current_user && current_user.admin? %>
<table>
<tr>
<th>Title</th>
</tr>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= link_to 'View', article_path(article) %> |</td>
<td><%= link_to 'Edit', edit_article_path(article) %> |</td>
<td><%= link_to 'Delete', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<p>
</table>
<% else %>
<table>
<tr>
<th>Title</th>
</tr>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= link_to 'View', article_path(article) %> |</td>
</tr>
<% end %>
</table>
<% end %>
So it allows Admin to see the Edit and Delete options.
This is what I want but I am not using CanCan to do this. Is there a
way to simplify this using CanCan? That way I don't have to repeat a lot of
my sections.
I like to stick with devise's current_user.admin? because it reads nicely:
<table>
<tr>
<th>Title</th>
</tr>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= link_to 'View', article_path(article) %> |</td>
<% if current_user.admin? %>
<td><%= link_to 'Edit', edit_article_path(article) %> |</td>
<td><%= link_to 'Delete', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
<% end %>
</tr>
<% end %>
</table>

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!

Rails: route helpers for nested resources

I have nested resources as below:
resources :categories do
resources :products
end
According to the Rails Guides,
You can also use url_for with a set of objects, and Rails will automatically determine which route you want:
<%= link_to 'Ad details', url_for([#magazine, #ad]) %>
In this case, Rails will see that #magazine is a Magazine and #ad is an Ad and will therefore use the magazine_ad_path helper. In helpers like link_to, you can specify just the object in place of the full url_for call:
<%= link_to 'Ad details', [#magazine, #ad] %>
For other actions, you just need to insert the action name as the first element of the array:
<%= link_to 'Edit Ad', [:edit, #magazine, #ad] %>
In my case, I have the following code which was fully functional:
<% #products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td><%= link_to 'Show', category_product_path(product, category_id: product.category_id) %></td>
<td><%= link_to 'Edit', edit_category_product_path(product, category_id: product.category_id) %></td>
<td><%= link_to 'Destroy', category_product_path(product, category_id: product.category_id), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
Obviously it is a little too verbose and I wanted to shorten it using the trick mentioned above from rails guides.
But if I changed the Show and Edit link as follows:
<% #products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td><%= link_to 'Show', [product, product.category_id] %></td>
<td><%= link_to 'Edit', [:edit, product, product.category_id] %></td>
<td><%= link_to 'Destroy', category_product_path(product, category_id: product.category_id), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
Neither of them works any more, and the pages complains the same thing:
NoMethodError in Products#index
Showing /root/Projects/foo/app/views/products/index.html.erb where line #16 raised:
undefined method `persisted?' for 3:Fixnum
What did I miss?
The way Rails is 'automagically' knowing which path to use is by inspecting the objects you pass for their classes, and then looking for a controller whose name matches. So you need to make sure that what you are passing to the link_to helper is the actual model object, and not something like category_id which is just a fixnum and therefore has no associated controller.
<% #products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td><%= link_to 'Show', [product.category, product] %></td>
<td><%= link_to 'Edit', [:edit, product.category, product] %></td>
<td><%= link_to 'Destroy', [product.category, product], method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
I'm guessing the offending line is one of these:
<td><%= link_to 'Show', [product, product.category_id] %></td>
<td><%= link_to 'Edit', [:edit, product, product.category_id] %></td>
The product.category_id is a Fixnum and the routing can't know that a random number is supposed to map to category_id.
Use the previous URLs you had, they're more readable.

Ruby on Rails Syntax <% vs <%= [duplicate]

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.

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) %>

Resources