Creating a custom view

292 views Asked by At

I am trying to create a landing page for an event for people to visit to see the events details. I have created the view, added a route to the event resources and made changes to the controller but something has been done incorrectly.

Here is my code:

routes.rb:

resources :events do
    resources :guests
    match '/landing_page', to:'events#landing_page', as: :landing_page, :via =>[:get, :post]
    # resources :guestlists
  end

event_controller:

def landing_page
    @event = Event.find(params[:id])
  end

When I open the landing page i get the following error:

"ActiveRecord::RecordNotFound (Couldn't find Event without an ID):"

1

There are 1 answers

0
rjgrazioli On

In case anyone else runs into this, I wanted to document Sergio's last comment here which I believe leads to the outcome I think most people will be looking for. Nesting this inside of a member block should get you an ideal outcome:

resources :events do
  member do
    get :landing_page
  end
end

Running rake routes should now show /events/:id/landing_page(.:format) so you can use the same method you use in your show method that just asks for params[:id].

I was wracking my brain for a while on this as rails resources seem to be dwindling on the interwebs.