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 %>
Related
I have this problem... I need save my table historic with all the authorizations ids checked, update the column authorization_origin_id and set the column refinancing_id with the refinancing id created. Example: I checked authorizations ids 2 and 3, so I create historic with two lines with authorization_origin_id 2 and 3 and the refinancing id will be 1. My code:
My view this it:
<%= simple_form_for(#refinancing) do |f| %>
<div class="form-inputs">
<%= f.hidden_field :employee_id, value: #employee.first.id %>
<%= f.hidden_field :authorization, value: #authorization %>
<%= f.input :contract_number %>
</div>
<h3>Reserved value</h3>
<table class="table table-condensed table-bordered table-striped">
<thead>
<th>Authorization id</th>
<th>Contract number</th>
</thead>
<% #authorizations.each do |authorization| %>
<tbody>
<tr>
<td><%= authorization.id %></td>
<td><%= authorization.contract_number %></td>
</tr>
</tbody>
<% end %>
</table>
<div class="form-actions">
<%= f.button :submit, "To Reserve" %>
</div>
<% end %>
Controller is this:
def new
if params[:authorization].present?
#selected_ids = params[:authorization][:contract_number]
#authorizations = Authorization.where("contract_number in (?)", #selected_ids)
auth_params = params[:authorization]
auth_params[:contract_number].zip(auth_params[:value_solve].reject(&:blank?)).each do |contract_number, value_solve|
Authorization.where(contract_number: contract_number).update_all(value_solve: value_solve, situation: 2)
end
#authorizations.ids.each do |auth_id|
#historic_refinancing = HistoricRefinancing.create
#historic_refinancing = HistoricRefinancing.update_all(authorization_origin_id: auth_id)
end
end
#employee = Employee.search_cpf(params[:search_employee_by_cpf])
#refinancing = Refinancing.new
end
Actually my table historic is wrong, authorization_origin_id take, just the last (just one ever =/) value checked and refinancing_id is null
The answer is it:
#historic_refinancing = HistoricRefinancing.where(authorization_origin_id: auth_id).update_all(authorization_origin_id: auth_id)
or simply this
#historic_refinancing = HistoricRefinancing.create(authorization_origin_id: auth_id)
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.
I have a table that I am trying to make a bit more streamlined. In my view (this view is already a partial (_recipe_ingredient.html.erb), I have the following code, where rendering the partial is not working:
<tr>
<td><%= recipe_ingredient.ingredient.name %></td>
<td><%= recipe_ingredient.ingredient.weight1*recipe_ingredient.quantity %></td>
<td><%= recipe_ingredient.ingredient.description1 %></td>
<td><%= recipe_ingredient.quantity %></td>
<td><%= render(:partial => 'recipe_ingredients/edit', :locals =>
{:recipe_ingredient=> recipe_ingredient}) %></td>
<td><%= link_to 'Remove', recipe_ingredient, :confirm => 'Remove ingredient from
recipe?', :method => :delete %></td>
</tr>
Previously, I was using link_to to edit the recipe_ingredient as follows (which worked fine), but I would like not to have the user go to another page to edit - instead I want the form to be part of the table:
<td><%= link_to 'Edit Quantity', edit_recipe_ingredient_path(recipe_ingredient) %></td>
The edit partial (which the new non-working code calls) looks like:
<h1>Editing recipe_ingredient</h1>
<%= render 'recipe_ingredients/form', :recipe_ingredient => #recipe_ingredient %>
And the standard form partial looks like:
<%= form_for(#recipe_ingredient) do |recipe_ingredient| %>
<% if #recipe_ingredient.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#recipe_ingredient.errors.count, "error") %> prohibited this
recipe_ingredient from being saved:</h2>
<ul>
<% #recipe_ingredient.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= recipe_ingredient.label :quantity %><br />
<%= recipe_ingredient.number_field :quantity %>
</div>
<div class="actions">
<%= recipe_ingredient.submit %>
</div>
<% end %>
Mainly, I'm confused why it works using link_to, but I can't simply render the partial. The error I'm getting is undefined method `model_name' for NilClass:Class in the first line of the form partial.
I've tried taking the "#" off #recipe_ingredient in the form partial, but that doesn't work either.
Thanks in advance for any help.
just create object with #recipe_ingredient in your action i think its an edit action.
#recipe_ingredient = RecipeIngredient.find_by_id(params[:id)
hope this will work fine.
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}">
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 %>