Alias Route to Controller - ruby-on-rails-3

I have a model called PointOfContact I created with a scaffold; so it should have some semblance of correctness about it.
Basically, I want an alias for my routes. Instead of going to '/point_of_contacts', I want '/pocs' and I don't want '/point_of_contacts' to be a valid route.
I have tried this:
resources :pocs, :controller => "point_of_contacts"
That works to create the '/pocs' route. However, now I am unsure as to how my Views should be written.
Specifically:
<% #point_of_contacts.each do |point_of_contact| %>
<tr>
<td><%= point_of_contact.first %></td>
<td><%= point_of_contact.last %></td>
<td><%= point_of_contact.title %></td>
<td><%= point_of_contact.phone %></td>
<td><%= point_of_contact.email %></td>
<td><%= link_to 'Show', point_of_contact %></td>
</tr>
<% end %>
That code creates this exception:
No route matches {:action=>"show", :controller=>"point_of_contacts", :id=>#<PointOfContact id: 1, system_id: nil, first: "Tester", last: "Test", title: "", phone: "", email: "", created_at: "2011-03-10 20:03:21", updated_at: "2011-03-10 20:03:21">}

Try resources :point_of_contact, :path => "/pocs"

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.

Grouping records with headings in 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 %>

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.