How do i apply a link to a td element in rails? - ruby-on-rails-3

I have the following:
<td>
<%= link_to simple_format(h(post.text)), post %>
<%= time_ago_in_words(post.created_at) %>
</td>
In rails, how do I make the entire td link to the post?

If you want to make all of the text in the cell clickable, you could do this:
<td>
<%= link_to post do %>
<%= simple_format(h(post.text)) %>
<%= time_ago_in_words(post.created_at) %>
<% end %>
</td>
which would wrap an a tag around both of your text bits.
If you want to make the entire td background clickable, you can't just wrap the td with an a tag since HTML doesn't allow an a as a child of a tr, and behavior is likely to be inconsistent across browsers. Rails would gladly do it for you (just put the td inside the link_to block), but it probably wouldn't behave the way you want it to. You'd need different markup.

I can report that I have used #Ribena's suggestion above based on #Rob Davis's answer and have created a div inside that makes (at least most of) the space in a <td> element.
<td>
<%= link_to user_url user.id do %>
<div id="clickable">
<%= user.name+' == '+user.current_score.to_s %>
</div>
<% end %>
</td>
makes the bulk of the white space clickable.

Related

css: how to display two products in one row using each loop (rails)

Need some help in css.
i have an array of products and m displaying it using each loop. I wanna show two products in each row. do you know how to do it? currently i am using 50% width of div so two products will come in div.
Is there any other way?
Here is code:
<div id="product_list" >
<% if #products.size <= 0 %>
<h1/>No products found</h1>
<% else %>
<% #products.each do |p| %>
<div class="products">
<div><%= image_tag p.photos.first.avatar.url(:big) if p.photos.size > 0 %>
</div>
</div>
<% end %>
<% end %>
</div>
div product_list is main div
div products is 50%.
So it displays 2 div under main.
But this behaves weird when filters based on category etc. Is there any good way for this?
You can use
I many times handle such situation like this, try if it works for you.
pseudo code:
<div id="product_list" >
<% if #products.size <= 0 %>
<h1/>No products found</h1>
<% else %>
<ul><li class="products">
<% #products.each do |p| %>
<%= (count%2==0):'</li><li>':''; %>
<div><%= image_tag p.photos.first.avatar.url(:big) if p.photos.size > 0 %>
</div>
<% end %>
</li></ul>
<% end %>
</div>

Rails div for each block result

I have a Rails 3 app where I'm using a block to loop through calls as below:
<div class="blah">
<% #call.each do |call| %>
<%= link_to call.incident_number, call%>
<% end %>
</div>
I want to style the DIV to where it has rounded corners and is a certain size. That's not a problem. But what I've noticed is for each result of the block it continues in the same DIV. I want each result to have it's own div so the results appear separated on the page.
How can I do this using DIV where each result of the block appears in its own DIV?
Just wrap your link_to method in a div content_tag.
<% #call.each do |call| %>
<%= content_tag :div, :class => "some_class" do %>
<%= link_to call.incident_number, call %>
<% end %>
<% end %>

Rails logs me out when I run a query

I have a site built using the railstutorial as a template. I have added a search controller to allow me to perform site searches and redirect the user to a search view. When I go to the path '/search' it is as expected (no results) but if I actually use the search input box I get logged out and have to log back in. What would be causing this?
My form:
<form action="/search" method="POST" class="navbar-search pull-right">
<input name="query" type="text" class="search-query" placeholder="Search">
</form>
My search controller:
class SearchController < ApplicationController
def index
unless params[:query].nil?
#results = ThinkingSphinx.search params[:query]
else
#results = []
end
end
end
My view:
<% unless #results.empty? %>
<table class="table">
<% #results.each do |result| %>
<tr>
<% if result.class.name == "Event" %>
<td><%= link_to result.name, organisation_event_path(result.organisation, result.slug) %></td>
<td><%= result.summary %></td>
<% end %>
</tr>
<% end %>
</table>
<% else %>
<p>No results found.</p>
<% end %>
My route:
match '/search', to: 'search#index'
It's related to CSRF issues. You can read the details here, or this SO question/answer.

Ruby on Rails Rendering tables with dynamic content and partials

I've used this site plenty of times to find my answer but im am unfortunately stuck now and need some help. On my main content page I am working on im running into an issue with creating a tables using CSS and div ids.
Overview
The table is compromised of 2 rows and 6 columns and then spilt into 12 boxes which each box 200px X 200px. Now the content of each box contains a new item (#books) which is generated from a separate controller(used the built in generate scaffold command). I am also using pagination to limit the items on the page (12) so older items just roll on back.
Issue
I am able to populate each of the 12 boxes with new content by using the following code on my controller and view
controller
def content
#books= Book.paginate(:page => params[:page], :per_page => 12)
respond_to do |format|
format.html # content.html.erb
end
view {content.html.erb}
<% #books.each do |book| %>
<div id="column1">
<tr>
<td><%= book.name %></td>
<td><%= book.title %></td>
<td><%= book.content %></td>
<td><%= link_to 'Show', book %></td>
</tr>
</div>
<% end %>
<br />
<div id="paginate">
<%= will_paginate(#books,
) %)
</div>
</div>
What happens when I generate the view is that everything is rendering perfectly but all of my books are using the same 'column 1'
<div id="column1">
<tr>
<td>Title</td>
<td>Name</td>
<td>Content</td>
<td>Show</td>
</tr>
</div>
<div id="column1">
<tr>
<td>Title </td>
<td>Name</td>
<td>Content</td>
<td>Show</td>
</tr>
</div>
I would like for each book to have its own column {2, 3,4,...12} so i can target each individually if i need to using jquery. The other problem is that i would like to use render partial for everything in the but when i use the render partial it places all the items within the 1 column {relisting of Book.all }so it just repeats all the contained information. I have a feeling im suppose to use another method or edit the current one to complete this but I'm unsure what goes were. Thanks!
greenz
Update
Hey Mike i really appreciate your effort in trying to explain to a newby whats going on. I basically went back to step one a applied each piece of code to see where it is going bonkers at. You are doing a wonderful job explaining the issue yet i can't grasp whats going on when rendering the code because the ruby doc's for rendering and layout are pretty basic and dont have any examples where i can see how div_for tag effects the rendering process (I'm a visual person). I wanted to show you the new code but comments don't allow it so i opened an answer :(
The first edit was removing the
<% #books.each do |book| %>
and replacing with
<table>
render #books
</table>
so now my code looks like this {content.html.erb}
<div id="maincontent">
<table>
render #books
</table>
<div id ="column1">
<tr>
<td><%= book.name %></td>
<td><%= book.title %></td>
<td><%= book.content %></td>
<td><%= link_to 'Show', book %></td>
</tr>
</div>
<% end %>
<br />
<div id="paginate">
<%= will_paginate(#books,
) %>
</div>
</div>
When this is applied i receive an unexpected end error when rendering the view pointing to the last line of my code.(When adding the end tag to the end it just threw another error) So i then changed the render page from _bookcontent.html.erb to _book.erb and inserted the partial between the column1 div as shown.
<div id ="column1">
<%= render :partial => "book" %>
</div>
<% end %>
I understand what this tag is now doing it telling my partial that for every "book" render the latest book id from the database. So now the render #books statement is keeping track of the books id for every render page i insert. (I could be mistaken but this is how i perceive it). So now onto the infamous
_book.erb that this page is rendering here is my code.
<% div_for(book) do %>
<tr>
<td><%= book.name %></td>
<td><%= book.title %></td>
<td><%= book.content %></td>
<td><%= link_to 'Show', book %></td>
</tr>
<% end %>
From my reading on the <% div_for(book) do %> is wrapping a unique div id around each book such as
<div id="book_123"> Book.name / Book.Title </div>
This div is now contained inside my column1 div ID from before which has all my styling for my column/boxes. From the lack of information on the div_for tag I am still running into errors and unexpected ends in my code. (Trying to debug the _book.erb with ruby -w doesn't work so i dont know how to use the end tags). These are the take aways i am trying to correct for my sanity.
Should I remove the <div id="column1"> from my content page and apply a new div id / class elsewhere on my content page?
Where do i insert <% end %> tags one in the partial and in my content.html.erb before my pagination? It seems as though when i have both the renderer complains about unexpected ends at the end of each page.
When substituting your code in place of mines i receive the above unexpected end but this isn't due to you im definitely missing a component in my code.
I could be approaching this the wrong way from a layout perceptive in the first place but my main goal is to have 2 rows of 6 boxes/columns w/ each box containing a different book. Then using pagination to scroll back to the earlier books and apply the same layout which it does fine now but i am using the same column id for each book and need to target all the boxes separately.
If I understand you correctly, this is just a case of it doing what you said, not what you meant.
Whatever controller has the content method should look like this:
def content
#books = Book.paginate :page => params[:page], :per_page => 50
end
Your view should look like this(old syntax but still should work fine).
content.html.erb:
<div id="maincontent">
<table>
<%= render :partial => 'book', :collection => #books %>
</table>
<br />
<%= will_paginate #books %>
</div>
Or try the new syntax. The new syntax is weird and I haven't tried it personally but I think this should work. So here is the alternate, more Rails 3 version of content.html.erb:
<div id="maincontent">
<table>
<%= render #books %>
</table>
<br />
<%= will_paginate #books %>
</div>
I think it chooses the partial based on the class name and whether it is enumerable.
The _book.html.erb partial:
<%= div_for(book) do %>
<tr>
<td><%= book.name %></td>
<td><%= book.title %></td>
<td><%= book.content %></td>
<td><%= link_to 'Show', video %></td>
</tr>
<% end %>
Div_for is the secret sauce.
The only end that is required is the one inside the partial to close the block opened when we called do after the div_for.
Now i am getting unexpected end errors when i render the page here is my view
view = content.html.erb
<div id="maincontent">
render #books
<div id="column1">
<tr>
<%= render :partial => "bookcontent" %>
</tr>
</div>
<% end %>
<br />
<div id="paginate">
<%= will_paginate(#books,
) %>
</div>
</div>
Here is my render partial = _bookcontent.html.erb
<% div_for(book) %>
<tr>
<h3><%= book.name %></h3>
<h3><%= book.title %></h3>
<h3><%= book.content %></h3>
<h3><%= link_to 'Show', book %></h3>
</tr>
<% end %>
I am also clueless as to if i should be using a div id or class. By default i always use just an id.
If I understand correctly, your code is working fine but you want to have:
column1
column2
column3
...
for each book. If that is the case, why don't you simply replace:
<div id="column1">
with
<div id="column#{#book.id}">
or use a counter
<%
book_counter=0
#books.each do |book|
book_counter = book_counter+1
%>
<div id="column#{book_counter.to_s}">

My application is showing EXTRA info...which is NOT needed

I have an action called 'list' and is defined like
def list
#subjects = Subject.order("subjects.position ASC")
end
And my list view looks like
<div>
<h2>Subjects</h2>
<%= link_to("Add new subject",{:action=>'new'},:class=>'action new')%>
<table class="listing" summary="Subject list">
<tr class="header">
<th> </th>
<th>Subject</th>
<th>Visible</th>
<th>Pages</th>
<th>Actions</th>
</tr>
<%= #subjects.each do |subject| %>
<tr>
<td><%=subject.position%></td>
<td><%=subject.name%></td>
<td class="center"><%=subject.visible ? 'Yes':'No' %></td>
<td class="center"><%=subject.pages.size%></td>
<td class="actions">
<%=link_to "Show",{:action=>'show',:id=>subject.id},:class=>'action show'%>
<%=link_to "Edit",{:action=>'edit',:id=>subject.id},:class=>'action edit'%>
<%=link_to "Delete",{:action=>'delete',:id=>subject.id},:class=>'action delete'%>
</td>
</tr>
<%end%>
</table>
</div>
So the problem is that besides getting an organized list of all the subjects nearby my link "Add subject" the page is printing all the information that #subjetcs brings from the controller.
Which of course is not a very good idea...I can't figure out why is that happening
Your problem is the line <%= #subjects.each do |subject| %>. Because you've got <%=, the return value of that line - which is #subjects - should be written into the document.
Use <% #subjects.each do |subject| %> instead to suppress the output.
<%= #subjects.each do |subject| %>
should be:
<% #subjects.each do |subject| %>
The equals sign tells ERB that the code should output something. You want to omit that in the iterator.
Remove the equals sign from the <%= #subjects.each do |subject| %> line.
ERB outputs everything within <%= ... %> tags to the page. So in this case, the return value of #subjects.each is written to the page. Thus, write your loop like this instead:
<% #subjects.each do |subject| %>
...
<% end %>
This applies to all statements that do stuff rather than output stuff to the page. For example, you would write an if statement as:
<% if some_condition %>
...
<% else %>
...
<% end %>
rather than as:
<%= if some_condition %>
...
<% else %>
...
<% end %>