just trying the link_to image_tag helper, and I can't get it to work.
Can someone please tell me whats wrong?
Controller
class PagesController < ApplicationController
def home
#categories = Category.all
#zone = Zone.find(params[:id])
#zones = Zone.all
#photos = Photo.landing
end
end
View
<% #zones.limit(8).each do |zone| %>
<%= link_to image_tag "category-box_#{zone.id}.jpg", zone(zone.id) %>
<% end %>
Update
I found the Id problem came from the controller. Now the error is:
undefined method `symbolize_keys' for 1:Fixnum
Thanks!
You need to wrap your image_tag in brackets. Your code should look something like this:
<% #zones.limit(8).each do |zone| %>
<%= link_to image_tag("category-box_#{zone.id}.jpg"), spots_by_zone_path(zone.id) %>
<% end %>
OR
You can simply use block form of link_to
<% #zones.limit(8).each do |zone| %>
<%= link_to spots_by_zone_path(zone.id) do %>
<%= image_tag "category-box_#{zone.id}.jpg" %>
<% end %>
<% end %>
Related
I want to do the following:
<% if current_user.has_role? :demo %>
<%= link_to profile_path(#selected_profile) do %>
<% else %>
<%= link_to profile_path(profile) do %>
<% end %>
What's throwing it off is the beginning of the block in the link_to, within the if statement.
So how do I achieve this without having to duplicate ALL of the code within this if block twice?
Edit 1
This is the error I get from the above code:
SyntaxError at /
syntax error, unexpected keyword_else, expecting keyword_end
'.freeze; else
^
You can do it like this:
<% chosen_profile = current_user.has_role?(:demo) ? #selected_profile : profile %>
<%= link_to profile_path(chosen_profile) %>
So this will not repeat your link_to tag which you need to do. As you have to redirect to the same path and just change the profile object then this will work. You may change the ternary to if else block if the line seems very long and not readable.
And as everyone mentioned that don't use a do after link_to until you need a block. So that will fix your error.
You can achieve this by defining method in you user.rb (Model)
def demo?
self.has_role?("demo")
end
Then you write in your view
<% if current_user.demo? %>
<%= link_to profile_path(#selected_profile) %>
<% else %>
<%= link_to profile_path(profile) %>
<% end %>
This may help you.
do should have end.
Here is the Ruby Doc reference for link_to
Here is more about do in Ruby
<% if current_user.has_role? :demo %>
<%= link_to profile_path(#selected_profile) do %>
selected profile
<% end %>
<% else %>
<%= link_to profile_path(profile) do %>
profile
<% end %>
<% end %>
You are getting error because of do, you are opening the block but not closing it, try this code
<% if current_user.has_role? :demo %>
<%= link_to 'Profile', profile_path(#selected_profile) %>
<% else %>
<%= link_to 'Profile', profile_path(profile) %>
<% end %>
or, you can do it in controller instead
#selected_profile = current_user.has_role?(:demo) ? #selected_profile : profile
and then in the view,
<%= link_to 'Profile', profile_path(#selected_profile) %>
Hope that helps!
I was testing out a blog. If a user posts in the blog, when they go to the index, it shows all the users name. But if the user logs out and go to the index page, they get an error.
undefined method `user' for nil:NilClass
It seems to not show the other users post unless he or she is logged in or this error comes. Why is that happening? Any help would be appreciated.
index.html
<% #posts.each do |post| %>
<%= image_tag(post.user.avatar.url(:thumb)) %>
<%= link_to post.user.name, #post.user %>
<% end %>
Change
<%= link_to post.user.name, #post.user %>
to
<%= link_to post.user.name, post.user %>
Try this
<% #posts.each do |post| %>
<% unless post.user.blank? %>
<%= image_tag(post.user.avatar.url(:thumb)) %>
<%= link_to post.user.name, post.user %>
<% end %>
<% end %>
Hope this helped!
How can I check if the Spree cart is empty, so I can change the button if it isn't?
I thought there was a method line_items.count...
<% unless line_items.count > 0 %>
<%= link_to "<button>Empezar Pedido</button>".html_safe, "/shop" %>
<% else %>
<%= link_to "<button>Terminar Pedido</button>".html_safe, "/shop" %>
<% end %>
Thanks!
This is very simple I solved my this problem like this
In application_controller.rb
def load_cart
#order = current_order
end
In frontend in you case
<% unless #order.line_items.count > 0 %>
<%= link_to "<button>Empezar Pedido</button>".html_safe, "/shop" %>
<% else %>
<%= link_to "<button>Terminar Pedido</button>".html_safe, "/shop" %>
<% end %>
Thanks
I'm trying to display a page navigation but Rails in addiction to page_title outputs all the record's informations inside brackets like this:
- About [#<Page id: 1, page_title: "About", page_content: "About page content", created_at: "2013-05-04 06:38:03", updated_at: "2013-05-04 06:38:03">]
- Other page [#....etc...]
How I can fix this? Thx.
application.html.erb
<li>
<%= #pages.each do |p| %>
<%= link_to page_path(p.id) do %>
<%= p.page_title %>
<% end %>
<% end %>
</li>
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :set_layout_variables
def set_layout_variables
#pages = Page.all
end
end
remove the = in your line <%= #pages.each do |p| %>
It should be
<li>
<% #pages.each do |p| %>
<%= link_to page_path(p.id) do %>
<%= p.page_title %>
<% end %>
<% end %>
</li>
<%= will output the result of the line (in your case, the loop), which displays the page record. That's why you see the record in your output.
I have view
<% #r.each do |g| %>
<%= link_to g.title %>
<% end %>
When I click the link i won't to display the count how many people's click this link.
Like this:
<%= link_to g.title %> Click:<%= countclick %>
Title Click:45
Article should have countclicks:integer database column.
Than in article show method (I think you use this method to show article):
def show
#article = Article.find(params[:id])
count=#article.countclicks+1
#article.update_attributes(:countclicks=> count)
#other code
end
And than in index view:
<% #articles.each do |article| %>
<%= link_to "#{article.name}", show_article_path(article)%>
<%=article.countclick %>
<% end %>
Or alternatively you can do this via model callbacks.
If you want to use link_to like this, you have to give it a block:
<% link_to g.title do %>
Click <%= countclick %>
<% end %>