Reputation: 495
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"
Upvotes: 1
Views: 925
Reputation: 101811
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 %>
Upvotes: 2