Reputation: 3895
I am again stuck on my road to learn RoR, now with form_tag. I followed the rails guide but not able to pass the parameters from form_tag to controller. My index.html.erb is:
<h1>Welcome to mySite.com</h1>
<p></p>
<p></p>
<%= form_tag(controller: "logins", method: "post") do %>
<p>
<%= label_tag(:username, "Username") %><br>
<%= text_field_tag(:username) %>
</p>
<p>
<%= label_tag(:password, "Password") %><br>
<%= password_field_tag(:password) %>
</p>
<p>
<%= submit_tag "create" %>
<%= submit_tag "clicked" %>
</p>
<% end %>
class LoginsController < ApplicationController
def index
end
def create
if params[:commit] == 'clicked'
render action: "clicked"
else
render text params.inspect
end
end
def clicked
render text: params.inspect
end
end
match "logins/index" => "logins#index", :as => :index , :via => [:get, :post]
#get "logins/create"
match "logins/create" => "logins#create", :as => :create ,:via => :get
#match "logins/clicked" => "logins#clicked", :as => :clicked, :via => [:get, :post]
get 'logins/clicked', to: 'logins#clicked'
#resources :logins
Button click on index.html.erb refreshes the page like brings me back to index page.
Update: Updated with the suggestions in the comment.
Thanks Abhi
Upvotes: 0
Views: 478
Reputation: 1611
<h1>Welcome to mySite.com</h1>
<p></p>
<%= form_tag(controller: "logins", action: "create") do %>
<p>
<%= label_tag(:username, "Username") %><br>
<%= text_field_tag(:username) %>
</p>
<p>
<%= label_tag(:password, "Password") %><br>
<%= password_field_tag(:password) %>
</p>
<p>
<%=submit_tag "Create"%>
<%= submit_tag "Clicked" %>
</p>
<% end %>
class LoginsController < ApplicationController
def index
end
def create
if params[:commit] == "Clicked"
p "hi"
redirect_to clicked_logins_path(request.parameters)
else
render text: params.inspect + "hi"
end
end
def clicked
render text: params.inspect
end
end
in routes.rb
resources :logins do
collection do
get 'clicked'
end
end
i hope it helps.
Upvotes: 1
Reputation: 1032
you need to give submit path in your form_tag
<%= form_tag(clicked_login_path, method: "post" ) do %>
<p>
<%= label_tag(:username, "Username") %><br>
<%= text_field_tag(:username) %>
</p>
<p>
<%= label_tag(:password, "Password") %><br>
<%= password_field_tag(:password) %>
</p>
<p>
<%= submit_tag 'submit'%>
</p>
Upvotes: 0