Question

I'm always getting this error and I don't understand where I am wrong. I think I have everything I need action in controller, resources in route file and view for controller action. I put the route current_events/new in the browser when I get this error. I also try with just resources :current_events

output of rake routes:

   current_events GET      /current_events(.:format)              current_events#index
new_current_event GET      /current_events/new(.:format)          current_events#new
    current_event GET      /current_events/:id(.:format)          current_events#show

config/routes.rb:

appname::Application.routes.draw do
  devise_for :users, controllers: { omniauth_callbacks: "omniauth_callbacks"}

  resources :current_events, only: [:show, :new, :index]
end

CurrentEventsController:

class CurrentEventsController < ApplicationController

  def index
      @current_event = CurrentEvent.all

      respond_to do |format|
          format.html
          format.json { render json: @current_event }
      end
  end

  def create
      @current_event = CurrentEvent.new(params[:current_event])

      respond_to do |format|
          if @current_event.save
              format.html { redirect_to @current_event, notice: 'current event was created.' }
              format.json { render json: @current_event, status: :created, location: @current_event }
          else
              format.html { render action: "new" }
              format.json {render json: @current_event.errors, status: :unprocessable_entity}
          end
      end
  end

  def new
      @current_event = CurrentEvent.new

      respond_to do |format|
          format.html
          format.json { render json: @current_event }
      end
  end

  def edit
      @current_event = CurrentEvent.find(params[:id])
  end

  def destroy
      @current_event = CurrentEvent.find(params[:id])
      @current_event.destroy

      respond_to do |format|
          format.html { redirect_to current_event_url}
          format.json { head :no_content }
      end
  end

  def show
      @current_event = CurrentEvent.find(params[:id])

      respond_to do |format|
          format.html
          format.json{ render json: @current_event}
      end
  end
end
Was it helpful?

Solution

I forgot to say, I am trying to go to new page so in browser I say current_events/new

It will still throw an error if there's a link that doesn't work on the page.

In your view, the link should look something like this:

<%= link_to "name of current event", current_event_path(@current_event) %>

update

based on your rake routes

current_event GET      /current_events/:id(.:format) #note ":id"

when you're trying to see a specific current event, you need to pass it an :id which makes sense - if you're trying to call a specific person you need to use their telephone number. so your code should look like this:

<%= link_to 'Back', current_event_path(@event) %>  

But keep in the mind that @event won't work unless you define it correctly in the controller action for this view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top