POST Routing Issue for Custom Form - ruby-on-rails

I have added a custom route, custom controller code, and actioned the custom route in the form, but I get an error regarding the route, even as it shows the route in error message.
The form/view is as follows:
views/survey_request/confirmation.html.erb
<%= form_for #survey_request, url: survey_requests_confirm_path do |f| %>
<p>Your Email Address: <%= f.text_field :customer_email %></p>
<%= f.hidden_field :survey_token, value: #survey_request.survey_token %>
<p>Your survey token: <%= #survey_request.survey_token %></p>
<p><%= f.submit %></p>
<% end %>
My relevant routes in the routes.rb file are as follows:
get 'survey/:id', to: 'survey_requests#confirmation'
put 'survey_requests/confirmation', to: 'survey_requests#confirmation'
put 'survey_requests/confirm', to: 'survey_requests#confirm'
In the survey_requests_controller.rb I have a method defined
def confirm
#code here to confirm the users email and token
end
When I run the app, the confirmation.html.erb form shows up fine, to include the token passed to it. When I submit the form I get the following error:
No route matches [POST] "/survey_requests/confirm"
However when I scroll down on the same error page, it shows the route:
survey_requests_confirm_path PUT /survey_requests/confirm(.:format) survey_requests#confirm
Any suggestions? Thanks!

The problem is that the defined route uses a PUT method and the form route uses a POST method.
Either change the route to be POST with post 'survey_requests/confirm', to: 'survey_requests#confirm' or add method: :put to the form form_for #survey_request, url: survey_requests_confirm_path, method: :put so the methods match.

Related

Form with custom url keeps submitting to Create method

I'm trying to build a simple form that passes a few params to a controller.
Here's what I have:
<%= form_tag({url: order_pizza_path}, method: :post) do %>
<%= hidden_field_tag :id, value: 0, name:"tag-1" %>
<!-- hidden field is then filled in with js -->
<%= submit_tag "Submit" %>
<% end %>
<!-- routes: -->
get 'pizza/new' => 'pizza#new', as: 'new_pizza'
post 'pizza' => 'pizza#create', as: 'create_pizza'
post 'order_pizza' => 'pizza#order', as: 'order_pizza'
But when I submit, it keeps trying to point to the Create method in my Pizza controller. I keep getting the following error:
ActionController::ParameterMissing in PizzaController#create
param is missing or the value is empty: pizza
The form's url is /pizza. This is the url of the error: /pizza?method=get&url=%2Forder_pizza
This happens even if I change it to a GET request not POST. Why does my browser keep trying to go to the Create method?
The correct syntax is
form_tag(order_pizza_path, method: :post)
not
form_tag({url: order_pizza_path}, method: :post)
{url: order_pizza_path} is not a valid url_for_options so the form_tag will submit to the default, which is the create action.
A valid value for url_for_options would be
{action: 'order'}

No route matches [Post] /characters/1/edit

I am trying to reach the edit page for my character class, but for some reason it is routing with a POST when it should be a GET. All similar questions have not helped.
Here is my edit function:
def edit
end
Here are my routes:
<%= button_to 'Edit Character', edit_character_path(#character) %>
characters_path GET /characters(.:format) characters#index
POST /characters(.:format) characters#create
new_character_path GET /characters/new(.:format) characters#new
edit_character_path GET /characters/:id/edit(.:format) characters#edit
character_path GET /characters/:id(.:format) characters#show
PATCH /characters/:id(.:format) characters#update
PUT /characters/:id(.:format) characters#update
DELETE /characters/:id(.:format) characters#destroy
here is my edit link:
<%= button_to 'Edit Character', edit_character_path(#character) %>
The problem is that button_to, acccording to documentation, generates a form, and the form method is post
Any reason for which you are ussing button_to ?
Otherwise you can just use a link_to and add a custom class:
<%= link_to 'Edit Character', edit_character_path(#character), class: 'my-custom-class' %>
so you can add the css for your custom class and make it look like a button

Rails button_to with image and actions

I want to show button with image.
I have this code
<%= image_submit_tag "down.png", controller: "posts", action: "votedown", post_id: post.id, topic_id: post.topic_id, class: "xta" %>
Its visible properly but not calling action "votedown"
In my routes I have
post '/votedown', to: 'posts#votedown
Please also suggest if there is any other way to call the method votedown with params and image "down.png"
image_submit_tag must be used in conjunction with a form - it works just a normal html <input type="submit"> button.
You might also want to change your route definition into something more restful:
patch '/posts/:id/votedown' => "posts#votedown", as: 'votedown_post'
This makes it more apparent that this route acts on a post - and we use the PATCH method since we are changing a resource instead of creating a new resource.
Armed with our new route we can simply create a form:
<%= form_for(#post, url: votedown_post_path(#post) ) do |f| %>
<%= image_submit_tag "down.png", class: "xta" %>
<% end %>
Note that you do not need to add an input for the post id since it will be available as params[:id].
Another way to do this would be to use Rails unobstructive javascript driver to create a link or button which sends a PATCH request to '/posts/:id/votedown'.
<%= link_to image_tag("down.png", class: "xta"), votedown_post_path(#post), method: :patch %>

Form with method: "get" makes a POST request?

This is my form:
<%= form_tag(method: "get") do %>
<%= submit_tag("Submit") %>
<% end %>
When I submit this form I get a server error because there is no POST action for this URL. In my routes I have an action for GET, but it's not picked up. The error goes away when I assign an action to POST at the same URL as the GET. What am I doing wrong?
Like the comment above says, you'll need to add a path to the form, so it would look something like this...
<%= form_tag whatever_the_current_page_is_path, :method => :get %>

Rails app submitting 'post' request to a custom 'get' route

I have this button in my app, which is located in the views/deals/mgmt.htm.erb file:
<%= button_to "Deals", deals_mgmt_path, class: "btn btn-default" %>
Here are my 'deals' routes specified in my routes file:
resources :deals
get "deals/mgmt"
When I run 'rake routes', here's the route as I specified:
deals_mgmt GET /deals/mgmt(.:format) deals#mgmt
When I click the button, I get this routing error:
No route matches [POST] "/deals/mgmt"
Why is my rails trying to submit a post request here? I'm thoroughly confused. Thanks in advance!
The helper button_to creates a form, and forms submits POST requests to the server.
Inspect the page and you will see the form.
You should use a link_to, for a request using GET.
You can use this:
<%= button_to 'deals', deals_management_path, {:method => get} %>
Else
it's better to Go with link_to as by default it send get request to Server.
<%= link_to 'deals', deals_management_path %>

Resources