NoMethodError in Browse#index - undefined method for nil:NilClass - ruby-on-rails

I created a new controller called browse and this is what I have inside it
class BrowseController < ApplicationController
def index
#hashtags = Hashtag.all
end
end
I'm trying to display all of the hashtags in view using this (views\browse\index.html.erb)
<ul>
<% #post.hashtags.each do |h| %>
<li><%= h.hashtags %></li>
<% end %>
</ul>
Here's the error that I'm getting
NoMethodError in Browse#index
undefined method `hashtags' for nil:NilClass
My routes.rb
get'/browse' => 'browse#index', :as => :index
How can I fix this error to display the list in browse.html.erb?
And if I wanted to display them in descending order of date, where do I need to put that?

You should use code in index view as follows:
<ul>
<% #hashtags.each do |h| %>
<li><%= h.{field_name} %></li>
<% end %>
</ul>
Where {field_name} must be the name of field included in Hashtagm model.

You did #hashtags = Hashtag.all in the controller, while you do #post.hashtags.each in your view.
Maybe you should do #hashtags.each in your view too.

Related

Rails undefined method `id' for nil:NilClass

I'm earning rails with the guidebook, but some code does not run as expected (use Rails 4.2.6, but book was written about older version). Appreciate if you can help me.
When i load pages of any of my objects (Ads) - i see nice page with object parameters, but when I load page with list of objects - I get
NoMethodError in Ads#index
Showing /home/mei33/mebay/app/views/ads/index.html.erb where line #11 raised:
undefined method `id' for nil:NilClass
<ul>
<% for ad in #ads %>
<li><%= #ad.name %></li>
<% end %>
</ul>
my ads_controler.rb looks like that:
class AdsController < ApplicationController
def show
#ad = Ad.find(params[:id])
end
def index
#ads = Ad.all
end
end
tried to add this line of code, but not helped:
def new
#ad = Ad.new
end
maybe there is something I cannot notice? some mistake?
You should call as ad.id not #ad.id:
<ul>
<% for ad in #ads %>
<li><%= ad.name %></li>
<% end %>
</ul>
Or:
<ul>
<% #ads.each do |ad| %>
<li><%= ad.name %></li>
<% end %>
</ul>

Without an action in controller how does rails execute the view?

I just started learning rails. I have a doubt in the following section.
Controller:
book_controller.rb
class BookController < ApplicationController
end
View:
list.html.erb
<% if #books.blank? %>
<p>There are not any books currently in the system.</p>
<% else %>
<p>These are the current books in our system</p>
<ul id="books">
<% #books.each do |c| %>
<li><%= link_to c.title, {:action => 'show', :id => c.id} -%></li>
<% end %>
</ul>
<% end %>
Router:
get 'list' => 'book#list'
When I goto localhost:3000/list, it displays the content of list.html.erb. How does that happen when I don't have an list action in Controller? How is my understanding wrong?
If the #books is defined in the book controller there is no trouble executing this code since the list.html.erb is already define inside the books folder and the route is set as
get 'list' => 'book#list'
Ruby MVC structure is integrated such that when you create a variable #varibale it is already available at view.

How an action call works in Rails

I have the following action method:
class CommandsController < ApplicationController
.
.
.
def usersList
#command = Command.find_by(id: params[:id])
#users_list = #command.user_commands.paginate(page: params[:page], per_page: 10)
end
end
The following view: users_list.html.erb
<% provide(:title, "Command list") %>
<h1>Lista Utenti per <%= #command.content %></h1>
<%= will_paginate #users_list %>
<ul class="users">
<% #users_list.each do |user| %>
<li><%= user.name.capitalize %>, <%= user.email %></li>
<% end %>
</ul>
And in the routes.rb
get 'commands/:id/usersList' => 'commands#users_list', as: :list
Now I don't understand why going to lochalhost:3000/commands/1/usersList I receive the following error:
undefined method `content' for nil:NilClass
refered to line <h1>Lista Utenti per <%= #command.content %></h1>
Why does it do so? When I call an action, before it renders the page and then after it executes the code inside the method?
I resolved the problem by changing the names: usersList --> userslist and then users_list.html.erb --->userslist.html.erb and in the routes.rb ----> get 'commands/:id/userslist' => 'commands#userslist', as: :list
Then I don't understand the naming convention...if I declare an action usersList how should I rename the related view?
You should change your action name to users_list.

Displaying tweets in my view

I'm a Rails noob and I'm following a blog post I found here...
I have everything working up until the end. Then things get nebulous.
So if I have this in my helper...
module ApplicationHelper
def display_content_with_links(tweet)
tweet.content.gsub(/(http:\/\/[a-zA-Z0-9\/\.\+\-_:?&=]+)/) {|a| "#{a}"}
end
end
Shouldn't I be able to have my tweets display in my view with this...
What am I doing wrong?
You are going to need a controller and view to have this display. Something simple like:
# app/controller/tweets_controller.rb
TweetsController < ApplicationController
def index
#tweets = Tweet.get_latest
end
end
and in the view:
# app/views/tweets/index.html.haml
%ul
- #tweets.each do |tweet|
%li
= display_content_with_links tweet
or if you use erb
# app/views/tweets/index.html.erb
<ul>
<% #tweets.each do |tweet| %>
<li>
<%= display_content_with_links tweet %>
</li>
<% end %>
</ul>
This is a pretty basic example that might not even come close to what you want, but it should point you in the right direction.
Actually, you should add
#tweet = Tweet.all
To the controller#action you have setup already, then iterate over them in your view:
<ul>
<% #tweets.each do |tweet| %>
<li><%= display_content_with_links tweet %></li>
<% end %>
</ul>

Obtaining a Nested Model's Count in the Controller

I have the following two models:
photoalbums
has_many:photos
photos
belongs_to:photoalbums
When I show a list of all the photoalbums, I want to also say how many photos exist in the album:
Controller:
def index
#photoalbums = PhotoAlbum.all
end
View:
<%# Get the number of photos per this album %>
<% #photos = Photo.find_by_photo_album_id(photoalbum.id) %>
<li><%= #photos.count %> Photos</li>
The above isn't allowing me to do #photos.count or #photos.record in the view.
Is there a better way to do this in the controller? I thought about perhaps an include(:photos) in the controller?
Thanks!!!
Solution 1) In your view you need to write this..
<% #photos = Photo.find_all_by_photo_album_id(photoalbum.id) %>
<li><%= #photos.count %> Photos</li>
instead of find_by_photo_album_id find_all_by_photo_album_id.
Solution 2)
In controller
def index
#photoalbums = PhotoAlbum.find(:all,:include => :photos)
end
In View
<% #photos = photoalbum.photos %>
<li><%= #photos.count %> Photos</li>

Resources