Undefined method, wrong association or call? - ruby-on-rails

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.

Related

Ruby on rails How do I display data associated with a specific row

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

uninitialized constant User::Post (NameError)

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

Rails ActiveRecord_Relation

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

Getting an undefined method `id' for #<ActiveRecord::Relation>

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.

Nested Attributes when Viewing Multiple Models (not a form)

I am having trouble rendering a model's attributes in foreign model's view. I think it is a nested attributes problem because f.bike works but f.bike.biketype gives an undefined method error. Below is my error and code. Any help would be great. Thanks!
Error from Browser:
NoMethodError in Carts#show
Showing /Users/willdennis/rails_projects/spinlister/app/views/carts/show.html.erb where line #4 raised:
undefined method `biketype' for nil:NilClass
Extracted source (around line #4):
1: <h2>Your Cart</h2>
2: <ul>
3: <% #cart.line_items.each do |f| %>
4: <li><%= f.bike.biketype %></li>
5: <% end %>
6: </ul>
views/carts/show.html.erb
<h2>Your Cart</h2>
<ul>
<% #cart.line_items.each do |f| %>
<li><%= f.bike.biketype %></li>
<% end %>
</ul>
cart.rb
class Cart < ActiveRecord::Base
has_many :line_items, :dependent => :destroy
belongs_to :user
accepts_nested_attributes_for :line_items
attr_accessible :bike_id, :name, :description, :size, :biketype, :price, :photo, :id, :address, :city, :state, :zip, :latitude, :longitude, :neighborhood
end
line_item.rb
class LineItem < ActiveRecord::Base
belongs_to :bike
belongs_to :cart
accepts_nested_attributes_for :bike, :cart
attr_accessible :name, :description, :size, :biketype, :price, :photo, :id, :address, :city, :state, :zip, :latitude, :longitude, :neighborhood
end
bike.rb
class Bike < ActiveRecord::Base
has_many :line_items
attr_accessible :name, :description, :size, :biketype, :price, :photo, :id, :address, :city, :state, :zip, :latitude, :longitude, :neighborhood
end
carts_controller.rb
def show
#cart = Cart.find(params[:id])
end
Your error is telling you that the object f.bike is of class nil. You would have been expecting it to be an instance of class Bike. Obviously you have a line item in your view that doesn't have a bike record attached to it.
To take care of the error so your view will display just add a check for blank?
li><%= f.bike.biketype unless f.bike.blank? || f.bike.biketype.blank? %></li>
As to why you have a line item with no bike? Well that's an entirely different question and there is not enough info here to answer that.
UPDATE Based on comments below
Your button_to looks a little wrong. Try
<%= button_to "Rent this Bicycle!", {line_items_path(:bike_id => #bike)}, {:id => "rentthisbike"} %>
Using the curly braces ensures that Rails knows the second param is a css style not a param to be passed into the controllers action
To check if your bike id is getting into the controller then check the params that are actually getting into the controller action that add the bike to the line item. You will be able to find this in your development.log file, find the post request for the action and one of the first lines in that action will list all the params. If you see the bike_id in that list then either one of 2 possible situations is occurring.
1) You are not accessing the params hash properly to get the bike id from it
or
2) You are not setting the bike ID on the line item before saving the record.
You should be able to access the id using params[:bike_id]
If you don't see the bike id then you need to look again at the button_to code to see why it's not passing the bike ID
End of update
Hope that helps

Resources