0

I'm learning RoR and working on a simple app to share posts. My trouble is creating the posts and displaying them on a separate page(not sure where my error is). When a "user" goes to post an "event" I get an undefined method:

undefined method `model_name' for NilClass:Class 12: <%= render @events %>

I thought I had defined the method properly, but I guess not.

Thanks in advance for any help you can give.

1 Answer 1

2

I'm guessing that this is the controller in question:

  def create
    @event = current_user.events.build(params[:event])
    if @event.save
      flash[:success] = "Event Shared"
      redirect_to root_path
    #else
    #  render 'pages/about'
    end


    #@event = Event.new         what I had before
    #@title = "Create An Event"
  end

You set @event in there but not @events. Instance variables are created on first use and default to nil so in your view, when you say this:

<%= render @events %>

you're creating @events right there and it will be nil. Perhaps you want:

<%= render @event %>
Sign up to request clarification or add additional context in comments.

3 Comments

I first tried singular "@event" and received an error with that as well. The aspect that confuses me is that my user controller here has a singular @user, but it displays on the users index page here
This is the error it gives, so I think you are right. Should the :content be defined in the controller? I feel like it's just one correction short of posting. Thanks for your help. Also, my repo is updated to my most current version. undefined method `content' for #<WillPaginate::Collection:0x11083f6d8> Extracted source (around line #6): 3: <div class="details"> 4: <h5><a href="#">Students Vs Food!</a></h5> 5: <p> 6: <%= @event.content %> 7: </p> 8: </div> 9: </div> Trace of template inclusion: app/views/events/show.html.erb
@Alekx: Your UsersController#index has @users. And in EventsController#show, your Event.paginate(:page => params[:page]) returns a list of events when show is normally handling a single event (@event = Event.find(params[:id])).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.