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
Related
<%= link_to (:controller => "company_stuff", :action => "index", :anchor => :menu), :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
I am having difficulty linking a page which is on a different controller and also the link is an anchor. Basically the controller is called company_stuff the action is index and the anchor is called #terms
The problem was that the :controller :action :anchor was not being passed through as a hash, separate from the CSS class
Below is the solution
<%= link_to "Terms Of Use", {:controller => "company_stuff", :anchor => "terms"}, :class => "links" %>
I believe you can try something like this
<%= link_to index_company_stuff_path + "#terms", :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
Or
<%= link_to index_company_stuffs_path + "#terms", :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
Depending on your controller name and route.
You can find more information on this question How to create an anchor and redirect to this specific anchor in Ruby on Rails
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"
When I use:
link_to "Title", {:controller => 'my', :id=>1, :param=>2}
It produces:
<a href="/my/index?id=1¶m=2">
But I want the following:
<a href="/my?id=1¶m=2">
What is the best way to achieve this?
I use Rails 3.1.0
In general, it is best to use named routes, that you can later call as methods.
Example:
In routes.rb:
match '/my/:id/:param' => 'my#index', :as => :my
In a view:
link_to 'anchor', my_url(:id => 2, :param => 3)
Other than Achilles answer You can also give string parameter to link_to function
<%= link_to "Title", "/my/index?id=1¶m=2" %>
You can also give string parameter to link_to function
<% id, param = 1,2 %>
<%= link_to "Title", "/my/index?id=#{id}¶m=#{param}" %>
I'm using link_to in RoR 3
When I use it like this, it works fine:
<%= link_to "Add to your favorites list",:controller =>
'favourite_companies', :action =>'create',
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}" %>
But I would like to pass in a class as well
however, this is not working for me. The class works, but it breaks the link. Any ideas?
<%= link_to "Add to your favorites list",{:controller =>
'favourite_companies', :action =>'create'},
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
:class=>"ui-button-text button_text"} %>
<%= link_to "Add to your favorites list",{:controller =>
'favourite_companies', :action =>'create'},
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
:class=>"ui-button-text button_text"} %>
try this
<%= link_to "Add to your favorites list", :controller =>
'favourite_companies', :action =>'create',
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
{ :class=>"ui-button-text button_text" } %>
Since the :class should be in :html_options (refering to API)
link_to(body, url, html_options = {})
The proper way of doing what you have is as follows:
link_to "Foo", { URL_FOR PARAMS HERE }, :class => "bar"
As far as setting the controller and action manually like this, well, it's crap. Rails builds url helpers for you; use them and save yourself some time, energy, and add clarity, all at once:
link_to "Foo", favourite_companies_path(#company), :method => :post
What you're doing with the string interpolation is a bad idea too…it's just wasteful and cluttered for no reason at all. The following is the same, just better:
link_to "Foo", :company_id => #company.id, :company_name => #company.name
As far as why your link wasn't working, if wrapping it in a div helped it sounds like you have a problem with your HTML structure, not the link_to syntax.
I'm using a link_to do-end block so the above previous solutions didn't work for me.
If you want to embed other tags in your a tag, then you can use the link_to do-end block.
<%= link_to favourite_companies_path(:company_id => #company.id, :another_url_param_here => "bar"), { :class => "ui-button-text button_text", :title=> "We can have more html attributes as well" } do %>
<i class="fa fa-star"></i>
<%= #company.company_name %>
<% end %>
In this case it's
<%= link_to path(url_params), html_options = {} do %>
<% end %>
Be careful because in Rails 5 the above methods will still result in a wrong URL generation. The controller and action need to be put in a literal hash in order for it to work in Rails 5. What you will have should be something like this
<%= link_to "Add to your favorites list",
{ controller: "favourite_companies", action:"create"},
company_id: #company.id,
company_name: #company.company_name,
class: "ui-button-text button_text" %>
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