I'm having a helper for displaying the tags of a post
# Show action
%p Tags #{ link_tags #post }
# Helper
def link_tags post
raw post.tag_list.map{ |t|
link_to t, posts_path(tag: t.name)
}.join(', ')
end
However I'm getting an error on screen
undefined method `name' for "ruby":String
How can I fix this? As a side note the code on my sidebar (where I list all post-tags) where I tried to copy from works fine
- tag_cloud Post.tag_counts, %w(tag) do |tag, css_class|
= link_to tag.name, posts_path(tag: tag.name), :class => css_class
post.tag_list will give you an array of tags as strings.
What you are looking for is probably post.tags which should return an array of ActsAsTaggableOn objects.
Here you have tutorial about tags on posts
Related
I am trying to create helper method that displays the navigation. How ever my helper displays an array even though i am using content_tag. Was wondering if i was doing something wrong.
module SubscriberNavigation
def navigation
get_menu_items.find_each.map do |menu|
content_tag(:li, link_to("#{ menu.title.try(:capitalize) }", "#{ menu.url.downcase }"))
end
end
def get_menu_items
#get_menu_items ||= Subscriber::Menu.all
end
end
And when i display
<%= navigation %>
An array of records in being displayed. Instead of content_tag list items.
["<li>Contacts</li>", "<li>Terms and conditions</li>", "<li>About us</li>"]
I tried .html_safe etc but looks like i'm missing something.
It is returning an Array. You can try this:
<%= navigation.join(' ').html_safe %>
I think this one is a little hard to explain by the title alone, so here's some I code I came up with:
Rails View Helper
module SplashHelper
def send_link_or_tag(link=true)
if link
link_to nil, root_path, class: 'to-block'
else
content_tag :div, 'The content'
end
end
end
View (haml) that uses the Helper
- 5.times do |i|
- if i%2 == 0
= send_link_or_tag do
-#THE PROBLEM IS THAT I CAN'T ADD CONTENT TO THE
RETURNED link_to (<a> tag) in this case the <p> tag
INSIDE THIS BLOCK!
%p = 2 + 2
- else
= send_link_or_tag false do
-# SAME PROBLEM HERE.
%p = 3 * 3
In summary, the Helper successfully returns a link_to or a content_tag, but I need to keep concatenating or adding more tags inside the tag returned by the Helper (through a block).
It seems this should be easy to do in Rails, What am I missing?
Thanks in advance!
Try this for your helper method,
def send_link_or_tag(link=true)
if link
link_to root_path, class: 'to-block' do
yield
end
else
content_tag :div do
yield
end
end
end
This will yield the content in an a or div tag from the block defined in your view.
Sorry if this sounds obvious but i'm getting confused.
I'm trying to build a navigation list helper.
At the moment it receives a list of links from an array.
module ApplicationHelper
def site_pages
%w{index about contact promotions}
end
def nav_builder list
list.map do |l|
content_tag :li do
content_tag :a, :href => "#{l}_path" do
l
end
end
end
end
end
But when I run the page it ouput everything is the page as an array.
[<li>index</li> <li>about</li> <li>contact</li> <li>promotions</li>]
instead of displaying a list of links.
Is there anything special to do?
==EDIT==
I call my builder this way:
<%= nav_builder site_pages %>
The helper is expected to return a String of HTML, rather than an Array. Try joining the Array elements into a single String:
def nav_builder list
list.map do |l|
content_tag :li do
content_tag :a, :href => "#" do
"test"
end
end
end.join("\n").html_safe
end
I want to display product count in a link_to, the link_to is a part of partial displayed in application.erb.html, the problem is, I have a method in my application controller named products_on_cart which return products count, when I try this code:
<%= link_to "<%= products_on_cart%>", :controller=>"carts", :action=>"index"%>
rails give me an error:
"syntax error, unexpected '>'
...er=>"carts", :action=>"index"%>"
I don't really understand why, can somebody help me?
You can't use <%= .. %> inside of <%= .. %>.
<%= link_to products_on_cart, [:carts] %>
You're nesting ERb tags. Make sure products_on_cart() is available as a helper method, then rewrite your link_to code without nested ERb tags as follows:
<%= link_to products_on_cart(), :controller => "carts", :action => "index" %>
To make products_on_cart() a helper method, either move it to app/helpers/application.rb, or declare it as a helper in your controller:
def products_on_cart()
# method definition goes here
end
helper_method :products_on_cart
If you only need to access products_on_cart from your views and not from your controllers, putting it in app/helpers/application.rb is the preferred way to go. If you need to use it in both controllers and views, use the helper_method approach above instead.
I have the tag functionality working ok but can't generate a tag_cloud
in my controller:
def tag_cloud
#tags = Article.tag_counts # returns all the tags used
end
in the view:
<% tag_cloud Article.tag_counts.sort { |x, y| x.name <=> y.name }, %w(x-small small normal large x-large) do |tag, css_class| %>
<%= link_to tag.name, tag_url( :tag => tag.name ), :class => css_class %>
<% end %>
I always get a undefined method error for tag_cloud
You can't call controller methods from the view. Try putting it in a model or passing it to the view from the controller.
If this isn't helpful enough, try editing the question and including more details such as the full definition of tag_cloud, explain why you're setting #tag but not using it, etc.
That code doesn't look like it'll do all you want, but to remedy the undefined method error, the proper place for auxiliary methods for views is in the helper, so move the method tag_cloud there instead.
You'll find it in app/helpers/controllername_helper.rb.
tag_cloud defined in module TagsHelper. You need to include it in corresponding helper:
module ApplicationHelper
include TagsHelper
end
Also there is no need in controllers tag_cloud
sergeykish.com is correct, you just need to include the helper in /app/helpers/application_helper.rb
module ApplicationHelper
include TagsHelper
end