anchor tag in link_to - ruby-on-rails

Found it difficult to frame the question, here's my problem.
alert.html
<div class="alert_text">
<%= link_to "Comment", :action=>:show, :id=>lp.id %>
</div>
when I click on the link it takes me to show page
show.html
table is displayed at beginning of the page after that, below part of code.
<div id="comments">
<%= render :partial=>'comments' %>
</div>
what I need is, when I click on link Comment it must load show page as normal but should direct to comments part of page.
Edit: Just like as it happens in this stackoverflow.com, on top left StackExchange when you click on inbox message.

It sounds like what you want is the anchor option with a path helper. Assuming you're using RESTful routing, you should be able to do something like:
<%= link_to "Comments", my_model_path(lp, anchor: 'comments') %>
You would need to change my_model to the whatever your resource is called. So if you have resources :articles in routes.rb, it would be article_path(lp, anchor: 'comments')

Give your comment div an ID and use it link this:
<%= link_to "Comment##{lp.id}", :action=>:show %>
If the div comment has your ID, it will straight go to that spot.

Related

New to rails... How do I set and call specific anchor points for a single page

So I want to create a single page portfolio, and I want to set anchor points so users can click on the nav bar and it jumps to the specific section of the page. So far I have this to set my anchor point:
<%= link_to "Code Snippets", anchor: "code_snippets" %>
<%= link_to "See My Web Apps", anchor: "web_apps" %>
<%= link_to "Contact Me", anchor: "contact" %>
<%= link_to "Resume", anchor: "Resume" %>
I dont know if this is the right syntax, but this is what I have found online so far...
Now, how do I call these anchor points, so my page jumps to the relevant information when the user clicks the link? Thanks in advance!
Just need to add the html below to jump to the anchor point when you click.
<a name="Results"></a>
or
<div id="Results"></div>
so if the url for this:
<%= link_to "Code Snippets", anchor: "code_snippets" %>
is /code_snippets
you would hit this url
/code_snippets#my_anchor

Rails link from database appends to current controller

I have a model, Notification, that has two fields: text and link. In my view for notifications, I have the following:
<% #notifications.each do |notification| %>
<li>
<%= notification.text %>
<%= link_to "View", notification.link %>
</li>
<% end %>
Examples of links include:
"foos/4/bars"
"about"
"foos"
However, when I attempt to follow the link, if I am in the "baz" controller, the result is an attempt at "baz/foos/4/bars", or "baz/about", rather than just "foos/4/bars" or "about".
Is there a better way to do this, or a way to disable the appending of the link to the current controller?
You trying to get a relative path to your current controller.
Try doing this ->
<%= link_to "View", "/" + notification.link %>
Thanks to #Kumar Abinash. The path was relative without a prepending "/". Simply changed links in the database to "/..."

rails link_to with block, controller options and html post

I am trying to use link_to in rails 4 with controller and html options and a do..end block. I have seen similar posts but have not been able to use any of the answers successfully.
Working code without a do..end block:
<%= link_to 'recommend', { controller: 'recommendations', id: offer.id }, method: :post %>
When I try to use some embedded ruby to add extra information to the link, I cannot get it to work:
<%= link_to( { controller: 'recommendations', id: offer.id }, method: :post) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
The code compiles but in the rendered, the link that is generated is the following:
<a controller="recommendations" id="38">
<p>Some Html</p>0
</a>
Any help would be appreciated. I think that it is a small problem with the syntax but I have tried all manner of brackets, spaces etc that I could think of without luck.
UPDATE: I have tried the following code without success:
<%= link_to( { controller: 'recommendations', action: 'create', id: offer.id }, method: :post) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
The HTML output is:
<a action="create" controller="recommendations" id="39">
<p>Some Html</p>0
</a>
This might not be important but as a side note, the create action doesn't have a helper function for links. When I run the
rake routes
command I get the following
...
recommendations GET /recommendations(.:format) recommendations#index
POST /recommendations(.:format) recommendations#create
new_recommendation GET /recommendations/new(.:format) recommendations#new
...
In my opinion this isn't a problem but it is a reason why code such as:
link_to create_recommendation_path
won't work. Finally, the intention of the link is to act as a 'like' button. It creates a recommendation and then displays the current page again. Once again, thanks for the help in advance.
The reason link_to create_recommendation_path doesn't work is because there is no named route for create_recommendation_path, only for recommendations_path. You can see the named routes in your routes list (which you have in your post above). The left most column that comes out of routes shows the named routes. Notice that recommendations#create doesn't have an entry in the list.
You could probably get the path you want with
<%= link_to recommendations_path(:offer_id => offer.id), :method => :post do %>
html stuff
<% end %>
This should post to a path that looks like
/recommendations?offer_id=<the offer id>
(except the post data will be in the headers not on the URL)
This will work if the create method going to do something like
Recommendation.create(params)
and the only parameter you need to create a new Recommendation is an offer_id
What I don't understand is why you're trying to POST with a link? Does creating a recommendation only require an offer id?
In your link_to you're only specifying a controller, you need to also specify the action otherwise it doesn't know where to route it to. Either use:
<%= link_to({ controller: 'recommendations', action: 'show', id: offer.id }) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
Or
<%= link_to({ show_recommendations_path(id: offer.id) }) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>

create large a tag in rails

I have this tag: <%= link_to 'Show', user_listing_url(listing.user, listing) %> but instead of simply having it say 'Show' I actually want to place HTML inside of the <a> tag. Is this possible?
Example:
<div><div><img /></div></div>
yes you can pass a block to link_to
try something like this:
<%= link_to(user_listing_url(listing.user, listing)) do %>
<div><div><img/></div></div>
<% end %>
I totally recommend marflar's answer above.
However I would add one comment which is that if you are using html elements within a link_to block this may apply rails default link styling which may not be desirable.
One alternative is to use a button_to link but don't forget the default method for this is POST so specify the options as GET:
button_to(user_listing_url(listing.user, listing), method: :get) do %>
<div></div>
<% end %>

How do you get this div to wrap around an image link?

I have this image link:
<%= link_to image_tag(comment.user.profile.photo.url(:tiny)), profile_path(comment.user.profile), :class => "comment_image" %>
and I want to wrap a div containing 1. text and 2. a list with a link and text around that image link. I want the image to be on the left, and the div to be on the right wrapping around the image.
Assuming you don't need any of the fancier features offered by the link_to helper, the easy answer is to just use an anchor tag directly.
<a href="<%= profile_path(comment.user.profile) %> class="comment_image">
<div>
Some stuff -- whatever
<%= image_tag(comment.user.profile.photo.url(:tiny)) %>
Some more stuff -- ya know...
</div>
</a>
would you care if i posted it in HAML(same thing as erb just without the <% %> and closing tags:(sort of pseudo code for html)
%ul
%li
= link_to image_tag(comment.user.profile.photo.url(:tiny)), profile_path(comment.user.profile), :class => "comment_image"
%div.user-comments
comment
username etc
%li
rinse-repeat
AND dont forget to clear your float on the li!
then in your css, just float comment_image and user-comments left.

Resources