Reputation: 3895
I know this has been asked in 100's of pages and I tried a lot to get it working. I do not know where I am missing out:
controller (logins)
class LoginsController < ApplicationController
def index
end
def create
end
def buttons
end
end
routes.rb
LoginApp::Application.routes.draw do
match "logins/index" => "logins#index", :as => :index , :via => :get
match "logins/create" => "logins#create", :as => :create ,:via => :get
match "buttons" => "logins#buttons", :via => :get
#get "logins/buttons" => "logins#buttons"
index.html.erb
<%= button_to 'Submit', { :action => "buttons", :controller => "logins"} %>
The buttons.html.erb page is created in the views directory.
When I click "Submit" Button, I get:
No route matches [POST] "/logins/index"
I tried changing the routes.rb, various form in index.htm.erb for buttons but not able to get it working. Have I overlooked anything?
Any help appreciated.
Thanks
Abhi
Upvotes: 0
Views: 8461
Reputation: 101
I know this is very old post....
This can be done in rails 4 as :
<%= button_to 'Submit', prefix_path, method: :get %>
prefix_path can be seen when you press rake routes and add '_path' at the end and replace in place of prefix_path
Upvotes: 0
Reputation: 76774
In light of the other answers not working, here is what I propose:
#config/routes.rb
resources :logins, only: [:index, :create] do
collection do
get :buttons
end
end
#view
<%= link_to "Submit", logins_buttons_path # -> whatever the path helper is %>
I have used link_to
because this answer recommends button_to
for POST
requests. This should work for you, but it's non-conventional, so you'll be best describing how you want it it work
Update
I am glad this worked for you
Several things which probably contributed to its functionality:
link_to
When using routing
in Rails, it's basically middleware which defines how your app will "catch" incoming requests. When you browse to domain.com/route
, Rails takes the /route
& the HTTP verb
part of the URL & loads the corresponding controller / action
to help it load up.
When defining your routes, you therefore need to be extremely careful about which routes
you define, and how you define them. The best way is to use Rails conventions (which essentially means using inbuilt helpers) such as resources :controller
--
We also used the link_to
in place of button_to
button_to
creates a small form on your page, which then sends a request to your assigned route. This form is typically POST
, but can be assigned GET
. In light of your new comment, you'll probably want to read up on the button_to
documentation to get it formatted correctly
Upvotes: 1
Reputation: 6404
The method of button_to is post by default.
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to
:method - Symbol of HTTP verb. Supported verbs are :post, :get, :delete, :patch, and :put. By default it will be :post.
Change the code in index.html.erb :
<%= button_to 'Submit', { :action => "buttons", :controller => "logins"}, method: :get %>
Upvotes: 3
Reputation: 22926
You have only get method defined in your routes.rb. That is why the error says no route matches for "POST" method. Add post method as well.
match "logins/index" => "logins#index", :as => :index , :via => [:get, :post]
Upvotes: 0