'show' route matched instead 'new' - ruby-on-rails

rails 3.1
rake routes for admin/sections_contoller
trigger_admin_section GET /admin/sections/:id/trigger(.:format) {:action=>"trigger", :controller=>"admin/sections"}
admin_sections GET /admin/sections(.:format) {:action=>"index", :controller=>"admin/sections"}
POST /admin/sections(.:format) {:action=>"create", :controller=>"admin/sections"}
new_admin_section GET /admin/sections/new(.:format) {:action=>"new", :controller=>"admin/sections"}
edit_admin_section GET /admin/sections/:id/edit(.:format) {:action=>"edit", :controller=>"admin/sections"}
admin_section GET /admin/sections/:id(.:format) {:action=>"show", :controller=>"admin/sections"}
PUT /admin/sections/:id(.:format) {:action=>"update", :controller=>"admin/sections"}
DELETE /admin/sections/:id(.:format) {:action=>"destroy", :controller=>"admin/sections"}
routes.rb
namespace :admin do
resources :sections do
resources :items
resources :parameters
get :trigger, :on => :member
end
...
end
view
<%= link_to "Add a section", new_admin_section_path, :class=>'add-btn' %>
generated link
http://localhost:3000/admin/sections/new
result
No route matches {:action=>"show", :controller=>"admin/sections",
:id=>#<Section id: nil, ..., meta_description: nil}
strange bug or my mistake. other controllers has similar routes and all works fine.
for ex.:
<%= link_to 'Add a group', new_admin_group_path, :class=>'add-btn' %>
works GREAT!
please, help or i'll kill myself someday
upd1 same problem on heroku with this app.
upd2 join github issue: https://github.com/rails/rails/issues/4704

i found the answer.
once i put this string in view 'admin/section/_form'
<%= link_to 'delete', admin_section_path(#section), :method => :delete, :confirm => "Sure?" %>
i used this form for creating and editing. so combination of new object and deleting link for it caused the bug.
i used debugger for analysis.

Look at your "create" method in sections_controller.
I guess your section is correctly created but it redirect to "show" action. And "show" view may not exist.
Have you checked your database ? Is the section saved ?

Related

Rails: routing and path helpers [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Rails: Routing without plurals gives strange helpers
Turns out I had :resources qtl_table in config/routes.rb twice! I get this error:
undefined local variable or method `search_qtl_table_index' for #<#<Class:0x805aff3e0>:0x805af47b0>
in app/views/qtl_table/index.html.erb:
<h2>Search for QTL</h2>
<%= form_tag search_qtl_table_index, :method => 'get' do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
and in config/routes.rb:
Qtl::Application.routes.draw do
resources :qtl_table do
collection do
get 'search'
end
end
...
end
yes I do have plurals turned off:
ActiveRecord::Base.pluralize_table_names = false
Output of rake routes:
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"}
You may have plurals turned off, but this only affects the table names in the database, not how Rails handles routes.
Because the search route belongs to a collection, not a member, it acts on multiple model objects. Therefore, the route should be search_qtl_tables_path, note plural tables.
qtl_table is a model, and you want to search a collection of them, so it make senses that the route reads like "search multiple records".
edit: My main concern is that your rake routes shouldn't be showing those qtl_table routes twice. Are you repeating yourself somewhere in routes.rb?
OK, so you've deleted the extra routes. Now, this should work...
<%= form_tag search_qtl_table_index_path, :method => 'get' do %>
Try:
Qtl::Application.routes.draw do
resources :qtl_table do
collection do
get 'search', :as => 'search_qtl_table'
end
end
...
end

using form_for with a singular model

I have a Wizard model that the client references w/o an ID (it's saved in the session), so I've created a singular resource for :show and :update. I want the admin to have access to all instances of that model via index so admin can delete strays, so I've added a plural resources for :index and :destroy. The index and destroy works, but I can't figure out the right arguments to pass to form_for in the update view.
The setup
# config/routes.rb
WTest::Application.routes.draw.do
resource :wizard, :only => [:show, :update]
resources :wizards, :only => [:index, :destroy]
...
end
resulting in
$ rake routes
wizards GET /wizards(.:format) {:action=>"index", :controller=>"wizards"}
wizard DELETE /wizards/:id(.:format) {:action=>"destroy", :controller=>"wizards"}
GET /wizard(.:format) {:action=>"show", :controller=>"wizards"}
PUT /wizard(.:format) {:action=>"update", :controller=>"wizards"}
This sets up routes the way I'd expect.
The question (revised since original post)
In the console:
>> app.wizard_path
raises the error ActionController::RoutingError: No route matches {:action=>"destroy", :controller=>"wizards"}
Why is this? Have I set up my routes incorrectly? I need to specify :url => wizard_path for form_for() in the wizards's update view.
The details
If I specify an explicit path in my call to form_for:
# app/view/wizards/update.html.erb
<%= form_for #wizard, :url => wizard_path do |f| %>
<%= f.submit %>
<% end %>
... then attempting to render this for gets an error on the form_for line:
No route matches {:action=>"destroy", :controller=>"wizards"}
I have no idea why it's trying to match the destroy action. How do I get the form to submit to the {action=>"update", :controller=>"wizards"} route?
(By the way, I looked at bug 267, and I don't think it is the same as what I'm observing. But if it is this bug, is there a workaround?)
Carrying on the long tradition of answering my own questions (meh!), I think I figured it out. If my analysis is wrong, I'd be happy to give someone else the checkmark...
The cause of the problem
Look at the output of rake routes
$ rake routes
wizards GET /wizards(.:format) {:action=>"index", :controller=>"wizards"}
wizard DELETE /wizards/:id(.:format) {:action=>"destroy", :controller=>"wizards"}
GET /wizard(.:format) {:action=>"show", :controller=>"wizards"}
PUT /wizard(.:format) {:action=>"update", :controller=>"wizards"}
The path method 'wizard_path' is ambiguous: it can either refer to the DELETE clause, in which case it needs an :id argument (wizard_path(22)), or it can refer to the GET and PUT clauses, in which case it doesn't take an ID argument.
The solution
So my solution was to create a route specifically for deletion. My revised routes.rb file now reads:
resources :wizards, :only => [:index]
resource :wizard, :only => [:show, :update]
match 'wizard/:id' => 'wizards#destroy', :via => :delete, :as => :delete_wizard
and rake routes now produces:
$ rake routes
wizards GET /wizards(.:format) {:action=>"index", :controller=>"wizards"}
wizard GET /wizard(.:format) {:action=>"show", :controller=>"wizards"}
PUT /wizard(.:format) {:action=>"update", :controller=>"wizards"}
delete_wizard DELETE /wizard/:id(.:format) {:controller=>"wizards", :action=>"destroy"}
I needed to make a one-line change to the delete link in wizards/index.html.erb to use the new delete_wizard_path, but everything works now.

Trouble on setting the ':controller' parameter for a 'link_to' statement

I am using Ruby on Rails 3.0.9 and I would like to know why I get the error described below and how can I solve that.
In my /views/articles/categories/_content.html.erb file I have:
...
<%= link_to("New article", {:controller => content[:article_controller], :action => 'new'}) %>
...
If I set the content[:article_controller] to (both setting true and false for the :only_path option)
1. content[:article_controller] = 'articles'
2. content[:article_controller] = '/articles'
3. content[:article_controller] = '/articles/'
4. content[:article_controller] = '/'
4. content[:article_controller] = ''
I get respectively the following errors (note :controller values):
1. `ActionView::Template::Error (No route matches {:controller=>"articles/categories/articles", :action=>"new"})`
2. `ActionView::Template::Error (No route matches {:controller=>"articles//articles", :action=>"new"})`
3. `ActionView::Template::Error (No route matches {:controller=>"articles/", :action=>"new"})`
4. `ActionView::Template::Error (No route matches {:controller=>"articles//", :action=>"new"})`
4. `ActionView::Template::Error (No route matches {:controller=>"articles/categories/", :action=>"new"})`
Is it a Ruby on Rails bug or is it my fault? What is the problem and how can I solve that making the link_to properly work?
However I can solve that problem by using:
<%= link_to("New article", {:controller => '../', :action => 'new'}) %>
But why it works with '.../' but not in other ways?
I noticed that some time the controller path for which I try to set the content[:article_contr8oller] seems to relying on the "base" current controller path that is handling the view file (the controller file is app/controllers/articles/categories/concerns_controller.rb - read below for more information)... why it happens?
It also happens using url_for:
url_for(:controller => 'articles', :action => 'new')
Running the rake routes command I get the following:
articles_categories GET /articles/categories(.:format) {:action=>"index", :controller=>"articles/categories"}
POST /articles/categories(.:format) {:action=>"create", :controller=>"articles/categories"}
new_articles_category GET /articles/categories/new(.:format) {:action=>"new", :controller=>"articles/categories"}
edit_articles_category GET /articles/categories/:id/edit(.:format) {:action=>"edit", :controller=>"articles/categories"}
articles_category GET /articles/categories/:id(.:format) {:action=>"show", :controller=>"articles/categories"}
PUT /articles/categories/:id(.:format) {:action=>"update", :controller=>"articles/categories"}
DELETE /articles/categories/:id(.:format) {:action=>"destroy", :controller=>"articles/categories"}
articles GET /articles(.:format) {:action=>"index", :controller=>"articles"}
POST /articles(.:format) {:action=>"create", :controller=>"articles"}
new_article GET /articles/new(.:format) {:action=>"new", :controller=>"articles"}
edit_article GET /articles/:id/edit(.:format) {:action=>"edit", :controller=>"articles"}
article GET /articles/:id(.:format) {:action=>"show", :controller=>"articles"}
PUT /articles/:id(.:format) {:action=>"update", :controller=>"articles"}
DELETE /articles/:id(.:format) {:action=>"destroy", :controller=>"articles"}
P.S.: If you need more information, let me know and I will update the question as well.
UPDATE I
In my route file I have:
namespace :articles do articles :categories end
scope :path => 'articles/categories/:id', :controller => 'articles/categories/concerns' do
...
end
resources :articles
UPDATE II
In my view /views/articles/categories/_content.html.erb files I have:
<div class="links">
<%= link_to("New article", {:controller => content[:article_controller], :action => 'new'}) %>
</div>
In my Articles::Categories::ConcernsController (that is, in the app/controllers/articles/categories/concerns_controller.rb file) I have:
def show
#articles_category = Articles::Category.find(params[:id])
respond_to do |format|
format.html {
render :partial => '/views/articles/categories/_content.html.erb',
:locals => {
:content => {
:article_controller => '/articles'
}
}
format.js {
...
end
end
end
Did you try using symbols? I think they are more "direct".
<%= link_to("New article", {:controller => content[:article_controller].to_sym, :action => :new}) %>
Did you try using a relativ path?
<%= link_to("New article", {:controller => "../#{content[:article_controller]}", :action => 'new'}) %>
Why aren't you using link_to 'New article', new_article_path? Why use the old, tired url_for ... when you can use the named path/url helper (new_article_url).

ruby on rails no route matches action :delete

I'm setting up friendships within a RoR website. The model for it is user_id, friend_id, and pending (boolean). I followed the RailsCast on friendships for the most part, but making some changes to it too. What I have so far is that when you go to a user's page you can click Request Friendship and the code uses:
user_friendships_path(current_user, :friend_id => #user), :method => :post
This calls the create method in the Friendships controller. It automatically sets pending to true. What I want now is to have a link to Accept it which would just turn pending to false. So I am trying to set it up like
(<%= link_to "Accept", user_friendship_path(:user_id => current_user.id, :friend_id => friendship.user.id, :pending => 'false'), :method => :put %>)
I don't actually want to go to the edit page, because it just needs to set that boolean to false, so I want to call the update directly. But when I run this page, I get the error:
No route matches {:action=>"destroy", :controller=>"friendships", :user_id=>1, :friend_id=>2, :pending=>"false"}
I don't understand why. I'm not calling the destroy (that would be with :method => :delete), and there actually is a destroy method within the Friendship controller.
The resources are set up like:
resources :users do
resources :friendships
end
And the paths from running "rake routes" are:
user_friendships GET /users/:user_id/friendships(.:format) {:action=>"index", :controller=>"friendships"}
user_friendships POST /users/:user_id/friendships(.:format) {:action=>"create", :controller=>"friendships"}
new_user_friendship GET /users/:user_id/friendships/new(.:format) {:action=>"new", :controller=>"friendships"}
edit_user_friendship GET /users/:user_id/friendships/:id/edit(.:format) {:action=>"edit", :controller=>"friendships"}
user_friendship GET /users/:user_id/friendships/:id(.:format) {:action=>"show", :controller=>"friendships"}
user_friendship PUT /users/:user_id/friendships/:id(.:format) {:action=>"update", :controller=>"friendships"}
user_friendship DELETE /users/:user_id/friendships/:id(.:format) {:action=>"destroy", :controller=>"friendships"}
Any help would be greatly appreciated. Please let me know if you require more information.
Thanks.
The path that exists according to rake routes is
user_friendship PUT /users/:user_id/friendships/:id(.:format) {:action=>"update", :controller=>"friendships"}
The method you are using is put, but you aren't supplying ':id'.
There are two solutions depending on what you are trying to do:
Change the method to get - that will point you to the new action
Add :id to your URL builder - that will point you to the update action
Your code is leaning towards the second, but I think if you want to create a new friendship, the first would be better.
I'm baffled as to why the URL helper is mapping to a destroy action. However there is a simpler problem: user_friendship_path expects params :user_id and :id, not :friend_id.

RoR routing very basic problem

I have Exam controller.
In routes.rb there is "resources :exams"
In controller there are REST-like generated methods.
I want to add my own method there:
def search
#exams = Exam.where("name like ?", params[:q])
end
In view file:
<%= form_tag(search_path, :method => "post") do %>
<%= label_tag(:q, "Szukaj: ") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Szukaj") %>
<% end %>
I know, there is no results presentation yet, it doesn't work at all at this moment (:
When i go to http://localhost:3000/exams/search it's mapping it to show controller and search is a :id paramether then...
How to get http://localhost:3000/exams/search to run the seach controller?
You forgot to add route. Put this in routes.rb, before resources :exams
map.search '/exams/search', :controller => :exams, :action => :search
Note, that resources :exams doesn't generate routes for all public methods of the controller, it generates very specific set of routes. You can find more information in the rails routing guide. (see section 3.2 in particular)
You'll need to add additional parameters to your mapping. You can add "collection" methods like so:
map.resources :exams, :collection => {:search => :get}
When you rake routes, you'll see that it generates something like so:
search_exams GET /exams/search(.:format) {:controller=>"exams", :action=>"search"}
exams GET /exams(.:format) {:controller=>"exams", :action=>"index"}
POST /exams(.:format) {:controller=>"exams", :action=>"create"}
new_exam GET /exams/new(.:format) {:controller=>"exams", :action=>"new"}
edit_exam GET /exams/:id/edit(.:format) {:controller=>"exams", :action=>"edit"}
exam GET /exams/:id(.:format) {:controller=>"exams", :action=>"show"}
PUT /exams/:id(.:format) {:controller=>"exams", :action=>"update"}
DELETE /exams/:id(.:format) {:controller=>"exams", :action=>"destroy"}

Resources