Rails: Displaying pages navigation inside application layout - ruby-on-rails

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.

Related

Couldn't find Zone without an ID

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 %>

How do I check CanCan abilities on an object in a `shared/partial`?

Per the CanCan documentation, to check if a user has the ability to do something to any element in the view, you do something like this:
<% if can? :create, #project %>
<%= link_to "New Project", new_project_path %>
<% end %>
Or you can check with the class like:
<% if can? :create, Project %>
<%= link_to "New Project", new_project_path %>
<% end %>
In my case, I have a DashboardController#Index, that has this:
#nodes = current_user.nodes.where(:is_comment => nil)
In my views/dashboard/index.html.erb, I have this:
<% #nodes.each do |node| %>
<!-- Upload Video Comment Popup -->
<div class="box">
<%= render partial: "shared/comments", locals: {node: node} %>
</div>
<% end %> <!-- node -->
Then in my shared/_comments.html.erb, I have this:
<% if node.comments.present? %>
<% node.comments.each do |comment| %>
<% if can? :manage, Comment %>
Show Something Interesting Here
<% else %>
Show something boring here
<% end %>
<% end %>
<% end %>
That doesn't work.
I also tried this:
<% if node.comments.present? %>
<% node.comments.each do |comment| %>
<% if can? :manage, comment %>
Show Something Interesting Here
<% else %>
Show something boring here
<% end %>
<% end %>
<% end %>
And that doesn't work either.
This is my ability.rb
can :manage, Comment, user_id: user.id
I thought about creating a #comments instance variable in the controller, but the issue with that is that the comments are on a collection of nodes (i.e. I need to show multiple nodes, and each node has multiple comments).
How do I approach this?
Your last code should work after updating to CanCanCommunity version of cancan
<% if node.comments.present? %>
<% node.comments.each do |comment| %>
<% if can? :manage, comment %>
Show Something Interesting Here
<% else %>
Show something boring here
<% end %>
<% end %>
<% end %>
related to How do I show an error for unauthorized can can access

Click count the link article in rails

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 %>

cannot display the view from the url parameter in ruby

I was trying to display a list of the blog with the same category which display on the url parameter. however when i click the link it still stay at the same page.
route
match '/microposts/:category', :to => 'microposts#category_list'
view
<h2>sidebar</h2>
<% #categories= Micropost.select("category").group("category")%>
<% unless #categories.nil? %>
<ul><% #categories.each do |category| %>
<li><%= link_to category.category, :controller =>"microposts", :category => category.category, :method => 'category_list' %></li>
<% end %>
</ul>
<% end %>
After click the link enter category_list view.
category_list.html.erb
<h2>Catergory</h2>
<% #microposts.each do |post|%>
<article>
<h4><%= link_to post.title, post %>| <%= post.created_at %></h4>
</article>
<% end %>
<%= will_paginate #microposts %>
microposts controller
def category_list
#micropost = Micropost.select("category").where(params[:category])
#title = "Category"
end
Ok, try this:
UPDATED:
microposts_controller.rb
def category_list
#microposts = Micropost.where('category = ?', params[:category])
#categories = Micropost.select('DISTINCT(category)').map(&:category).compact
...
end
routes.rb
match '/microposts/:category' => 'microposts#category_list', :as => :microposts_category_list
_sidebar.html.erb
<h2>sidebar</h2>
<% unless #categories.empty? %>
<ul>
<% #categories.each do |category| %>
<li><%= link_to category, microposts_category_list_path(category) %></li>
<% end %>
</ul>
<% end %>

Rails Geocoder - Still learning

I'm sure it's a simply issue due to me not fully understanding how bits fit together in Rails...
I've followed the rails cast but I'm having trouble implementing it into my app (I've had it working stand-alone).
The error I get is
undefined method `nearbys'
Here's what I've got:
user.rb
geocoded_by :full_address
after_validation :geocode
def full_address
[address1, address2, address3, city, country, postcode].compact.join(', ')
end
users_controller.rb
def index
#title = "All users"
if params[:search].present?
#users = User.near(params[:search], 50, :order => :distance)
else
#users = User.all
end
end
index.html.erb
<h3>Nearby locations</h3>
<ul>
<% for user in #users.nearbys(10) %>
<li><%= link_to user.address1, user %> (<%= user.distance.round(2) %> miles)</li>
<% end %>
</ul>
_sidebar.html.erb
<%= form_tag users_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search Near", :name => nil %>
</p>
<% end %>
Thanks
If I comment out the .nearbys
<% for user in #users#.nearbys(10) %>
<li><%= link_to user.latitude, user %> (<%= user.distance.round(2) %> miles)</li>
<% end %>
The search works. Could this be a problem with the install of geocoder?
The function nearbys is a function on your model, not on a collection of models. The variable #users contains a collection of User models. You need to call the function on a single model instance, for example for each user in #users.
As an example, but not sure if you really want this:
<% #users.each do |user| %>
<% user.nearbys(10).each do |near_user| %>
<li><%= link_to near_user.latitude, near_user %> (<%= near_user.distance.round(2) %> miles)</li>
<% end %>
<% end %>
(Note that I also changed the for user in #users to use #users.each do |user|, which is more "Rubyish".)

Resources