At this time I added a new route in config/routes.rb this way
resources :users do
get "newasignacion" => "users#newasignacion", :as => "newasignacion"
end
And the link is this way
= link_to "Asignar Tarea", user_newasignacion_path(#user),
:class => 'btn btn-success btn-mini' %>
But when I access the link i get this link:
http://localhost:3003/users/%23%3CActiveRecord::Relation::ActiveRecord_Relation_User:0xa6c7f3c%3E/newasignacion
Can somebody tellme why is this problem ?
It should be like that http://localhost:3003/users/5/newasignacion.
Whatever is assigned to #user is an class type ActiveRecord::Relation, when it should be an instance of User. For the sake of illustration, see how passing User.first to your link helper outputs a valid link:
= link_to "Asignar Tarea", user_newasignacion_path(User.first),
:class => 'btn btn-success btn-mini' %>
Verify that you're correctly assigning a user to #user in whatever controller action is rendering out the view in question.
Related
Really new to using Ruby on Rails and programming in gentle so apologies in advance if this seems very basic.
I'm working on a very simple wiki based website, where users can upgrade and downgrade their account.
In my downgrade.html.erb I have the following code:
<p>Are you sure you want to downgrade back to standard?<p>
<%= link_to "Yes", :controller => :charges, :action => :downgrade1, class: 'btn btn-danger' %>
<%= link_to "No", root_url, class: 'btn btn-success' %>
and in my charges_controller.rb I have my downgrade1 method:
def downgrade1
if current_user.premium?
current_user.update_attribute(:role, 'standard')
flash[:success] = "You have been downgraded to standard."
redirect_to root_url
else
false
end
end
Ultimately, when the user clicks that 'Yes' button, I want that downgrade1 method to run and the user's account to be downgraded back to standard.
However, what happens is the website loads a 'show webpage' with my header and footer, but the user is still a premium user.
Any ideas how I can fix this?
Thanks for your attempts to solve my problem, both your answers eventually led to me finding the solution.
In the end, I used this code in my view:
<%= button_to "Yes", { :controller => "charges", :action => "downgrade1"}, class: 'btn btn-danger' %>
and added this to my routes.rb
post "charges/downgrade1" => "charges#downgrade1"
Downgrading now works as planned.
Thanks again for your help.
I think the problem here is that you didn't specify the HTTP method for your link_to, like
<%= link_to "Yes", :controller => :charges, :action => :downgrade1, class: 'btn btn-danger', :method => :patch %>
By default, it will call a GET method, then nothing in your database will be changed.
And make sure you have defined that method in your routes.rb
You are missing one closing tag at the end of your if - else statement:
else
false
end
end
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.
I'm trying to style a rails link using css using the following code:
<%= link_to "Learn More", :controller => "menus", :action => "index", :class => "btn btn-inverse" %>
I would expect that this would create a link that looks like this:
Learn More
Instead, rails is rendering this -
Learn More
Has anyone else had this problem / know what I'm doing wrong? I know I can avoid this problem by manually creating the anchor tag rather than using helper, but I was wondering if there was a way to pass the css class info to the helper itself. I'm using Rails 3.2.6.
Thanks!
You have a syntax problem. Try this instead:
<%= link_to "Learn More", {controller: "menus", action: "index"}, class: "btn btn-inverse" %>
Some documentation for you to go further with the link_to Helper
They say:
Be careful when using the older argument style, as an extra literal hash is needed:
link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article"
# => Articles
Leaving the hash off gives the wrong link:
link_to "WRONG!", :controller => "articles", :id => "news", :class => "article"
# => WRONG!
I recommend you to use the URL helper generated following your routes configuration. In your case:
link_to "Learn More", menus_path, :class => "btn btn-inverse"
A little reminder on the Helpers generated:
# routes.rb
resources :users
# any view/controller
users_path #=> /users
edit_user_path(user) #=> /users/:id/edit
user_path(user) #=> /users/:id (show action)
new_user_path(user) #=> /users/new
Try new argument convention:
<%= link_to 'Learn More', 'menus#index', class: 'btn btn-inverse' %>
if you do not have a controller action / route necessary for the link, you can pass nil as the placeholder and get the classes to apply as necessary
<%= link_to 'link verbiage', nil, class: 'classes for action tag'%>
I solved my problem by the way
<%= link_to image_tag("imageexamplo.png", class: 'class or id examplo css'),{controller: "user" , action: "index"}%>
This is how i solved it using another view engine, HAML just in case a fellow developer is having this need
%i= link_to "Add New Blog Post", user_post_edit_new_url(current_user), :class => "fa fa-plus-circle"
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
How can I achieve query string and URL parameters in a link_to block declaration? Right now, I have this, which works:
<%= link_to 'Edit', :edit, :type => 'book', :id => book %>
The above works, and outputs:
http://localhost:3000/books/edit/1?type=book
What I want to do is something like this:
<% link_to :edit, :type => 'book', :id => book do %>
...
<% end %>
But the above format outputs:
http://localhost:3000/books/edit/
Which isn't what I'm looking for... I want it to output a URL like the previous example.
How can I achieve this?
link_to takes the same options that url_for does. Having said that, there is no :type option and it doesn't really accept blocks, so my guess is that the reason the your second example works is because it's located within the scope of a Book view. As mentioned by Tom in a reply to this answer, passing a block to link_to can be used as a replacement for the first argument (the link text).
If Book is a resource, you can get the link_to helper to generate the URL you're looking for by passing it one of the handy, automatically generated resource routes rails makes for you. Run rake routes before you try this:
<%= link_to "Edit", edit_book_path(book) %>
Otherwise, you can explicitly state what controller/action you want to link to:
<%= link_to "Edit", :controller => "books", :action => "edit", :id => book %>
Happy hacking.
EDIT: Almost forgot, you CAN add query strings bypassing them in AFTER you declare the id of the object you're linking to.
<%= link_to "Edit", edit_book_path(book, :query1 => "value", :query2 => "value")
Would product /books/1/edit?query1=value&query2=value. Alternatively:
<%= link_to "Edit", :controller => "books", :action => "edit", :id => book, :query1 => "value", :query2 => "value" %>
Try Follwing
<% link_to(:edit, :type => 'book', :id => book) do %>
...
<% end %>
or to achieve same url Use
<% link_to(:action=>'edit', :type => 'book', :id => book) do %>
...
<% end %>
Ruby doesn't know if you're sending the do ... end block to link_to or book, and is sending it to book because it is closer to the block. book do ... end returns nil, so you're left with link_to :edit, :type=>'book', :id=>nil. You will need to bracket the parameters, and while you're at it, I would rewrite it to be more understandable with a controller, action, id setup: link_to{:controller=>"books",:action=>"edit",:id=>book}do ... end
in mime_types.rb file add:
Mime::Type.register "text/application", :book