Event id is nil

146 views Asked by At

I am trying to connect my pages so that the user has a show page and once they get to the show page they see events. The Event can also be clicked on and once that event is clicked on it goes directly to that events show page. The error I am getting is

No route matches {:action=>"show", :controller=>"events", :id=>nil}

Here is what my events form looks like:

<span class="name">EventName: <%= link_to(event.name, controller: "events", action: "show", id: @event) %></span>
<span class="partycode">PartyCode:<br><%= event.partycode %></span>

Here is my Events controller:

class EventsController < ApplicationController
    def show
        @event = Event.find(params[:id])
    end

    def create
        @event = current_user.events.build(event_params)
        if @event.save
            flash[:success] = "Event Created!"
            redirect_to root_url
        else
            render 'welcome#index'
        end
    end

    def destroy
    end

    private 

      def event_params
        params.require(:event).permit(:name, :partycode)
      end
end

Let me know if you need more code to figure out the problem my error occurs in the events form on the first line. This has been really frustrating because this should be a simple task.

2

There are 2 answers

0
Codebeef On BEST ANSWER

Try changing

<%= link_to(event.name, controller: "events", action: "show", id: @event) %>

to

<%= link_to(event.name, controller: "events", action: "show", id: event.id) %>

(note the missing @) in your template.

0
br3nt On

What about using the path helpers such as event_path(@event).

Your view would then become:

<%= link_to(@event.name, event_path(@event) %>