rails dynamic dropdown error with application.html.erb - ruby-on-rails

i am still new to rails and i need your help. I have a little ticketsystem. I want to show a dropdown menu in my navbar (bootstrap 3) with my different tags. This is working for the index of my tickets.
Here is a part of my application.html.erb
<li class="dropdown">
Tags <b class="caret"> </b>
<ul class="dropdown-menu">
<li>
<% #tickets.each do |ticket| %>
<% ticket.tags.each do |tag| %>
<%= link_to tag.name, tag_path(tag) %>
But when no other site is working when I add the above code to my navbar. For example when I want to open a tag i get the following error. (When i remove the above code, everything is fine.
undefined method `each' for nil:NilClass
Extracted source (around line #32): app/views/layouts/application.html.erb where line #32 raised:
Tags <b class="caret"></b>
<ul class="dropdown-menu">
<li>
<% #tickets.each do |ticket| %>
Can you help me?

I am not sure why you want to access #tickets at the application layout level(Master layout).
If you want to then, you need to set the value of #tickets in ApplicationController.
You are getting error as its currently set to nil.
You can update ApplicationController as follows to set it:
class ApplicationController < ActionController::Base
before_action :set_tickets
private
def set_tickets
#tickets = Ticket.all ## Assuming that you want all tickets from model Ticket
end
end
#tickets= Ticket.all change the query as per your requirement.

Related

Rails: Dropdown with links to show pages

I am trying to put a drop down in my navbar where users can click on links to the different stone show pages. With the code I have in now I am getting a NoMethodError private method `each' called for nil:NilClass.
I am pretty sure this private method error is coming up because I am putting this code in my navbar which is in my application.html.erb rather then in the stone model. Could someone point me in the right direction as to where I should define methods for the navbar? Or if there is something else I should be doing instead?
Here is what I have attempted so far:
application.html.erb
<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Dropdown Example
<span class="caret"></span></button>
<ul class="dropdown-menu">
<% #stones.each do |stone| %>
<li> <%= link_to stone_url do %>
<%= stone.name %>
<% end %>
</li>
<% end %>
</ul>
</div>
application_controller.rb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
helper_method :current_order
def each
#product = Product.all
end
def current_order
if !session[:order_id].nil?
Order.find(session[:order_id])
else
Order.new
end
end
end
you are calling .each on the var #stones and not on your controller, views cant call methods from controllers, unless they are helper methods. try to use a before_filter in your controller that will set your #stones and remove that each method from the controller
Just replace #stones.each to Stone.all.each.
Because there will be somewhere using var #stones, you should not define #stones on your application_controller.rb.

Rails: Public_Activity Gem - uninitialised constant

I am trying to make an app with Rails 4.
I have installed the public_activity gem.
I followed the Ryan Bates Railscast and took the controller based approach, and also the lighter Common option (as opposed to tracking the Model).
In my activities_controller I have:
class ActivitiesController < ApplicationController
def index
#activities = PublicActivity::Activity.order("created_at desc")
end
end
In my project.rb, I have:
include PublicActivity::Common
In my projects controller, create action, I have:
#project.create_activity :create, owner: current_user
In my activity view - index, I have:
<% Activities.each do |activity| %>
<div class="col-md-4">
<div class="indexdisplay">
<span class="indexheading">
<%= link_to activity.owner.name, activity.owner if activity.owner %>
</span>
<span class="indexsubtext">
<%= render_activity activity %>
</span>
In my public activity (view folder)/project/_create.html.erb, I have:
<% if activity.trackable %>
<%= link_to activity.trackable.name, activity.trackable %>
<% else %>
which has since been removed
<% end %>
When I try this, I get this error:
NameError at /activities
uninitialized constant ActionView::CompiledTemplates::Activities
I tried replacing the opening line of the activity#index so that Activities, is Activity, but it just changed the error message to:
NameError at /activities
uninitialized constant ActionView::CompiledTemplates::Activities
What does this error mean? How do I fix it?
Thank you
It seems like you use Class in your loop. Try to use your instance variable in your controller.
Change this
<% Activities.each do |activity| %>
into
<% #activities.each do |activity| %>
It should be <% Activity.find_each do |activity| %>
Model name always is singular
You cannot call each just on Model
I'd recommend you use find_each instead of each in case you have a lot of records
if you do want all the records you can always use .all method
http://apidock.com/rails/ActiveRecord/Batches/ClassMethods/find_each

Rails defining Current_page

I am trying to use a basic current_page method to define whether a navigation link should be highlighted as the current page or not.
I am not receiving any errors at the moment but am clearly not defining things properly as it's not using my CSS correctly.
In my PagesController I have the following:
def current_page
current_page = (path)
end
and I am using an if and else statement on my pages view to try and define which CSS line to use which looks like this:
<div id="nav">
<ul>
<li>
<% if "current_page" %>
About
<% else %>
About
<% end %>
</li>
</ul>
</div>
I have read quite a few forums on this but still can't seem to get it right.
Mhh you got an error in your code. With if "current_page" you will get always true, and only the first link will get rendered. You should use if current_page instead.
But rails has a build in helper, called current_page? for exactly this purpose:
Just do it like this:
<% if current_page?(path) %>
About
<% else %>
About
<% end %>
See here for more information on current_page?
The klump's answer follows your requirement, but I dare to propose a different approach:
<div id="nav">
<ul>
<li>
<%= link_to_unless_current 'About', about_path %>
</li>
</ul>
</div>
This way your About will appear as a text if current page is about_path and as a link on any other.
About

Rails tutorial will_paginate raises undefined_method total_pages

Ive been stuck for a while now in the rails tutorial. Im in section 10.3, where we are supposed to add pagination. I have added the gem 'will-paginate' to my Gemfile, and this is the index view
<h1>All users</h1>
<%= will_paginate %>
<ul class="users">
<% #users.each do |user| %>
<li>
<%= gravatar_for user, :size => 30 %>
<%= link_to user.name, user %>
</li>
<% end %>
</ul>
<%= will_paginate %>
The server raises the following problem:
ActionView::Template::Error (undefined method `total_pages' for #<Array:0xa0283fc>):
1: <h1>All users</h1>
2: <%= will_paginate %>
3: <ul class="users">
4: <% #users.each do |user| %>
5: <li>
I have been trying to search for it.. but none of the solutions to apparently similar issues worked. Any ideas?
Just scroll lower in the tutorial - the next section addresses that problem :)
The view in Listing 10.27 doesn’t work yet, though, because currently #users contains the results of User.all (Listing 10.20), which is of class Array, whereas will_paginate expects an object of class WillPaginate::Collection.
It then has you modify the UsersController to fix it-
class UsersController < ApplicationController
before_filter :authenticate, :only => [:index, :edit, :update]
.
.
.
def index
#title = "All users"
#users = User.paginate(:page => params[:page])
end
.
.
.
end
I guess you are missing an argument to will_paginate:
will_paginate #users

undefined method `css' for nil:NilClass

Im receiving this error in my Rails app and cant find the solution. I will add some code from the class where it fails. It might be a more of an architectural problem if you think so please do say and I will add more code from other classes. I basically only have this model, and one controller and another class which is located in lib. An then a view of course. Thanks in advance!
#app/models/news_api.rb
require 'open-uri'
class NewsApi
URL = "http://www.mysomething.com/partner/api/1_0/somerandomnumber/
channel/290/material/list/"
def self.download
new.document.css('news').map {|node| record(node) }
end
def document
Nokogiri::XML(open(URL))
puts URL.to_s
end
def record(node)
Story.new(node_to_hash(node))
end
def node_to_hash(node)
Hash[Story::ATTRIBUTES.collect {|attribute| [attribute, text(node, attribute)] }]
end
def text(node, selector)
node.css(selector).text
end
end
#idlefingers: Here is the code from my view. What Im doing is basically using Nokogiri to parse some xml from an API and then displaying the response in HTMl in my view.
<ul id="news">
<% #stories.each do |story| %>
<li class=" <%= story.type_of_media %>">
<h2><%= link_to story.header, story.url %></h2>
<p class="permalink"><%= link_to 'Trackback', story.url %></p>
<p class="meta">
<strong><%= story.source_name %></strong>
<br/>
<%= story.created_at %>
<br/>
<%= story.geo %>
</p>
<p class="summary"><%= story.summary %></p>
</li>
<% end %>
Where is the error coming from? At a glance, it looks like it's coming from the .document method, but it isn't clear. Can you add the error backtrace?
The .document method as it stands will be returning the result of puts URL.to_s, which will be nil. You just need to switch around the two lines in your .document method by the looks of it...
def document
puts URL.to_s
Nokogiri::XML(open(URL))
end

Resources