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 !
Related
It's a little late here so maybe this a trivial question where I'm missing something simple. But when I click a button (with link_to) I created the following gets appended to my URL:
%23<ActionView::Helpers::FormBuilder:0x3ef1fd8>
Why is this, and how can I prevent this? Again, I apologize if this is a shallow question. I can post more information regarding routes and whatnot if that is needed.
Thanks!
Edit: More information as requested.
View:
<%= link_to "Index", welcome_path(f), :class => 'button' %>
with f being part of a form_for loop. I think I'm passing the wrong parameter but I'm unsure.
Relevant Route:
get "index" => 'welcome#show', :as => 'index'
Update:
Thanks for the help everyone. I ended up getting it working by pluralizing my controller (I don't know why I didn't have that before) and utilizing welcome_url instead. That seemed to do the trick.
Check out the very first example and paragraph in the Rails API docs for ActionView::Helpers::FormBuilder:
<%= form_for #person do |f| %>
Name: <%= f.text_field :name %>
Admin: <%= f.check_box :admin %>
<% end %>
What this is saying is that f represents an instantiated FormBuilder object that you are passing to the welcome_path method in your link_to helper.
Typically, you would not mix #index and #show in your routes. Depending on what you want to use the WelcomesController for, you might actually want to route your root_path to welcome_index:
get "welcome/show" => 'welcome#show', :as => 'welcome'
root 'welcome#index'
You should run: $ rake routes in the terminal to get an idea of path view helpers that you can use in your app.
Maybe you're trying to send users to a personalized welcome page. You could have something like this for your corresponding link_to helpers would look best like this:
<%= link_to "Show", welcome_path(#user.id), :class => 'button %>
<%= link_to "Index", root_path, :class => 'button' %>
I have a view that uses link_to to pass parameters to a controller. The url is a variable. Something is not working. I'd appreciate any clues. Thanks!
<% url1 = dialogs_path(#dialogId) %>
<%= url1 %>
<%= link_to "Go!", url1(:uid1 => #uid1, :uid2 => #uid2), :id => "my_link" %>
url1 displays correctly. However, executing link_to crashes.
You should use dialog_path(#dialogId) instead.
You're trying to view a particular object, it's singular and what rails expects.
Take a look here :
http://guides.rubyonrails.org/routing.html#paths-and-urls
The way you use url confuses me, try something like this :
<%= link_to "Go!", dialog_path(#dialogId, :uid1 => #uid1, :uid2 => #uid2), :id => "my_link" %>
The url1 should be defined as:
<% url1 = dialogs_path(#dialogId)+'?uid1=' + #uid1 + '&uid2=' + #uid2% , :id => "my_link" %>
And the link_to should be:
<%= link_to "Go!", url1, :id => "my_link" %>
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})
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)) %>
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.