I am trying to generate a link that will use post method
<%= link_to "guess", :action => "submit_guess", :method => "post" %>
But when I click on that link I am getting
No route matches [GET] "/guess/submit_guess"
<%= button_to 'guess', action: 'submit_guess' %>
Or, if you have JS enabled:
<%= link_to 'guess', action: 'submit_guess', method: :post %>
# note the symbol
(It's better to use button_to and then style it with css if necessary)
<%= link_to "guess", {:action => "submit_guess"}, :method => "post" %>
Related
This should be an easy question, but I just can't figure it out. I want to trigger an action (which just renders a page) through a button using "button_to" in view:
<%= button_to "Fresh", action: 'fresh', method: 'get' %>
The error says "No route matches [POST] "/static_pages/fresh"". It seems that the button still uses "post" instead the "get". Meanwhile, if I use "link_to", it works fine.
<%= link_to "Fresh", action: 'fresh', method: 'get' %>
Thanks for any comments and help.
Try this
<%= button_to "delete", {:controller => :static_pages, :action => 'fresh'}, :method => :get %>
Also check your routes.rb to ensure that the route to fresh is defined.
<%= link_to (:controller => "company_stuff", :action => "index", :anchor => :menu), :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
I am having difficulty linking a page which is on a different controller and also the link is an anchor. Basically the controller is called company_stuff the action is index and the anchor is called #terms
The problem was that the :controller :action :anchor was not being passed through as a hash, separate from the CSS class
Below is the solution
<%= link_to "Terms Of Use", {:controller => "company_stuff", :anchor => "terms"}, :class => "links" %>
I believe you can try something like this
<%= link_to index_company_stuff_path + "#terms", :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
Or
<%= link_to index_company_stuffs_path + "#terms", :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
Depending on your controller name and route.
You can find more information on this question How to create an anchor and redirect to this specific anchor in Ruby on Rails
i was wondering why does
<%= form_tag( { :action => "/search", :method => "get" }, :class => "span4" ) do %>
...
<% end %>
give the following error?
No route matches [POST] "/assets"
i notice it's because of the /search. if i rewrote the code as...
<%= form_tag( { :action => "search", :method => "get" }, :class => "span4" ) do %>
...
<% end %>
without the /search, it correctly calls my controller method. can someone explain why? thanks
This will do what you mean:
<%= form_tag( "/search", :method => "get" , :class => "span4" ) do %>
...
<% end %>
If the first parameter of form_tag is a hash as you have given, it is passed behind-the-scenes to url_for, which inteprets :action as the action part of a route for it to reverse-map.
Since you (I guess) want to just pass a plain URL, just pass it as a string for the first arg.
More info here of course :)
the ":action" should receive a name of "action", such as: "create", "update", or "destroy"
:action => "/search"
here the "/search" is not a name, but an "url", which is not legal.
btw, I suggest you use xx_url instead of { :action => "", :controller => ""}, e.g.
<%= form_tag "/search", :method => "get" %>
or
# you defined "search_path" in config/routes.rb
<%= form_tag search_path, :method => "get" %>
I am trying to get this form to submit correctly. Here's what I have so far:
<% form_for(:user, :url => update_user_setting_path, :remote => true, :html => {:method => :post, :class => "search_form general_form"}) do |f| %>
and the button renders with this code:
<li><%= link_to raw("<span class='button approve'><span><span>SAVE</span></span></span>"), :action => 'create' %></li>
I am using action create, is this correct?
Here is the rendered form tag:
<form method="post" data-remote="true" class="search_form general_form" action="/settings/2/update_user" accept-charset="UTF-8">
What am I missing? Thanks for your help!
No, you are not using link_to properly. You need to use a submit tag to submit your form, not a link_to tag, for example:
<% form_for(:user, :url => update_user_setting_path, :remote => true, :html => {:method => :post, :class => "search_form general_form"}) do |f| %>
...
<li><%= f.submit "Save" %></li>
If you want to use a text link you'll have to have javascript submit the form. For example, if you are using jQuery you could do the following:
<%= link_to 'Save', "#", :onclick=>"$('.search_form').submit()" %>
I like Pan's solution but I prefer to use the ID of the form directly which you can get from the dom_id(obj). The form_for helper also uses dom_id(obj) to assign the form's ID. This way you aren't dependent on setting classes by hand or subject to accidentally submitting more than one form that share the same CSS class. It looks a little stranger but I usually have a custom FormBuilder anyway so I just add a generic link_to_submit method to encapsulate this:
<%= link_to 'Save', "#", :onclick => "$('##{dom_id(#user)}').submit()" %>
You don't need to use an id or a selector if you have jquery, you can simply do :
= link_to 'Save', "#", onclick: "$(this).closest('form').submit()"
Thanks for the answers... I ended up using this and it works great:
<li><%= link_to raw("<span class='button approve'><span><span>SAVE</span></span></span>"), "index_users", :onclick=>"document.forms['form1'].submit();"%></li>
I've ran into a ror problem using the link_to. Why does my link to use the GET method and my button_to use the POST method, after I specified my "method"=>"post" within the link_to parameters?
View:
<%= button_to "pdf", :action => 'getquote' %>
<%= link_to 'pdf', {:controller => 'inventories', :action => 'getquote', :method => :post } %>
Controller Method:
def getquote
#cart = find_cart
respond_to do |format|
format.pdf
end
end
Terminal Output (Button/Link, respectively):
Processing InventoriesController#getquote (for 127.0.0.1 at 2010-01-30 01:38:02) [POST]
Parameters: {"action"=>"getquote", "authenticity_token"=>"D2cwnHyTHgomdUM3wXBBXlOe4NQLmv1Srn0paLbExpQ=", "controller"=>"inventories"}
Processing InventoriesController#show (for 127.0.0.1 at 2010-01-30 01:39:07) [GET]
Parameters: {"method"=>"post", "action"=>"show", "id"=>"getquote", "controller"=>"inventories"}
I think your html options have to be in a separate hash from your url options:
<%= link_to 'pdf', {:controller => 'inventories', :action => 'getquote'}, {:method => :post } %>
I looked all over for a proper example, with no luck. For my code, I've mostly given up and just use the new style:
<%= link_to 'Delete', custom_event, :confirm => 'Are you sure?', :method => :delete %>
Might be useful for someone who is visiting :)
By default, button_to performs POST action only.
to do make a GET the syntax is as follows:
<%= button_to 'pdf', { :action => 'getquote'}, :method => :get %>
One possibility is that you have Javascript disabled, in which case it will fall back to a GET.