Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

Murat KAYA
Murat KAYA
10,127 Points

Rails Belongs To User association

I just want to show the username who crated the events.But When I try it says undefined method `user_name' for nil:NilClass

This is my event.rb

class Event < ActiveRecord::Base
  belongs_to :user
  validates :name, presence: true
  validates :description, presence: true, length: {minimum: 5}
end

And this is my user.rb

class User < ActiveRecord::Base
  has_secure_password
  has_many :events
end

And I am trying to show the user name in html.erb file like this.

<%= event.user.user_name %>

But I am getting this error.

So this is my create method in events_controller

def create

    @event = current_user.events.new(event_params)

    respond_to do |format|
      if @event.save
        format.html { redirect_to @event, notice: 'Event was successfully created.' }
      else
        format.html { render :new }
      end
    end
  end

So what should I do for showing username in that page.

Thank You

2 Answers

Can you post up your schema?

You also may not have the correct controller method yet. You have only shown the create method, which is NOT typically used to render a view. In a typical Rails CRUD app you will have a 'show' view which corresponds to a 'show' method in a controller.

For example, in my Yelp clone app, I have a Restaurants controller. When I want to look at a particular Restaurant in the app, I would go to a Restaurant's 'show' view, which corresponds to the 'show' method in the Restaurants controller. There, an instance variable, @restaurant is created which is then accessible in the view:

Restaurants Controller:

class RestaurantsController < ApplicationController
  before_action :authenticate_user!, :except => [:index, :show]

  def index
    @restaurants = Restaurant.all
  end

  def show
    @restaurant = Restaurant.find(params[:id])
  end

  def new
    @restaurant = Restaurant.new
  end

  def create
    @restaurant = current_user.restaurants.new(restaurant_params)
    if @restaurant.save
      redirect_to restaurants_path
    else
      render 'new'
    end
  end

  def edit
    @restaurant = Restaurant.find(params[:id])
  end

  def update
    @restaurant = Restaurant.find(params[:id])

    if @restaurant.update_as(current_user, restaurant_params)
      redirect_to restaurants_path
      flash[:notice] = "Restaurant successfully updated"
    else
      redirect_to restaurants_path
      flash[:notice] = "You cannot edit someone else's restaurant"
    end
  end

  def destroy
    @restaurant = Restaurant.find(params[:id])

    if @restaurant.delete_as(current_user)
      redirect_to restaurants_path
      flash[:notice] = "Restaurant successfully deleted"
    else
      redirect_to restaurants_path
      flash[:notice] = "You cannot delete someone else's restaurant"
    end
  end

  private

  def restaurant_params
    params.require(:restaurant).permit(:name, :image)
  end
end

app/views/restaurants/show.html.erb:

<p><%= @restaurant.name %></p>
<p><%= @restaurant.description %></p>

Have a scout around here to look at the full code: https://github.com/Yorkshireman/yelp_clone

You might need to provide more detail here to get the answer you need. Please do!

Murat KAYA
Murat KAYA
10,127 Points

Actually I figured out problem with this line

<%= event.user.try(:user_name) %>

Thank You