Ruby on Rails Rendering tables with dynamic content and partials - ruby-on-rails-3

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}">

Related

How to use Russian Doll caching on same model

I am building a Rails 3.2 web app.
In this app I am using the russian doll style of caching.
I am fetching projects in a table list and records are cached. The problem is that
when I am for example changing the title of a record the title in the list is not updated.
If this were a association I would use :touch => true but it´s the same model.
This is how I start the listing:
<%= render #projects %>
This is the projects.html.erb template:
<% cache projects do %>
<%= render project %>
<% end %>
This is the project.html.erb template:
<% cache project do %>
<tr>
<td><%= link_to project.title.capitalize, admin_project_path(project) %></td>
<td><%= project.reference %></td>
<td><%= show_status project.status %></td>
</tr>
<% end %>
What am I doing wrong?
Thankful for all input.

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.

How do i apply a link to a td element in rails?

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.

Rails 3 i need some help with making a FAQ page

first off ive tried google and cant seem to find an answer i can understand what i would like to do is list my questions from an active record witch i have that much working then make the question its self a link so her is my page so far!
<h1>Listing faqs</h1>
<table>
<% #faqs.each do |faq| %>
<tr><td width="90px"><div id="right">Question : </div></td><td><%= faq.question %></td></tr>
<% end %>
</table>
<br />
<table>
<% #faqs.each do |faq| %>
<tr><td width="90px"><div id="right">Question : </div></td><td><%= faq.question %></td></tr>
<tr><td valign="top"><div id="right">Answer : </div></td><td><%= faq.answer %></td></tr>
<% end %>
</table>
<br />
<%= link_to 'New Faq', new_faq_path %>
im amusingenter code here i can stick something in before faq.question like a link_to or something or something but im not shure what
First of all, in Rails 3 forms should start with <%=, not <%.
<%= #faqs.each do |faq| %>
<tr>
<td width="90px">
<div id="right">Question : </div>
</td>
<td>
<%= link_to(faq.question, faq_path(faq.id) %>
</td>
</tr>
<% end %>

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