I'm getting this error, ActionController::MethodNotAllowed (Only get, put, and delete requests are allowed.), when I try to submit a form. My route looks like this,
admin.resources :email_launcher
and this is my form_for,
form_for :email_launcher, :url => new_admin_email_launcher_path, :method => :get do |f|
This is not in a form but in the new view, and I'm using HAML. I've google searched all day on the error but never found anything of value. Any help would be great, thanks.
Here's my rake routes for admin_email_launcher
admin_email_launcher_index GET /admin/email_launcher(.:format) {:controller=>"admin/email_launcher", :action=>"index"}
POST /admin/email_launcher(.:format) {:controller=>"admin/email_launcher", :action=>"create"}
new_admin_email_launcher GET /admin/email_launcher/new(.:format) {:controller=>"admin/email_launcher", :action=>"new"}
edit_admin_email_launcher GET /admin/email_launcher/:id/edit(.:format) {:controller=>"admin/email_launcher", :action=>"edit"}
GET /admin/email_launcher/:id(.:format) {:controller=>"admin/email_launcher", :action=>"show"}
PUT /admin/email_launcher/:id(.:format) {:controller=>"admin/email_launcher", :action=>"update"}
DELETE /admin/email_launcher/:id(.:format) {:controller=>"admin/email_launcher", :action=>"destroy"}
routes.rb
namespace :admin do
resources :email_launcher
end
admin/email_launchers_controller.rb
def new
#email_launcher = EmailLauncher.new
end
admin/email_launcher/new
<%= form_for([:admin,#email_launcher]) do |f| %>
...
<%= f.submit %>
<% end -%>
It will POST new email_launcher.
Why do you need to GET the new action? You're already on the new page.
Related
I am working an an Rails app. The problem I am have is when I cycle between my about and contact pages. I always get the error
No route matches [GET] "/pages/pages/about"
or
No route matches [GET] "/pages/pages/contact"
I'm trying to change the routes my nav bar partial the tag href to "/about" but the same error occur. it ask me to use the command rake routes and it shows
$ rake routes
restaurants GET /restaurants(.:format) restaurants#index
POST /restaurants(.:format) restaurants#create
new_restaurant GET /restaurants/new(.:format) restaurants#new
edit_restaurant GET /restaurants/:id/edit(.:format) restaurants#edit
restaurant GET /restaurants/:id(.:format) restaurants#show
PUT /restaurants/:id(.:format) restaurants#update
DELETE /restaurants/:id(.:format) restaurants#destroy
pages_about GET /pages/about(.:format) pages#about
root / restaurants#index
pages_contact GET /pages/contact(.:format) pages#contact"
can some one please help me!!
Your about and contact are under the url /pages/about and /pages/contact but the URL you are accessing is /pages/pages/about which doesn't exist.
You can see in the rake routes, the possible urls in the system.
you need to have the link in your web app as
<%= link_to 'About', pages_about_path %>
<%= link_to 'Contact', pages_contact_path %>
in your routes you can do something like
#config/routes.rb
ClientCruiser::Application.routes.draw do
....
match "contact" => "pages#contact", :as => :contact, via: :all
match "about" => "pages#about", :as => :about, via: :all
....
root :to => 'pages#index'
end
The output of this
contact /contact(.:format) pages#contact
about /about(.:format) pages#about
root GET / pages#index
the way you would make the call would be
<%= link_to about_path, "About", class: '' %>
<%= link_to contact_path, "Contact", class: '' %>
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
This is what I've used with remote_form_tag:
<% form_remote_tag(:url => {:controller => '/companies', :action => 'update'},
:update => 'tags') do %>
<%= text_field :company, :tag_list %>
<%= submit_tag 'Save' %>
<% end %>
This is in a Company.view, where Company is a model that is acts_as_taggable_on enabled.
My expectation is that, via ajax, a post is made to companies/10/update
But, instead, what is posted is:
http://localhost:3000/companies/10
and the response is:
No action responded to 10. Actions: create, destroy, edit, email_this_week, index, new, show, and update
This is the update method in CompaniesController:
def update
#company = Company.find(params[:id])
if request.xhr?
# add the given tag to the company
#company.tags << params[:company][:taglist]
#company.save
render :partial => 'tags'
else
if #company.update_attributes(params[:company])
flash[:notice] = "Successfully updated company."
redirect_to #company
else
render :action => 'edit'
end
end
end
Help...?
DELETE /companies/:company_id/contacts/:id(.:forma
{:controller=>"contacts", :action=>"destroy"}
companies GET /companies(.:format)
{:controller=>"companies", :action=>"index"}
POST /companies(.:format)
{:controller=>"companies", :action=>"create"}
new_company GET /companies/new(.:format)
{:controller=>"companies", :action=>"new"}
edit_company GET /companies/:id/edit(.:format)
{:controller=>"companies", :action=>"edit"}
company GET /companies/:id(.:format)
{:controller=>"companies", :action=>"show"}
PUT /companies/:id(.:format)
{:controller=>"companies", :action=>"update"}
DELETE /companies/:id(.:format)
{:controller=>"companies", :action=>"destroy"}
When you update a resource like Company with ID 10, Rails will use the RESTful route:
PUT /companies/10
The PUT method is taken into account when routing your request. Taken from your routes:
PUT /companies/:id(.:format)
{:controller=>"companies", :action=>"update"}
This is correct behaviour for Rails. Just implement the update method in your CompaniesController.
If you require more info on RESTful routes in Rails, check up on this document: http://guides.rubyonrails.org/routing.html
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"}
I'm working with Ruby on rails 2.3.4 and I'd like to have a link that executes an action when clicked.
The relevant part of the routes.rb file looks like this:
map.search_filter_relevance "/anuncios/buscar", :controller => 'announcements', :action => 'search_filter_relevance'
My view(it's the model's index page) looks like this:
<%= link_to 'MÁS RELEVANTES', search_filter_relevance_path %>
And my controller looks like this:
def search_filter_relevance
raise params.inspect
unless params[:announcements].nil? or params[:announcements].empty?
#announcements = params[:announcements].order_by_featured
end
end
The problem is that when I click the link I get an error due to some null value in the Show action! I'm not accessing that action at all...why is executing it?
EDIT:
Here is the routes output:
search_filter_relevance_announcements GET /anuncios/search_filter_relevance(.:format) {:controller=>"announcements", :action=>"search_filter_relevance"}
announcements GET /anuncios(.:format) {:controller=>"announcements", :action=>"index"}
POST /anuncios(.:format) {:controller=>"announcements", :action=>"create"}
new_announcement GET /anuncios/new(.:format) {:controller=>"announcements", :action=>"new"}
edit_announcement GET /anuncios/:id/edit(.:format) {:controller=>"announcements", :action=>"edit"}
announcement GET /anuncios/:id(.:format) {:controller=>"announcements", :action=>"show"}
PUT /anuncios/:id(.:format) {:controller=>"announcements", :action=>"update"}
DELETE /anuncios/:id(.:format) {:controller=>"announcements", :action=>"destroy"}
search /anuncios/buscar {:controller=>"announcements", :action=>"search"}
power_search /anuncios/buscar {:controller=>"announcements", :action=>"power_search"}
Try this code in your routes.rb file
map.resources :announcements,:collection=>{:search_filter_relevance=>:get}
And, comment the below line of code in your routes.rb file
map.search_filter_relevance "/anuncios/buscar", :controller => 'announcements', :action => 'search_filter_relevance'
When you go into console and
rake routes
can you paste all the lines that have search_filter_relevance as well as anuncios ?
Updated:
Since you want to call the search_filter_relevance action in the announcements controller you will need to use the listed route search_filter_relevance_announcements_path
<%= link_to 'MÁS RELEVANTES', search_filter_relevance_announcements_path %>
One other option would be to specify the controller and action manually
<%= link_to 'MÁS RELEVANTES', {:controller => "announcements", :action => "search_filter_relevance"} %>