I am new to ROR and I can't seem to troubleshoot the following error message:
NoMethodError in GolfClubsController#show
undefined method `id' for #
Below are my models.
class GolfClub < ActiveRecord::Base
attr_accessible :name
has_many :course_nines
validates :name, presence: true, length: { maximum: 150 }
end
class CourseNine < ActiveRecord::Base
attr_accessible :name, :golfclub_id
belongs_to :golf_club
end
This is my controller:
def show
#golf_club = GolfClub.find(params[:id])
#courses = CourseNine.where(golfclub_id: #golf_club.id)
end
and my show.html.erb
<% provide(:title, #golf_club.name) %>
<h1><%= #golf_club.name %></h1>
<% #courses.each do |course| %>
<li>
<%= course.name %>
</li>
<% end %>
I found that the error is due to the way I named my fk. it should have been named golf_club_id in the CourseNine model instead of golfclub_id. It seems as if rails always looks for golf_club_id.
Related
I'm trying to make cumulative show.html for each user, that displays some data from 4 different models. Those tables:
Problem is that I dont know how to call data from Websites table for each Button. With displaying Buttons' data there is no problem.
That part of html should looks something like this:
<% if #profile.buttons.any? %>
<ul>
<% #buttons.each do |button| %>
<li>
<%= button.website.name %>
<%= link_to button.user_website_url, button.user_website_url %>
</li>
<% end %>
</ul>
<% else %>
<p>None websites added. Please finish your profile creation.</p>
<% end %>
Models:
class Website < ActiveRecord::Base
has_many :buttons
mount_uploader :logo, LogoUploader
validates :name, presence: true, length: { maximum: 50 }
validates :logo, presence: true
end
class Button < ActiveRecord::Base
belongs_to :profile
belongs_to :website
accepts_nested_attributes_for :website
validates :description, length: { maximum: 140 }
validates :user_website_url, presence: true, length: { maximum: 200 }
end
class Profile < ActiveRecord::Base
belongs_to :user
has_many :buttons, :dependent => :destroy
#...
end
User Controller:
def show
#user = User.find(params[:id])
#profile = #user.profile
#buttons = #profile.buttons
end
At this state of everything I'm getting an error:
undefined method `name' for nil:NilClass
So, what is wrong with this? I've tried many variations of associations and I'm getting either the error I put above or that no Id is passing (wrong query) for each website (that I also didn't know how to deal with).
This is probably due to a button that has no associated website. See the line:
<%= button.website.name %>
If one of the buttons has a website_id of nil, calling the method name on the nil value would produce the error you see.
Something is wrong with the following code, button.website returns nil,and nil dosen't have a method name:
<%= button.website.name %>
to fix it ,just use try:
<%= button.try(:website).try(:name) %>
This line raising the error <%= button.website.name %>
.name is not getting any value because button has no any associated website and that's why it returns nil:NilClass as an error.
In my ruby on rails application I have a show.html.erb page that displays the details of a film, what I want to do is display the dates that film is being shown. In the show.html.erb file I have done this so far:
<% #film.showings.each do |film| %>
<%= #film.showings.show_date %>
<% end %>
And in my models I have the associations:
film.rb:
class Film < ActiveRecord::Base
has_many :showings
validates :title, presence: true, length: { minimum: 3 }
end
showing.rb:
class Showing < ActiveRecord::Base
belongs_to :film
end
In my showings_controller.rb:
class ShowingsController < ApplicationController
def show
#showing = Showing.find(params[:id])
end
end
What am I doing wrong? At the minute I get the error:
NoMethodError in Films#show
undefined method show_date for #
What should I be doing?
I don't know if that's the problem, but the code in show.html.erb is wrong. You are looping over #film's showing, but INSIDE the loop you're stil referring to ALL the showings (#film.showings). Inside, try this:
<% #film.showings.each do |showing| %>
<%= showing.show_date %>
<% end %>
I have the following problem, In UserController#show there has to be a list of posts, but it throws an error as shown in the screen shot:
The part of the code which is responsible to show user posts (show.html.erb)
<div class="span8">
<% if #user.posts.any? %>
<h3>Работы (<%= #user.posts.count %>)</h3>
<ol class="posts">
<%= render #posts %>
</ol>
<%= will_paginate #posts %>
<% end %>
</div>
posts.rb:
class Posts < ActiveRecord::Base
belongs_to :user
default_scope -> { order('created_at DESC') }
validates :description, presence: true, lenght: { minimum: 6 }
validates :user_id, presence: true
end
part of a code in user.rb
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
Your help, thanks in advance is very important.
Excuse for possible mistakes in the text
You should name your model in singular form:
class Post < ActiveRecord::Base
I am currently developing a somewhat 'big' project. In this project I have many models, views, and controllers from which I have to mention the following:
Group.rb:
class Group < ActiveRecord::Base
has_many :users, through: :grouprel
has_many :grouprel, dependent: :destroy
validates :name, presence: true, length: {maximum: 25},
uniqueness: { case_sensitive: false }
validates :description, presence: true , length: {maximum: 140}
end
Grouprel.rb
class Grouprel < ActiveRecord::Base
belongs_to :user
belongs_to :group
validates :user_id, presence: true
validates :group_id, presence: true
end
User.rb
class User < ActiveRecord::Base
.....
has_many :groups, through: :grouprel, dependent: :destroy
has_many :grouprel, dependent: :destroy
.....
StaticPageController:
class StaticPagesController < ApplicationController
def home
if logged_in?
#tweet = current_user.tweets.build
#feed_items = current_user.feed.paginate(page: params[:page])
#groupi = Grouprel.where(user_id: current_user.id).pluck(:group_id)
#groupies = Group.where(id: #groupi).paginate(page: params[:page])
end
end
.....
end
Home.html.erb:
<% if logged_in? %>
.......
<section class="user_info">
<%= render 'shared/group_participation' %>
</section>
</aside>
.............
_group_participation.html.erb
<h5> Your groups: </h5>
<% if #groupies %>
<ol class="tweets">
<%= content_tag_for(:li, #groupies) do %>
<%= link_to #groupies.name, group_path(#groupies) %>
<% end %>
</ol>
<%= will_paginate #groupies %>
<% end %>
here I want to display every single group that a user is part of. The error I get when trying to get the #groupies in the StaticPagesController is %23<Group::ActiveRecord_Relation:0x007f86b00f6ed0> . I checked in my rails console , and it should return something.
What my limited knowledge about rails and ruby can tell is that this is a problem because the StaticPageController can't see the Grouprel.rb table. I tried to include controllers in herlpers. I even tried to define a method that returns 'groupies' in the application controller and then use that in the StaticPagesController. Could I get a hint of why I get that error returned ?
If my post has to contain any more specifications please do tell I will post them the second I see the request
You're not iterating over the groupies collection and are calling the name method on the collection itself. content_tag_for can iterate over the collection for you but you need to use the value it yields to the block:
<%= content_tag_for(:li, #groupies) do |group| %>
<%= link_to group.name, group_path(group) %>
<% end %>
I am working on a Rails app, wherein I have two models, i.e. a chef model and a dish model.
class Dish < ActiveRecord::Base
belongs_to :chef
attr_accessible :description, :photo, :price
validates :chef_id, presence: true
has_attached_file :photo
end
class Chef < ActiveRecord::Base
attr_accessible :name, :email, :mobile ,:password, :password_confirmation, :postcode
has_many :dishes
has_secure_password
end
I (chef) am trying to create to a dish by going to the /upload url, whose view is
<%= form_for(#dish) do |d| %>
<%= d.label :description, "Please name your dish..."%>
<%= d.text_field(:description)%>
<%= d.label :price, "What should the price of the dish be..."%>
<%= d.number_field(:price)%>
<%= d.submit "Submit this Dish", class: "btn btn-large btn-primary"%>
<% end %>
I want the created dish to appear on the show page of the chef,
<% provide(:title, #chef.name)%>
<div class = "row">
<aside class = "span4">
<h1><%= #chef.name %></h1>
<h2><%= #chef.dishes%></h2>
</aside>
<div>
<% end %>
And, dishes_controller is:
class DishesController < ApplicationController
def create
#dish = chef.dishes.build(params[:dish])
if #dish.save
redirect_to chef_path(#chef)
else
render 'static_pages/home'
end
But as soon as I try to create a dish from the /upload url, I get the following error in the dishes_controller:
NameError undefined local variable or method `chef' for #<DishesController:0x3465494>
app/controllers/dishes_controller.rb:5:in `create'
I think I have instantiated all variables, but the problem persists.
In this line:
#dish = chef.dishes.build(params[:dish])
The chef variable is not instantiated. You have to do something like this:
#chef = Chef.find(params[:chef_id])
#dish = #chef.dishes.build(params[:dish])
This way the #chef variable is populated before you use it.