Hiding some content on specific layout - Rails - ruby-on-rails

I have two layouts - application.html.erb and application.pdf.erb
They both render the same partial invoices/show.html.erb
invoices/show.html.erb has a table and a part of it looks like this:
<tr>
<td><%= in.name%></td>
<td><%= link_to 'remove', some_path(in), method: :delete%></td>
</tr>
This is working fine but I would like to remove the link_to when I am rendering application.pdf.erb layout. So it looks like this:
<tr>
<td><%= in.name%></td>
<td></td>
</tr>
I have tried to do it by using content_for/yield but I can't have it working.
Any suggestion on how to achieve this?

You can actually get the format in your partial:
<tr>
<td><%= in.name%></td>
<% if controller.request.format == "html" %>
<td><%= link_to 'remove', some_path(in), method: :delete%></td>
<% end %>
</tr>

Related

Update function Ruby

I want this function, if the status of progress so that it changes on the done exactly at those IDs where the status is for the conditions, at the moment I get an error and secondly, if it worked without errors, then it would change everything, but I need if progress is finished and if you don’t, then on progress
I selected three records through the form using a checkbox ...
in the function, I want that if the status of the record from database is "done", then it should change to "progress", if the status is "progress" in the record, then it should be changed to "done". Now I click on "submit_tag" and I get an error and in any case this code will not work the way I want it, I want everything to be conditional.
I am completely in ruby, help please, maybe the problem is in the syntax
no implicit conversion from nil to integer
def update_me
#iteam = Iteam.find(params[:id])
if #iteam[params[:status]] == 'progress'
Iteam.where(params[:id]).update_all(status: 'DONE')
else
Iteam.where(params[:id]).update_all(status: 'progress')
end
end
index view
<%= form_tag update_me_iteams_path, :method =>'put' do %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th>Status</th>
<th></th>
</tr>
<% #iteams.each do |iteam| %>
<tr>
<td><%= iteam.id %></td>
<td><%= iteam.title %></td>
<td><%= iteam.text %></td>
<td><%= iteam.status %></td>
<td><%= link_to 'Show', iteam_path(iteam) %></td>
<td>
<%= check_box_tag "id[]", iteam.id %>
</td>
</tr>
<% end %>
</table>
<%= submit_tag "Edit Checked" %>
<% end %>
Please Use following to find with id:
Iteam.where(id: params[:id])
or you can use the following
Iteam.find(params[:id])

users names not showing in users view

Can anyone tell me why the email addresses are not showing in the users list view below?
<p id="notice"><%= notice %></p>
<h1>Users</h1>
<table>
<thead>
<tr>
<th>Email</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
**<td><%= user.email %></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 %>
I'm not familiar with seeing the two asterisks around the element. I'm thinking that might be messing something up. If you want to bold the user's email you could try something like <td><b><%= user.email %></b></td>.
Are you getting any errors anywhere?
If the asterisks are just there to help us see the line of code, and you say that you see the users (and their emails) in the db, I think it could be one of two other relatively minor things.
Whats your controller look like for this view? Are you passing #users to this view? You could try just dumping #users on the page to make sure that it's available.
Is the field on your User model that holds the email called email?

.html.erb set div doesn't work correctly

I just created a scaffold in rails and list all stories in index page, and _stories.html.erb is partial which was rendered in index.html.erb
I want each story div has a red background for example:
.storyCell{
background-color:red;
height:100px;
}
_stories.html.erb
<tbody>
<% #stories.each do |story| %>
<div class="storyCell">
<tr>
<td><%= story.content %></td>
<td><%= story.finished %></td>
<td><%= link_to 'Show', story %></td>
<td><%= link_to 'Edit', edit_story_path(story) %></td>
<td><%= link_to 'Destroy', story, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
</div>
<% end %>
</tbody>
But the result is the red div all in top of the story model properties. Thank you!
This is invalid html. You can't have a div inside a tbody. Remove your div, and just put the class directly on the table row:
<tr class="storyCell">
What's happening here is that the browser is trying to do the best it can to render the invalid html, and so it pulls the div out (which it nows is not allowed inside the table) and renders it above the table instead.
You can give the class to <tbody> like this:
<tbody class="storyCell">
Here is the w3schools example you can look at: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_tbody
.You can't insert div in table elements as it will render in correct html output.

Rails Links with Model Object Path

I have the following table, and I want the name of my "restaurant" to be clickable and link to the page of that restaurant.
<table class="table table-striped">
<tr>
<th>Name</th>
<th>Adress</th>
<th>City</th>
<th>No. of Recipies</th>
</tr>
<% #city.restaurants.each do |rest| %>
<tr>
<td><%= rest.name %></td>
<td><%= rest.adress %></td>
<td><%= rest.city.name %></td>
<td>5</td>
</tr>
<% end %>
How am I going to add something like rest_path to:
Link text
You could try this :
<%= link_to rest.name, rest %>
If you have defined a resource restaurant in your config/routes.rb, it will target the url given by the helper restaurant_path(rest) (ie /restaurants/id_of_restaurant).
As the restaurant is a child of a city you might want to have an url like /cities/id_of_city/restaurants/id_of_restaurant so you could try the following :
<%= link_to rest.name, city_restaurant_path(#city, rest) %>
Be sure to have this in your config/routes.rb in order to generate the corresponding helpers.
resources :cities do
resources :restaurants
end
Then you will be able to see all the available helpers for your routes using the command 'rake routes' in the terminal.
Okay so I did something like this
<table class="table table-striped">
<tr>
<th>Name</th>
<th>Adress</th>
<th>City</th>
<th>No. of Recipies</th>
</tr>
<% #city.restaurants.each do |rest| %>
<tr>
<td><%= link_to rest.name, rest %></td>
<td><%= rest.adress %></td>
<td><%= rest.city.name %></td>
<td>5</td>
</tr>
<% end %>
</table>
but now the link is very ugly when I hover on it? How do I get rid of that?

Making a table row into a link in Rails

I am trying to make a row in a table link to the edit page. I know the links are being created, because I can print them out. I am close, but am missing something important. What do I change to make the link work properly?
<h1>Scouts</h1>
<p><%= button_to "Add a new Scout", new_scout_path, :method => :get %></p>
<div class="message-board">
<table>
<tr>
<th>Name</th>
<th>Rank</th>
<th>Advancement Date</th>
<th>Age</th>
</tr>
<% #scouts.each do |scout| %>
<tr <% link_to edit_scout_path(scout) %> >
<td><%= scout.name %></td>
<td><%= scout.rank %></td>
<td><%= scout.advancement %></td>
<td><%= scout.age %></td>
</tr>
<% end %>
</table>
</div>
As Robin said, that's invalid HTML. You probably shouldn't do that.
I personally would put an onclick event on the tr using jQuery. The tr element would look like this:
<tr data-link="<%= edit_scout_path(scout) %>">
...
</tr>
And then the associated JavaScript (placed in a file such as app/assets/javascripts/scouts.js) would be something like this:
$("tr[data-link]").click(function() {
window.location = $(this).data("link")
})
This would make all tr elements that have a data-link attribute act as if they were URLs in the most unobtrusive way I can think possible.
I am new on rails and I have the same problem and use the Ryan's advise with some changes that are following -
$("tr").click(function() {
window.location = $(this).data("link")
})
You have to use $(this).
Here is my take to make those links, remote: true
$("tr[data-link]").click(function () {
$.ajax({
url: this.getAttribute('data-link'),
dataType: "script",
type: "GET"
});
event.preventDefault();
});
Simple Solution: add a link into a table cell:
This doesn't answer your question, but it provides a solution to the problem you are likely really after: just add an edit link into a cell, rather than on the table row because having a link on the table row itself may lead to expected results for users. If they are clicking on it, they might not want to be led to an edit link.
Like my grandpa used to say: KISS - Keep It Simple Stupid
<% #scouts.each do |scout| %>
<tr>
<!-- Simply edit the scout -->
<td> <%= link_to edit_scout_path(scout), "Edit Scout" %> </td>
<td><%= scout.name %></td>
<td><%= scout.rank %></td>
<td><%= scout.advancement %></td>
<td><%= scout.age %></td>
</tr>

Resources