rails link_to controller action - ruby-on-rails

I show in my homepage 4 pictures, and when a user click on one of it, I want to change a parameter :asked in my db.
In my view I've added
<%= link_to image_tag(friends.picture), {:controller => "static_pages", :action => "recomend", :id => friends.user_id} %>
In the Static_Pages_Controller I have
def recomend
a = Friends.find_by_user_id(params[:id])
a.update_attribute(:asked, true)
end
And in routes.rb
resources :static_pages do
resources :recomend
end
but when I click on it, the server refresh my home (why?!) and in the server logs i see
Started GET "/auth/failure?action=recomend&controller=static_pages&id=101" for 127.0.0.1 at 2012-11-17 19:59:25 +0100.

Maybe it's not recognize the link. I suppose friends.picture is a link for image, so you can try this:
<%= link_to( "", :controller => "static_pages", :action => "recomend", :id => friends.user_id) do %>
<%= image_path(friends.picture) %>
<% end %>

Related

I'm getting No route matches [DELETE] with custom action and routes in ruby on rails

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.

Rails adding id as a param in url

I'm sure that this has something to do with my N00b syntax but I'm having trouble working out what it is...
I am creating a menu by looping through items in my subpages table and creating a link for each item that is returned, Like this:
<% #subpages.each do |menu| %>
<%= link_to(menu.name, {:controller => 'public', :action => "page", :id => menu.permalink }, :class => "show action footer-link") %>
<% end %>
this is working fine on the homepage of my site but if you visit one of the subpages, let's say the about us page:
http://localhost:3000/public/page/about-us
and then try to use the menu again to visit "contact us", instead of taking you to this link as i would expect:
http://localhost:3000/public/page/contact-us
It takes you to this link:
http://localhost:3000/public/page/about-us?id=contact-us
What school-boy error am I making here?
Thanks in advance.
edit: my routes
root :to => "public#index"
get 'admin', :to => 'access#menu'
get 'public/show/:permalink', :to => 'public#show'
get 'public/page/:permalink', :to => 'public#page'
Try with this :
<% #subpages.each do |menu| %>
<%= link_to(menu.name, page_public_path(menu.permalink), :class => "show action footer-link") %>
<% end %>
Thanks
I managed to get it to work by passing the :permalink param to the controller instead of the ID
<% #subpages.each do |menu| %>
<%= link_to(menu.name, { :action => "page", :permalink => menu.permalink }, :class => "show action footer-link") %>
<% end %>

Rails : not routing properly, redirect back to another controller

I have a page on the website, which has lots of anchor link eg #menu | #sauces etc
On the page itself the links work fine, its brilliant.
However when I am on a different controller/view, The links do not take me back to the main controller, and to the anchor point clicked.
here is an example of one anchor link, which is in the header (which is on ALL controller views)
<%= link_to '#main', :id => 'menu_link' do %>
<li>Menu</li>
<% end %>
That is in :controller => "main", :action => "index"
When I am in another controller, for example my locations controller,
The links become like this localhost:3000/locations#menu
It should really be localhost:3000/#menu
The root is set to go to the main controller and index action.
Here is my routes.rb file
root :to => "main#index"
match 'admin', :to => 'access#admin_index'
match 'locations', :to => 'ranch_locations#locations'
match ':controller(/:action(/:id))(.:format)'
You need to specify that it is going to a different controller.
<%= link_to '#main', :controller => "main", :action => "index", :id => 'menu_link' do %>
<li>Menu</li>
<% end %>
You should use root_path(:anchor => :main) instead of "#main"
link_to "Comment wall", profile_path(#profile, :anchor => "wall")
# => Comment wall
http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
upd:
<%= link_to '#main', :id => 'menu_link' do %>
should be changed to
<%= link_to root_path(:anchor => :main), :id => 'menu_link' do %>

problems with form_tag for controller action with members-get route

I'm making a form_tag panel that contains information (checkboxes) specific to a controller action. This action is set up in "routes.rb" as follows:
resources :students do
collection do
get :send_student_report_pdf
end
end
This setup works perfectly when I call the action from a link_to:
<%= link_to "Download PDF Report", :action => 'send_student_report_pdf', :controller => 'students'%>
However when I used it in a form_tag, it keeps giving me this error:
Routing Error
No route matches "/students/send_student_report_pdf"
The form_tag code I have is here:
<%= form_tag :controller => 'students', :action => 'send_student_report_pdf', :method => 'get' do %>
<%= label_tag "Include columns" %> <br>
<%= check_box_tag "first_name", params[:first_name], checked = true %> <%= label_tag "First Name" %><br>
<%= submit_tag "Download PDF Report", :action => 'send_student_report_pdf', :controller => 'students'%>
<% end %>
I have tried giving it the url, path like:
<%= form_tag send_student_report_pdf_students_path, :method => 'get' do %>
But it has been consistently giving me the same Route error (as if the action doesn't exist at all in routes.rb, even though it works perfectly using link_to instead of form_tag submit
Here is the code for the action in the controller, it basically sends back a file.
def send_student_report_pdf
#students = search_sort_and_paginate
puts "params[:first_name] = ", params[:first_namea]
send_data(generate_pdf_report(#students), :filename => "report.pdf", :type => 'application/pdf')
end
If you see that I'm missing something here, please help me.
Thank you very much,
Regards,
The :method => 'get' part in your form_for is in the url_for_options hash, not the options hash, so Rails will be putting it onto the url as cgi params instead. Try changing it to this:
form_tag url_for(:controller => 'students', :action => 'send_student_report_pdf'), :method => 'get' do ...
The reason you can't use the named route is because you didn't name it in your routes. If you name it in your routes and use the named route in your form_tag, you won't need to use url_for...
resources :students do
collection do
get :send_student_report_pdf, :as => :send_student_report_pdf
end
end
You can check whether your routes are as you expect by running rake routes

No route matches in Rails 3.0.4

Been staring at this problem for a while now. Here's the error I'm getting when I try to view the page.
No route matches {:action=>"confirm", :controller=>"locations"}
This is what I have in the view.
<%= form_for(#location, :url => { :action => :confirm }) do |f| %>
<% end %>
And I think my routes file is set up correctly.
Finder::Application.routes.draw do
resources :locations do
member do
post :confirm
end
end
root :to => 'locations/index'
end
Any ideas?
Updated:
Ran rake routes and get what I think is correct.
confirm_location POST /locations/:id/confirm(.:format) {:action=>"confirm", :controller=>"locations"}
You can debug your routes easily in the future by running $ rake routes and looking at the output. ;)
I think what is happening is that your post :confirm isn't registering the route you're expecting. In the guides, match and it's brethren accept a string as a URL segment like so:
resources :locations do
member do
post 'confirm'
end
end
Note that "confirm" is now a string instead of a symbol.
If this doesn't help, run $ rake routes and tack the output onto your question.
Update
After seeing your rake output, I think that you just need to specify the POST method on your form_for:
<%= form_for(#location, :url => { :action => :confirm }, :method => :post) do |f| %>
<% end %>
You can also make this more readable using that helper method that Rails defines:
<%= form_for(#location, :url => confirm_location_path(#location), :method => :post) do |f| %>
<% end %>
Did you define the confirm action in your LocationsController?
Try adding a :method => :post to your form_for
<%= form_for(#location, :url => { :action => :confirm }, :method => :post) do |f| %>
<% end %>
Make sure that form_for doesn't sneak in a hidden field with _method=put if you have declared the route as accepting only post in your routes file.

Resources