How to modify a link? - ruby-on-rails

I need to modify this link to go to
channels/params[:channel_id]/messages
here is the current link
<%= link_to pluralize(#channel.messages.size, 'message') %>
result of rake routes
{:action=>"index", :controller=>"messages"} POST /channels/:channel_id/messages(.:format)
{:action=>"create", :controller=>"messages"}new_channel_message GET /channels/:channel_id/messages/new(.:format)
{:action=>"new", :controller=>"messages"} edit_channel_message GET /channels/:channel_id/messages/:id/edit(.:format)
{:action=>"edit", :controller=>"messages"}channel_message GET /channels/:channel_id/messages/:id(.:format)
{:action=>"show", :controller=>"messages"} PUT /channels/:channel_id/messages/:id(.:format)
{:action=>"update", :controller=>"messages"} DELETE /channels/:channel_id/messages/:id(.:format)
{:action=>"destroy", :controller=>"messages"} channels GET /channels(.:format)
{:action=>"index", :controller=>"channels"} POST /channels(.:format)
{:action=>"create", :controller=>"channels"} new_channel GET /channels/new(.:format)
{:action=>"new", :controller=>"channels"} edit_channel GET /channels/:id/edit(.:format)
{:action=>"edit", :controller=>"channels"}channel GET /channels/:id(.:format)
{:action=>"show", :controller=>"channels"}PUT /channels/:id(.:format)
{:action=>"update", :controller=>"channels"} DELETE /channels/:id(.:format)

If you would have used nested routes and if you redirect that link to index action in messages controller, then you can define your link like this:
<%= link_to pluralize(#channel.messages.size, 'message'), channel_messages_path(#channel) %>

You don't really need to "modify" it, since it most likely never worked anyway :)
Search for the path macro for your route, using:
rake routes
Then you can use the correct macro, which most likely is something like channel_message. You'll reach something like this:
<%= link_to pluralize(#channel.messages.size, 'message'), channel_message_path(#channel) %>

Related

Non standard rails routes in used in helper

In my routes.rb there is
resources :clients_assessments do
member do
get :medical_edit
get :mental_health_edit
get :personal_edit
end
collection do
end
end
Which gives routes
medical_edit_clients_assessment GET /clients_assessments/:id/medical_edit(.:format) {:action=>"medical_edit", :controller=>"clients_assessments"}
mental_health_edit_clients_assessment GET /clients_assessments/:id/mental_health_edit(.:format) {:action=>"mental_health_edit", :controller=>"clients_assessments"}
personal_edit_clients_assessment GET /clients_assessments/:id/personal_edit(.:format) {:action=>"personal_edit", :controller=>"clients_assessments"}
clients_assessments GET /clients_assessments(.:format) {:action=>"index", :controller=>"clients_assessments"}
POST /clients_assessments(.:format) {:action=>"create", :controller=>"clients_assessments"}
new_clients_assessment GET /clients_assessments/new(.:format) {:action=>"new", :controller=>"clients_assessments"}
edit_clients_assessment GET /clients_assessments/:id/edit(.:format) {:action=>"edit", :controller=>"clients_assessments"}
clients_assessment GET /clients_assessments/:id(.:format) {:action=>"show", :controller=>"clients_assessments"}
PUT /clients_assessments/:id(.:format) {:action=>"update", :controller=>"clients_assessments"}
DELETE /clients_assessments/:id(.:format) {:action=>"destroy", :controller=>"clients_assessments"}
When I try to use them in a helper file like
route = medical_edit_clients_assessment_path(id)
An error message is generated
No route matches {:action=>"medical_edit", :controller=>"clients_assessments"}
I have to change it to
route = "/clients_assessment/#{able_id}/medical_edit"
to get around the error message. What is funny though that a route like
route = (able_id.nil? ? new_client_path : edit_client_path(able_id))
works just fine.
I know someone one will ask for it so here is the entire routes.rb and the result of the rake routes
https://gist.github.com/3074287
Thanks, Russ
This might happened because id is nil

Rails: Routing without plurals gives strange helpers

I am getting a strange named helpers with this setup:
In config/routes.rb I have:
Qtl::Application.routes.draw do
resources :qtl_table do
collection do
get 'search'
end
end
...
end
rake routes outputs this:
search_qtl_table_index GET /qtl_table/search(.:format) {:action=>"search", :controller=>"qtl_table"}
qtl_table_index GET /qtl_table(.:format) {:action=>"index", :controller=>"qtl_table"}
POST /qtl_table(.:format) {:action=>"create", :controller=>"qtl_table"}
new_qtl_table GET /qtl_table/new(.:format) {:action=>"new", :controller=>"qtl_table"}
edit_qtl_table GET /qtl_table/:id/edit(.:format) {:action=>"edit", :controller=>"qtl_table"}
qtl_table GET /qtl_table/:id(.:format) {:action=>"show", :controller=>"qtl_table"}
PUT /qtl_table/:id(.:format) {:action=>"update", :controller=>"qtl_table"}
DELETE /qtl_table/:id(.:format) {:action=>"destroy", :controller=>"qtl_table"}
and I do have plurals turned off:
ActiveRecord::Base.pluralize_table_names = false
but I get this error:
undefined local variable or method `search_qtl_table_index' for #<#<Class:0x8056a3fa8>:0x8056a2338>
This is related to this question which I will delete soon:
Rails: routing and path helpers
This has nothing to do with pluralizing. YOu need to use search_qtl_table_index_path when you reference it rather than just search_qtl_table_index (you need to add the _path to the end).
So, your form_tag statement should be:
<%= form_tag search_qtl_table_index_path, :method => 'get' do %>

Is there a way to fix a singular controller in rails 3.1?

I accidentally generated a singular controller comm_log without 's'. 's' was added to controller name, helpers, views, specs and routes. The customer_comm_log_path(f.customer_id, f.id) seems not right.
The relation is a comm log belongs to a customer and a customer has many comm logs.
resources :customers do
resources :comm_logs
end
The output of rake routes (related) is:
comm_logs_index GET /comm_logs/index(.:format) {:controller=>"comm_logs", :action=>"index"}
comm_logs_new GET /comm_logs/new(.:format) {:controller=>"comm_logs", :action=>"new"}
comm_logs_create GET /comm_logs/create(.:format) {:controller=>"comm_logs", :action=>"create"}
comm_logs_show GET /comm_logs/show(.:format) {:controller=>"comm_logs", :action=>"show"}
comm_logs_destroy GET /comm_logs/destroy(.:format) {:controller=>"comm_logs", :action=>"destroy"}
customer_comm_logs GET /customers/:customer_id/comm_logs(.:format) {:action=>"index", :controller=>"comm_logs"}
POST /customers/:customer_id/comm_logs(.:format) {:action=>"create", :controller=>"comm_logs"}
new_customer_comm_log GET /customers/:customer_id/comm_logs/new(.:format) {:action=>"new", :controller=>"comm_logs"}
edit_customer_comm_log GET /customers/:customer_id/comm_logs/:id/edit(.:format) {:action=>"edit", :controller=>"comm_logs"}
customer_comm_log GET /customers/:customer_id/comm_logs/:id(.:format) {:action=>"show", :controller=>"comm_logs"}
PUT /customers/:customer_id/comm_logs/:id(.:format) {:action=>"update", :controller=>"comm_logs"}
DELETE /customers/:customer_id/comm_logs/:id(.:format) {:action=>"destroy", :controller=>"comm_logs"}
Is there a way to fix the singular controller? Thanks.
Try script/destroy your_controller_name using the same controller name used to create it.

Why do edit links in this Rails view have the same id?

In this model, scheduled games and players are nested resources of franchise, with routing set up as follows:
OTH::Application.routes.draw do
resources :franchises do
resources :scheduled_games
resources :players
end
I can create these model objects correctly. But I must be doing something wrong in the view, which looks like this:
<% #franchise.scheduled_games.each do |game| %>
<p class="games">
<span class="date">
<b>Opponent:</b> <%= game.opponent %>
<b>date:</b> <%= game.date %>
<%= link_to 'Edit',edit_franchise_scheduled_game_path(#franchise) %>
</span> </p>
<% end %>
There are two things that are going wrong when this view is displayed. The worst is that each edit link has the same id for the nested resource (/franchises/1/scheduled_games/1/edit) even though the other information for the item is correct.
Second is that a blank record always displays.
Where have I gone wrong?
Update Here is the output of rake routes:
franchise_scheduled_games
GET /franchises/:franchise_id/scheduled_games(.:format) {:action=>"index", :controller=>"scheduled_games"}
POST /franchises/:franchise_id/scheduled_games(.:format) {:action=>"create", :controller=>"scheduled_games"}
new_franchise_scheduled_game
GET /franchises/:franchise_id/scheduled_games/new(.:format) {:action=>"new", :controller=>"scheduled_games"}
edit_franchise_scheduled_game
GET /franchises/:franchise_id/scheduled_games/:id/edit(.:format) {:action=>"edit", :controller=>"scheduled_games"}
franchise_scheduled_game
GET /franchises/:franchise_id/scheduled_games/:id(.:format) {:action=>"show", :controller=>"scheduled_games"}
PUT /franchises/:franchise_id/scheduled_games/:id(.:format) {:action=>"update", :controller=>"scheduled_games"}
DELETE /franchises/:franchise_id/scheduled_games/:id(.:format) {:action=>"destroy", :controller=>"scheduled_games"}
franchise_players
GET /franchises/:franchise_id/players(.:format) {:action=>"index", :controller=>"players"}
POST /franchises/:franchise_id/players(.:format) {:action=>"create", :controller=>"players"}
new_franchise_player
GET /franchises/:franchise_id/players/new(.:format) {:action=>"new", :controller=>"players"}
edit_franchise_player
GET /franchises/:franchise_id/players/:id/edit(.:format) {:action=>"edit", :controller=>"players"}
franchise_player
GET /franchises/:franchise_id/players/:id(.:format) {:action=>"show", :controller=>"players"}
PUT /franchises/:franchise_id/players/:id(.:format) {:action=>"update", :controller=>"players"}
DELETE /franchises/:franchise_id/players/:id(.:format) {:action=>"destroy", :controller=>"players"}
You need to specify the game as well when using link_to with nested resources:
edit_franchise_scheduled_game_path(#franchise, game)
Because they use the REST pattern, so if you want to delete a resource you use the HTTP/Delete request to the same url as you would for a GET request.

Problem with rails routing

I have the following in my routing config:
resources :users do
resources :apps, :controller => :user_apps
end
rake routes includes the following:
user_apps GET /users/:user_id/apps(.:format) {:action=>"index", :controller=>"user_apps"}
user_apps POST /users/:user_id/apps(.:format) {:action=>"create", :controller=>"user_apps"}
new_user_app GET /users/:user_id/apps/new(.:format) {:action=>"new", :controller=>"user_apps"}
edit_user_app GET /users/:user_id/apps/:id/edit(.:format) {:action=>"edit", :controller=>"user_apps"}
user_app GET /users/:user_id/apps/:id(.:format) {:action=>"show", :controller=>"user_apps"}
user_app PUT /users/:user_id/apps/:id(.:format) {:action=>"update", :controller=>"user_apps"}
user_app DELETE /users/:user_id/apps/:id(.:format) {:action=>"destroy", :controller=>"user_apps"}
However, when I try to access eg user_apps_path(1,2) I get /users/1/apps.2 rather than /users/1/apps/2.
Where am I going wrong?
I'm using rails 3.
The correct route is user_app_path(1,2) The pluralized version goes to the index action, making the second argument the format / extension of the request.

Resources