I have a dropdown list having links to specific categories, in each line of category I want it to include name of category and number of books of that category. Anyway, I want the number part having a different style in comparison with name part.
Here is the code for each one:
%li= link_to "#{category.name} (#{category.books.count})" , root_path(category_id: category.id)
I want to set id for #{category.name} and (#{category.books.count}), what should I do?
You should define the attribute id. In your code, you should do it like:
%li= link_to "#{category.name} (#{category.books.count})" , root_path(category_id: category.id), id: "#{category.name} (#{category.books.count})"
I found the answer, we should use link_to do like this:
= link_to root_path(category_id: category.id, per_page: params[:per_page]) do
.row.cate-display
#cate-name= category.name
#cate-count= "(#{category.books.count})"
Related
In Rails, I have a "notifications" class, one field of which is "link". The links contained within this class are formatted like: exchange_path(6), where that is the path to the show action in the exchange controller.
I'm now trying to output this link as such:
<%= link_to "View Exchange", notification.link %>
This line is in a loop which begins as such:
<% #notifications.each do |notification| %>
When I click this link, it takes me to localhost:3000/users/exchange_path(6) instead of localhost:3000/exchanges/6 like I would expect. (The loop generating the faulty link is on localhost:3000/users/2)
this could be scary...
<%= link_to "View Exchange", eval(notification.link) %>
should evaluate and use the path helpers. but you need to be 100% sure that nothing bad gets put in the link field..
You could do this:
<%= link_to("View Exchange", "/#{notification.link.gsub('(', '/').gsub(')', '').gsub('_path', 's')}") %>
or set up a method in your model that formats it for you:
def format_link
link.gsub('(', '/').gsub(')', '').gsub('_path', 's')
end
and just call that in your link_to:
link_to("View Exchanges", notification.format_link)
This will only work if all the links are formatted exactly as the example in the question
How do I define a method the attribute of a model.
I have a Picture model that has a title.
Picture.title = "Some title #with a few #hashtags"
I want to make those hashtags links to the tag#show
Picture.title.with_links = "Some title <%= link_to "#with", tag_path(tag) %> a few <%= link_to "#hashtags", tag_path(tag) %>"
Whats the best way to do this. Where do I define the method(with_links)? in Picture.rb? or Pictures_helper.rb?
In short, Rails model does not have access to routes. In very rare cases it is possible to use routes from within model, however in this case it is not the right place to do so.
The right place for with_links is PicturesHelper, so that it would be accessible in view via with_links(picture). The declaration would be:
def with_links(picture)
out= "Some title "
out += picture.tags.collect do |tag|
link_to(tag.name, tag_path(tag))
end.join(", ")
raw out
end
I am using Haml and have a header that contains a link to another page like so:
.heading
= link_to community.community_tag_path(community_tag) do
This ultimately renders a link.
I need to embed the same link as used in the header into a generic span tag like so:
%span View all Articles
How do I use this as a link using haml? Basically, I need the same link as in the header to work with the span
Yes you can pass a do block to the link_to function:
= link_to community.community_tag_path(community_tag) do
%span View all Articles
Or if you want the span to wrap the link_to:
%span= link_to "View all Articles", community.community_tag_path(community_tag)
To display the link with the community tag's name, simply do:
%span= link_to community_tag.name, community.community_tag_path(community_tag)
Here is some documentation about link_to: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
Following the comments, to display links like "View all [community tag's name] articles", you can do a string interpolation:
"View all #{community_tag.name} articles"
And add this string as the first argument of the link_to.
You can achieve this by the following:
.heading
= link_to community.community_tag_path(community_tag) do
%span View all Articles
Also you can use a method inside of a string. But I prefer use directly like in the pasts comments.
%a{href: "#{community.community_tag_path(community_tag)}"} View all Articles
I know the title of this question might be strange, but that was the best way I could explain it in words. Here is the case:
I have made a simple Rails app which has a table called "Teams". There are entries in this table which I want to iterate through and list the names of all the teams. Here is the code I have written for my controller and my view:
teams_controller.rb
def index
#teams = Team.all
respond_to do |format|
format.html
end
end
index.html.haml
%h3
Listing teams
= #teams.each do |team|
%p= link_to team.name, team
%p= link_to 'Add new team', new_team_path
The output correctly iterates through all the teams, but then also throws out a strange dump of the entire contents of #teams. Here is a screenshot:
The kicker is, previously when I had this written out using the default erb standard, it was all good. I decided to use haml instead, and this seems to be the only problem that has popped-up and I can't figure out for the life of me why it is.
Would love some help.
Thanks.
Remove the = on the beginning of each line (line 4) and replace it with -. You're outputting the return of each (which is every row).
Like this:
%h3
Listing teams
- #teams.each do |team|
%p= link_to team.name, team
%p= link_to 'Add new team', new_team_path
In a haml file I have an element such as the following:
%th Movie Title
that I am trying to turn into a clickable element that will sort the column it is the header for. So far I've worked out that this may look something like the following
%th= link_to "Movie Title", "foo"
except I need to set a parameter and instead of going to "foo" I want to just reload the current page with the list of movies sorted in the controller (though separate research, my guess is that this can be done via something like:
def index
#movies = Movie.find(:all, :order => params[:sort])
end
Can someone give me some advice on what I should do about the link_to call? Is anything I've written above way off? Thanks.
Here you go :)
= link_to 'Movie Title', request.parameters.merge({:sort => "title ASC"})