This doesn't give the full URL, which breaks as it's in a mailer
<%= link_to #conversation.title, conversations_path %>
This gives me the full URL, which is good:
<%= conversations_url(:only_path => false) %>
How do I get the best of both worlds? I want to link_to but have the full path?
Thanks
Without testing this.. Have you tried this?
<% conversations_url(:only_path => false) do %>
#conversation.title
<% end &>
Thanks Oluf, ran into your answer when I was trying to fix a similiar problem.
The link may be rewritten into:
<%= link_to( #conversation.title, conversations_url( :only_path => false)) %>
Related
I want to get this url:
blog.example.com/12
i have this:
<%= link_to post.title, root_url(:subdomain => "blog") %>
this gives me blog.example.com, how can I add the 12 at the end that is the post.id
Edit:
I found a way to fix this, but to me it looks like a hack, this is what I did:
<%= link_to post.title, root_url(:subdomain => "blog")+post.id.to_s %>
If anyone finds a more elegant solution, let me know !
Just upgraded to rails 3.1 and now my form_tag doesn't work anymore, I don't get any errors at all?
<% form_tag({:action => 'search'}, :remote => true) do %>
<%= select_tag "prod_id", options_for_select(["-"]) %>
...
<% end %>
Have something dramatically changed so I need to change my code?
Thanks in advance
Code blocks in your views (like form_for, for instance) now need to use the <%= %> syntax instead of <% %>.
Change the first line of your code to look like this:
<%= form_tag({:action => 'search'}, :remote => true) do %>
and you should be good to go.
As a note, I think this change actually came about in one of the Rails 3.0 betas. Check out http://asciicasts.com/episodes/208-erb-blocks-in-rails-3 for a bit of documentation on it.
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" %>
What is the best way to go about getting embedded HTML in the body of a link generated with the link_to method?
I basically want the following:
This is a <strong>link</strong>
I have been trying to go about this as suggested in Rails and the <span> tag but with no luck. My code looks like the following:
item_helper.rb
def picture_filter
#...Some other code up here
text = "Show items with " + content_tag(:strong, 'pictures')
link_to text, {:pics => true}, :class => 'highlight'
end
item_view.html.erb
#...
<%=raw picture_filter %>
#...
Try it this way
<%= link_to(raw("a <strong>strong</strong> link"),{:pics => true},{ :class => 'highlight'}) %>
= link_to "http://www.example.com" do
<strong>strong</strong>
As of 2016 I prefer this method.
<%= link_to my_path do %>
This is a <strong>ape</strong>
<% end %>
you can use html_safe
<%= link_to ("<i class='someIcon'></i> Link").html_safe %>
Not sure if this is the best way.
But I have been very successful in staking alot of the view helpers inside the content_tag call.
It also might not hurt to call a .html_safe
link_to(content_tag(:span, "Show yada " + content_tag(:strong, "Pictures")), {:pics => true})
I have been struggling with a problem in Rails for a couple of days and still could not find the solution. Could you help me with that?
Problem: I have a search box that puts a :search_string entry in the params structure. I use a form_tag for that and it works fine.
<% form_tag :controller=> 'items', :action => 'find' do %>
<%= text_field_tag :search_string, params[:search_string] %>
<% end %>
The problem is when I want to add and update other params key-value (in another view), for instance :start_date, to filter the search_string result. Here is the code snipped that I use in the view:
<% form_tag :controller=> "items", :action => "find", :params => params do %>
<%= hidden_field_tag :date_start, '2010-04-01' %>
<%= submit_tag 'April' %>
<% end %>
<% form_tag :controller=> "items", :action => "find", :params => params do %>
<%= hidden_field_tag :date_start, '2010-03-01' %>
<%= submit_tag 'March' %>
<% end %>
When I first click on "April" submit button, then the params is correctly passed to the controller (i.e. there is a params[:start_date]='April'). However when I try to click "March" button afterwards, the params[:start_date] is not updated. I definitely think this is a stupid newbie mistake, but I cannot figure out how to properly use the form_tag. Could you tell me if I am doing something work? Otherwise, could you advise me which is the best way to update the params using form_tag's ? Thank you very much in advance.
Miquel
What you may want to do is instead force-merge the parameters, something along the lines of:
<% form_tag :controller=> "items", :action => "find", :params => params.merge(:date_start => '2010-03-01') do %>
<%= submit_tag 'March' %>
<% end %>
There is a chance you're inadvertently submitting two of the same parameter and the first of them is getting picked, but since the "first" is not clearly defined, you may get inconsistent results.
Have a look in your log file to see what parameters are received from the two forms.