I had my link for editing brands like this:
<%= link_to 'Edit', :action => 'edit', :id => brand %>
I found around here a way to make it look like a button, so I did this (not sure why the url helper method needs to be used, but with :action => 'edit' it didn't work, it kept thinking it was post action):
<%= button_to 'Edit', admin_brand_edit_path(brand), :method => "get" %>
However, the generated url is like this:
http://localhost:3000/admin/brand/edit.1
Where it should be
http://localhost:3000/admin/brand/edit/1
Result of rake routes for brand edit:
admin_brand_edit GET /admin/brand/edit(.:format) admin/brand#edit
admin_brand_update POST /admin/brand/update(.:format) admin/brand#update
routes.rb
get 'admin/brand/edit'
Related
The forum_posts controller is located in app/controllers/forum_threads/forum_posts_controller.rb
I don't know if I have to call forum_threads:forum_posts in the link_to.
Controller:
http://pastebin.com/t9vuyxdP
HTML:
http://pastebin.com/LextuZ74
On a side note, how do you add a button to the link_to? I've tried adding :class => "button" at the end, doesn't cause an error but still just shows a link not a button.
If you want to use link_to with older-style controller/action/id arguments:
<%= link_to "Edit", :controller => "forum_threads/forum_posts", :action => "edit", :id => forum_post.id, :forum_thread_id => #forum_thread.id %>
But Rails prefer newer RESTful routes whenever possible. If you define your controller correctly in routes.rb, you can write link_to like this:
<%= link_to "Edit", edit_forum_thread_forum_post_path(#forum_thread, forum_post) %>
UPDATE 18/11/2015: You forgot to include forum_thread_id in link_to as I saw your route require forum_thread_id and id param to make it work.
For more information on link_to, refer to: http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
I have a show page which lists all objects in my DB. I have a delete link that sends the ID of that object to the destroy method in the associated controller and I can then easily delete that object and redirect back to the show page.
On the same show page I have a set of inline images beside each corresponding object. Each image is actually a link (except first as that shouldn't be deleted). When I click a specific image I send the ID of the object plus the column name of the object as a string to a new custom method called destroyImage.
I've created this method in my controller:
def destroyImage
garment = Parse::Query.new("Garments").eq("objectId", params[:id]).get.first
garment[params[:imageToDelete]] = nil
if garment.parse_delete
flash[:success] = "Image successfully deleted!"
redirect_to adminpanel_path
else
flash[:error] = "Image not deleted, try again!"
render "show"
end
end
In my view:
<td class= "images">
<div id="deleteText"><%= "Click on image to delete." %></div>
<%= image_tag garment["image"].url if garment["image"] %>
<%= link_to image_tag(garment["image2"].url), destroyImage_adminpanel_path(:id => garment["objectId"], :imageToDelete => "image2"), :method => 'delete' if garment["image2"] %>
<%= link_to image_tag(garment["image3"].url), destroyImage_adminpanel_path(:id => garment["objectId"], :imageToDelete => "image3"), :method => 'delete' if garment["image3"] %>
<%= link_to image_tag(garment["image4"].url), destroyImage_adminpanel_path(:id => garment["objectId"], :imageToDelete => "image4"), :method => 'delete' if garment["image4"] %>
<%= link_to image_tag(garment["image5"].url), destroyImage_adminpanel_path(:id => garment["objectId"], :imageToDelete => "image5"), :method => 'delete' if garment["image5"] %>
<%= link_to image_tag(garment["image6"].url), destroyImage_adminpanel_path(:id => garment["objectId"], :imageToDelete => "image6"), :method => 'delete' if garment["image6"] %>
</td>
</tr>
</tbody>
In my routes:
Rails.application.routes.draw do
resources :adminpanel do
member do
get 'destroyImage'
end
end
Rake routes:
destroyImage_adminpanel GET /adminpanel/:id/destroyImage(.:format) adminpanel#destroyImage
adminpanel_index GET /adminpanel(.:format) adminpanel#index
POST /adminpanel(.:format) adminpanel#create
new_adminpanel GET /adminpanel/new(.:format) adminpanel#new
edit_adminpanel GET /adminpanel/:id/edit(.:format) adminpanel#edit
adminpanel GET /adminpanel/:id(.:format) adminpanel#show
PATCH /adminpanel/:id(.:format) adminpanel#update
PUT /adminpanel/:id(.:format) adminpanel#update
DELETE /adminpanel/:id(.:format) adminpanel#destroy
I'm getting an error ActionController::RoutingError (No route matches [DELETE]. I've looked at my routing and there seems to be no path for destroyImage (DELETE) I can only see one for GET.
I'm sure this is small issue that can me fixed easily but after reading the guides I'm still slightly lost.
Appreciate your help.
Thanks.
You should create your route with delete instead of get:
delete 'destroyImage'
Also the convention in Ruby is that methods (thus, also Rails actions) are named with underscore instead of camel case, so your action should be named destroy_image.
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.
I have a Model with an attribute votes. I have a link in a view that needs to increment the value of votes - what is the best way to do this?
I am currently trying a link like:
<%= link_to 'Up', '#', :method => :voteup %>
and a voteup method in the model_controller but this isn't working.
I think the best way would be this:
In config/routes.rb:
resources :quotes do
member do
post :upvote
end
end
And your link:
<%= link_to 'Up', upvote_quote_path(#quote), :method => :post %>
Note that we use a POST request, which is more appropriate than a GET request when modifying a record.
:method is only supposed to be used to specify between POST, GET, DELETE, and PUT requests. Your second parameter of link_to should be the action you want to execute in your controller.
<%= link_to "Up", :action => :voteup %>
I'm trying to create unique anchors for every comment on my blog so a person can take the url of an anchor and paste it in their browser, which will automatically load the page and scroll down to the point in the page where their comment starts.
Perhaps I'm going about this the wrong way but I've tried this which was to no avail.
Comment view - Fail 1 - when pasted in a browser this link does not scroll down to the desired position
<%= link_to '#', :controller => 'posts', :action => 'show', :id => comment.post, :anchor => 'comment_' << comment.id.to_s %>
Comments controller - Fail 2 - Correct url in browser but no scrolling happens it just stays at the top of the page
redirect_to :controller => 'posts', :action => 'show', :id => #post, :anchor => 'comment_' + #comment.id.to_s
If someone could help I'd be very grateful :)
UPDATE: The solutions below almost work, however I come out with the following URL which isn't being scrolled to if I click on it.
#
i.e. http://localhost:3000/posts/please-work
Actually, anchor is an option for the path, not for the link_to
<%= link_to '#', post_path(comment.post, :anchor => "comment_#{comment.id}") %>
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#M001565
link_to "Comment wall", profile_path(#profile, :anchor => "wall")
# => Comment wall
It looks like you want to use the link_to code that you have in your question. Then in your list of comments you have to make sure that you have an anchor tag named the same thing in the link.
So this:
<%= link_to 'Your comment', post_path(#comment.post) + "#comment_#{#comment.id.to_s}" %>
will generate something like this
Your comment
/* html code */
<a name="comment_1234">This is a comment</a>
You have to manually tack on the #comment_ otherwise the link_to method thinks that the :anchor attribute that you are passing it is for that tag.
Here's an improvement on #XGamerX's answer.
<%= link_to '#', [comment.post, { anchor: dom_id(comment) }] %>
Or
<%= link_to '#', post_path(comment.post, anchor: dom_id(comment)) %>
Try this:
<%= link_to '#', post_path(comment.post), :anchor => "comment_#{comment.id}" %>
this is best way:
<%= link_to '#', post_path(comment.post, anchor: dom_id(comment.id)) %>
These links will scroll down to position where you have code like:
<a name="comment_1"></a>
I don't know if there are helpers that will do it for you, but it is very simple and you can write your own.