No route matches [Post] /characters/1/edit - ruby-on-rails

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

Related

Http request work differently from the parameter I gave in Ruby on Rails

I have article model and i want to give it votable ability with acts_as_votable gem.I have like_article route for like article and its PUT method and i have that route in my file
<%= link_to like_article_path(article), method: :put do %>
Like
<%= article.get_upvotes.size %>
<% end %>
if i hit the like button i'm getting No route matches [GET] "/articles/11/like" error it act like get method but i gave method: :put parameter it should be put why it dowsnt see my parameter how can i fix that?
I dont know why but i used button_to instead of link_to it worked.

POST Routing Issue for Custom Form

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.

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 %>

Rails button_to and using patch vs post

I'm having a really hard time getting rails to use the 'button_to' tag with a patch request vs post.
This is my tag:
<%= button_to 'Start Game', { :controller => 'provides', :id => #prov_id}, :method => :patch %>
I'm using 'resources :provides' in my routes, and I have an update method in my Provide controller.
Here is the output for provides when I run rake routes..
provides GET /provides(.:format) provides#index
POST /provides(.:format) provides#create
new_provide GET /provides/new(.:format) provides#new
edit_provide GET /provides/:id/edit(.:format) provides#edit
provide GET /provides/:id(.:format) provides#show
PATCH /provides/:id(.:format) provides#update
PUT /provides/:id(.:format) provides#update
DELETE /provides/:id(.:format) provides#destroy
Everything should be working, but I get the below error...
Started PATCH "/provides?id=1" for 127.0.0.1 at 2014-03-12 19:20:28 -0500
ActionController::RoutingError (No route matches [PATCH] "/provides"):
What is going wrong here? This is driving me insane, any help is highly appreciated.
You have a route: PATCH /provides/:id(.:format), but there is no PATCH route for "/provides?id=1"
Try:
<%= button_to 'Start Game', provide_path(#prov_id), :method => :patch %>
See more: http://guides.rubyonrails.org/routing.html
I am pretty sure you need to have a form tag for a PATCH since browsers don't currently generate non AJAX PATCH requests.
So pass :method => :patch to form_for as follows:
<%= form_for #provides, method: :patch do |f| %>
<%= f.hidden_field :foo, value: 'somevalue' %>
<%= f.submit 'Start Game' %>
<% end %>
This will send a method parameter which rails will detect and your controller can handle as a real PATCH. I included in the example the ability to pass hidden parameters for the actual patch as its unclear what usefulness PATCH would offer in your example otherwise.
Note: Don't try and embed the above html inside another form. The HTML spec doesn't permit nested form tags.

No Route Matches Rails 4

I generated a scaffold for Studio. Now am linking from Products view
<%= button_to 'Customize', new_studio_path %>
resources :studios is placed in routes.rb
I am still getting No route matches [POST] "/studios/new"
If someone could help me figure this out I would appreciate it tons.
Here is a gists of the files I am working with for this. https://gist.github.com/JRizzle88/7861628
Button's default HTTP method is POST, what you need is method GET, so you need to specify this explicitly:
<%= button_to 'Customize', new_studio_path, method: :get %>
Button is sending a POST request, where your server expects a GET request for given url. You need to specify method on a button:
<%= button_to 'Customize', new_studio_path, method: :get %>
The rails convention would be to use link_to which will send a GET request:
<%= link_to 'Customize', new_studio_path %>

Resources