Grouping records with headings in Rails 3 - ruby-on-rails-3

I have an articles model in my Rails 3 application. The article model has a column called categories which is set using a select box in the new form view. (It's a select box because the options should never change and there are only four of them).
The index view code I have is as follows:
<% #articles.category.each do |article| %>
<%= article.category %>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.author %></td>
<td><%= article.category %></td>
<td><%= link_to 'Show', article %></td>
<td><%= link_to 'Destroy', article, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
<% end %>
I have grouped by category in my controller:
#articles = Article.group(:category).order('title ASC')
However, this results in an exception which points to the following line <% #articles.category.each do |article| %>.
What is the tidiest (from within my view code) way of achieving the following:
Category 1
Article 1
Article 2
Category 2
Article 5
Category 3
Article 8
So each article is listed under it's category.

I'd suggest you to use group_by method (Documentation):
# in your controller
articles = Article.order('title ASC')
#grouped_articles = articles.group_by &:category
# in your view
<% #grouped_articles.each do |category, articles| %>
<%= category %>
<% articles.each do |a| %>
<tr>
<td><%= a.title %></td>
<td><%= a.author %></td>
<td><%= link_to 'Show', a %></td>
<td><%= link_to 'Destroy', a, confirm: 'Are you sure?', method: :delete %> </td>
</tr>
<% end %>
<% end %>

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 - Rendering an edit form partial within another partial

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.

How to get selected value in select in rails 3?

I want to know how to get the selected value of <select>. I only know how to populate this.
Here's is my code in index.html.erb. I used this to populate the <select> dropdown menu.
<h1>Trap</h1>
<%= form_for #search do |f| %>
<p>
Employee Code:
<%= f.select(:empcode_contains, #employee.collect {|e| [ e.empcode, e.id ]}) %>
</p>
<p class="button"><%= f.submit "Search" %></p>
<% end %>
<p>
Sort by:
<%= sort_link #search, :empcode %> |
<%= sort_link #search, :date_entry %> |
</p>
<table>
<tr>
<th>Empcode</th>
<th>Date entry</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #traps.each do |trap| %>
<tr>
<td><%= trap.empcode %></td>
<td><%= trap.date_entry %></td>
<td><%= link_to 'Show', trap %></td>
<td><%= link_to 'Edit', edit_trap_path(trap) %></td>
<td><%= link_to 'Destroy', trap, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Trap', new_trap_path %>
And in my controller traps_controller.rb:
def index
#search = Trap.search(params[:search])
#traps = #search.all
#employee = Employee.all
Trap.all.each do |t|
Employee.create(:empcode => t.empcode)
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #traps }
end
end
Pls tell me how to get the selected value if the user selects a value. I don't have any idea.
Try (in the controller) :
value = params[:empcode_contains]
or you could fetch the whole object this way:
search = params[:search]

rails 3 with a ajax created form

I want to create a page which user can modify data on that page.
So I tried to use a ajax call to replace the original data table row into a form.
The new form could be created and replace the row by my code currently. But after edited data in that form and click 'Update', nothing happened. In my console shows something like:
ActionController::RoutingError (No route matches "/projects/5"):
I can't figure out why it didn't work.
Codes are shown as following:
index.html.erb
<tr>
<th>Name</th>
<th></th>
<th></th>
</tr>
<% #projects.each do |project| %>
<tr id="project_<%= project.id %>">
<td><%= link_to project.name, project %></td>
<td><%= link_to 'Edit', edit_project_path(project), :method => :get, :remote => true %></td>
<td><%= link_to 'Destroy', project, :confirm => 'Are you sure?', :method => :delete , :remote => true %></td>
</tr>
<% end %>
edit.js.erb
$('#project_<%= #project.id%>').replaceWith("<%= escape_javascript(render :partial=>'edit') %>");
_edit.html.erb
<tr id="project_<%= #project.id%>">
<%= form_for(#project, :remote => true) do |f| %>
<%= form_authenticity_token %>
<td><%= f.text_field :name %></td>
<td><%= f.submit 'Update' %></td>
<td><%= link_to 'Destroy', #project, :confirm => 'Are you sure?', :method => :delete , :remote => true %></td>
<% end %>
</tr>
Routing errors are usually caused by one of the following:
you don't have the route specified in routes.rb. Run "rake routes" at the command line and see if something like "/projects/:id" shows up. If not you have to add the route by adding a projects resource or otherwise specifying the route manually. Make sure the HTTP method matches the HTTP verb specified in routes.rb.
you have no controller named named ProjectsController or no create method (if your ajax method is posting) or no update method (if its putting).

Getting current_user error when using Ryan Bates' CanCan authorization plugin

The portion of the view that is applicable:
<% #projects.each do |project| %>
<tr>
<td><%= project.name %></td>
<td><%= project.description %></td>
<td><%= link_to 'Show', project %></td>
<% if can? :update, #project %>
<td><%= link_to 'Edit', edit_project_path(project) %></td>
<% end %>
<% if can? :destroy, #project %>
<td><%= link_to 'Destroy', project, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>
</tr>
<% end %>
models/ability.rb
class Ability
include CanCan::Ability
def initialize(designer)
can :read, :all
end
end
This is the error I get:
NameError in Projects#index
undefined local variable or method `current_user' for #<ProjectsController:0x000001016d62d8>
Extracted source (around line #18):
15: <td><%= project.description %></td>
16: <td><%= link_to 'Show', project %></td>
17:
18: <% if can? :update, #project %>
19: <td><%= link_to 'Edit', edit_project_path(project) %></td>
20: <% end %>
21:
Thoughts?
I had errors in my AuthLogic install. Or rather, not errors, but when I installed it I used current_designer (which was the main user I was concentrating on) rather than current_user.
It seems CanCan didn't like that.
So I am now in the process of re-doing all my user models. But so far, it seems to have fixed this issue.