Rails 3 routing - ruby-on-rails

I have a weird problem.
I was using rails with scaffold to create CRUD and stuff.
Then I added a function called cnt that should give me the count of table entries.
For example Albums.count for a cipher.
But this morning, the whole routing was directing to that page.
When I tried to visit xxx/elements
I was linked do /cntalbums.
Looked like a routing thing - this is my routes.rb:
Something::Application.routes.draw do
resources :albums
get "home/index"
resources :elements
root :to => 'home#index'
end
That's all.
I deleted the view of cnt, I deleted the method, which was locaten in albums_controller.
So there is no friggn spot left, where cnt is implemented.
But still it seems like every GET links to that page.
I got this one in my logs:
Started GET "/albums" for xxxx at Fri Oct 05 09:54:42 +0200 2012
Processing by AlbumsController#cnt as HTML
Parameters: {"cntalbums"=>"albums"}
Completed 500 Internal Server Error in 47ms
ActionView::MissingTemplate (Missing template albums/cnt, application/cnt with {:formats=>[:html], :locale=>[:en], :handlers=>[:erb, :builder]}. Searched in:
* "/kunden/nnax.de/webseiten/entwicklung/poezy/app/views"
):
app/controllers/albums_controller.rb:18:in `cnt'
I had:
match ':cntalbums' => 'albums#cnt', :as => 'cnt_albums', :via => :get
Before; but I deleted that already.
So I don't have ANY clue, why I cannot see my albums, elements and so on.
Finally: rake routes gives me that:
albums GET /albums(.:format) albums#index
POST /albums(.:format) albums#create
new_album GET /albums/new(.:format) albums#new
edit_album GET /albums/:id/edit(.:format) albums#edit
album GET /albums/:id(.:format) albums#show
PUT /albums/:id(.:format) albums#update
DELETE /albums/:id(.:format) albums#destroy
elements GET /elements(.:format) elements#index
POST /elements(.:format) elements#create
new_element GET /elements/new(.:format) elements#new
edit_element GET /elements/:id/edit(.:format) elements#edit
element GET /elements/:id(.:format) elements#show
PUT /elements/:id(.:format) elements#update
DELETE /elements/:id(.:format) elements#destroy
home_index GET /home/index(.:format) home#index
root / home#index
so for my understanding it is clear; I wanna go to albums or elements and it enters the function of the fitting controller.
But it ALWAYS tries to reach AlbumsController#cnt
Although that function does not even exist anymore
ANY hints ? :/

Rails routes are first-come-first-routed, so if the CNT rule was above everything else, Rails will route that. Looking at the rule
match ':cntalbums' => 'albums#cnt', :as => 'cnt_albums', :via => :get
notice that any URL will match this. The path /hello/world will result in :cntalbums => "hello/world".
If you've deleted the line from your routes but are still being routed by it, are you sure that you've restarted your Rails server? (maybe even try clearing your browser's cache, but that really shouldn't be the problem)

try this
Something::Application.routes.draw do
resources :albums
...
match ':cntalbums' => 'albums#cnt', :as => 'cnt_albums', :via => :get
...
get "home/index"
resources :elements
root :to => 'home#index'
end
because rails routes are first come first serve

Related

Rails route appears in routes but throws a 404

I'm trying to add a simple route to an existing controller/action but strangely I'm getting a 404 error even though the route appears to exist.
Here's the relevant section of my routes.rb:
# Wines
scope 'wine' do
get '/', to: 'wines#index', as: 'wine_index'
get '/:collection', to: 'wines#collection_detail', as: 'collection_detail'
get '/:collection/:slug', to: 'wines#wine_detail', as: 'wine_detail'
get '/:style', to: 'wines#style_detail', as: 'style_detail'
end
It seems correct because here's what I see when I check:
$ rake routes
=>
Prefix Verb URI Pattern Controller#Action
wine_index GET /wine(.:format) wines#index
collection_detail GET /wine/:collection(.:format) wines#collection_detail
wine_detail GET /wine/:collection/:slug(.:format) wines#wine_detail
style_detail GET /wine/:style(.:format) wines#style_detail
GET|POST /*path(.:format) pages#error404
I also see an expected response in the console:
2.3.1 :003 > app.style_detail_path('semi-dry')
=> "/wine/semi-dry"
Yet, when I try to visit /wine/semi-sweet/ (Semi-sweet is a style "slug" which I use to search in the action) I get a 404 error.
What could I me missing? I've searched dozens of similar questions on S.O. and none of the solutions apply to my situation.
It seems you need to specify constraints. When you say 'wines/semi-sweet', how would the router decide whether it's a style_detail path or a colletion_detail path? They both have the same mask '/wines/:something'
It should be something like:
scope 'wine' do
get '/', to: 'wines#index', as: 'wine_index'
get '/:style', to: 'wines#style_detail', as: 'style_detail', constraints: proc { |r| Style.include?(r.params[:style]) }
get '/:collection', to: 'wines#collection_detail', as: 'collection_detail'
get '/:collection/:slug', to: 'wines#wine_detail', as: 'wine_detail'
end
This way the router will match predefined words (could be an array too) with wine styles, all the other strings will be considered as wine collections.
But it would be best to change the mask for these two paths, just to be safe, for example:
get '/:style', to: 'wines#style_detail', as: 'style_detail'
get '/c/:collection', to: 'wines#collection_detail', as: 'collection_detail'

Rails routes config thinks action method is an object id

I'm developing rails application and encountered such problem.
I have movies_controller.rb, where I have these actions and routes defined:
Prefix Verb URI Pattern Controller#Action
movies GET /movies(.:format) movies#index
POST /movies(.:format) movies#create
new_movie GET /movies/new(.:format) movies#new
edit_movie GET /movies/:id/edit(.:format) movies#edit
movie GET /movies/:id(.:format) movies#show
PATCH /movies/:id(.:format) movies#update
PUT /movies/:id(.:format) movies#update
DELETE /movies/:id(.:format) movies#destroy
root GET / redirect(301, /movies)
movies_by_director GET /movies/by_director(.:format) movies#by_director
But when I try to go to /movies/by_director?director="something", rails think, that I'm navigating to movies#show action with parameter :id = by_director.
What am I doing wrong?
Routes are matched in the order they are specified so make sure the route for "by_director" is defined above the resource routes for movies.
Something like this should do the trick:
get '/movies/by_director' => 'movies#by_director'
resources :movies
There are two problems in one here:
The default pattern matching for :id is loose enough that by_director is interpreted as an :id.
Routes are matched in order and GET /movies/:id appears before GET
/movies/by_director.
You can manually define GET /movies/by_director before your resources :movie as infused suggests or you could add a constraint to narrow what :ids look like:
resources :movies, constraints: { id: /\d+/ } do
#...
end
Manually ordering the routes is fine if there's just one or two of them to deal with, constraining :id is (IMO) cleaner and less error prone.

Rails Routes with Rapidfire

I'm working on creating an app that allows users to create surveys. I decided to add in the rapidfire gem which really helps to speed up the process of making surveys.
So far it's working great, however the "Home" link in my head that points to the root_url no longer brings me back to the root, but the "rapidfire" homepage.
Here's my config/routes.rb file:
SurveyMe::Application.routes.draw do
devise_for :developers
devise_for :users
mount Rapidfire::Engine => "/surveys"
root "static_pages#home"
match "/about", to: "static_pages#use", via: "get"
match "/developers", to: "static_pages#developer" , via: "get"
match "/how", to: "static_pages#how", via: "get"
when I do rake routes I get this:
rapidfire /surveys Rapidfire::Engine
root GET / static_pages#home
about GET /about(.:format) static_pages#use
developers GET /developers(.:format) static_pages#developer
how GET /how(.:format) static_pages#how
Routes for Rapidfire::Engine:
results_question_group GET /question_groups/:id/results(.:format) rapidfire/question_groups#results
question_group_questions GET /question_groups/:question_group_id/questions(.:format) rapidfire/questions#index
POST /question_groups/:question_group_id/questions(.:format) rapidfire/questions#create
new_question_group_question GET /question_groups/:question_group_id/questions/new(.:format) rapidfire/questions#new
edit_question_group_question GET /question_groups/:question_group_id/questions/:id/edit(.:format) rapidfire/questions#edit
question_group_question GET /question_groups/:question_group_id/questions/:id(.:format) rapidfire/questions#show
PATCH /question_groups/:question_group_id/questions/:id(.:format) rapidfire/questions#update
PUT /question_groups/:question_group_id/questions/:id(.:format) rapidfire/questions#update
DELETE /question_groups/:question_group_id/questions/:id(.:format) rapidfire/questions#destroy
question_group_answer_groups POST /question_groups/:question_group_id/answer_groups(.:format) rapidfire/answer_groups#create
new_question_group_answer_group GET /question_groups/:question_group_id/answer_groups/new(.:format) rapidfire/answer_groups#new
question_groups GET /question_groups(.:format) rapidfire/question_groups#index
POST /question_groups(.:format) rapidfire/question_groups#create
new_question_group GET /question_groups/new(.:format) rapidfire/question_groups#new
edit_question_group GET /question_groups/:id/edit(.:format) rapidfire/question_groups#edit
question_group GET /question_groups/:id(.:format) rapidfire/question_groups#show
PATCH /question_groups/:id(.:format) rapidfire/question_groups#update
PUT /question_groups/:id(.:format) rapidfire/question_groups#update
DELETE /question_groups/:id(.:format) rapidfire/question_groups#destroy
root GET / rapidfire/question_groups#index
I guess I don't really understanding what the "mount Rapidfire::Engine => "/surveys"" line is doing. Is there anyway I can adjust the root that it's generating so that it indeed points back to my original app's home page?
RapidFire, as an engine, has it's own root which is mounted under the /surveys path that you specified. Calling root_path from within a RapidFire namespaced controller/view will refer to the that root - not the sitewide root.
To fix this, you can pass the sitewide root path to the engine, and call that from the header instead - see Rails.root from engine for a way to do this.

Rails 3 add new routes to resources

I have the following problem:
I created one entity "Film" with the command "scaffold" and automatically added in my routes file "resources: films", and then I try to added an autocomplete via ajax, but always the calling ajax calls the "show" action instead of call the route that I added "autocomplete_term"
My routes files (routes.rb)
resources :films
I tried the following possibilities (routes.rb)
match 'films/autocomplete_term' => "films#index", :via=>:get
match "films/autocomplete_term/:term" => "films#autocomplete_term", :controller=>"films", :action=>"autocomplete_term", :as => :films_autocomplete, :via => :get
resources :films do
collection do
get 'autocomplete_term'
end
end
The route
** localhost.com:3000/films/autocomplete_term?term=a**
The ERROR
Couldn't find Film with id=autocomplete_term
app/controllers/films_controller.rb:28:in `show'
When I run the command rake routes
GET /films/autocomplete_term/:term(.:format) films#autocomplete_term
films_autocomplete
GET /films/autocomplete_term/:term(.:format) films#autocomplete_term
autocomplete_term_films
GET /films/autocomplete_term(.:format) films#autocomplete_term
Sorry for my English
And thanks in advance
The url to access this route
GET /films/autocomplete_term/:term
should be
localhost.com:3000/films/autocomplete_term/a
if you do localhost.com:3000/films/autocomplete_term?term=a it will think it is the show action, it will ignore the ?term=a
GET /films/:id
Thanks to Anezio, solved the problem, basicament had two things wrong:
1 - The route
match "films / autocomplete_term /: term"
2: The order in my routes.rb file (Rails routes are matched in the order they are specified)
resources: films
match 'films / autocomplete_term /: term' => "films # autocomplete_term"
The Final Solution: (routes.rb)
match 'films / autocomplete_term' => "films # autocomplete_term"
resources: films

Rails root url returns blank page

When i'm trying to get mydomain.com it returns blank page with no errors.
I have the correct root in routes.rb and when I'm trying to get the page that must be root mydomain.com/root_page it works fine.
Any page of my site works fine
public/index.html is deleted.
what is the problem with this approach?
Thanks.
here is my routes.rb file
mydomain.com::Application.routes.draw do
get "admin/index"
match "admin" => "admin#index"
match "materialy" => "materials#indexall"
match "materialy/:id" => "materials#showall"
match "m/:id" => "materials#light"
match "nashi-raboti" => "ourworks#indexall"
match "nashi-raboti/:alias" => "wphotos#indexall"
match "galereja" => "gphotos#indexall"
scope "/admin" do
resources :gphotos, :materials, :posts, :mphotos, :titles
resources :ourworks do
resources :wphotos
end
end
match ":alias" => "posts#showall"
root :to => "posts#showall", :alias => "main"
end
so when im trying to get mydomain.com/main everything works fine.
Nothing is written to development.log when i'm truing to get mydomain.com, but everything is good when i'm trying to get mydomain.com/main or any other page.
"mydomain.com" it is your application name? I think the problem is due to this. Try create app without dot in name.

Resources