0

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
ekremkaraca
  • 1,453
  • 2
  • 18
  • 37
Naomi K
  • 1,437
  • 4
  • 13
  • 20

1 Answers1

0

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.

dax
  • 10,779
  • 8
  • 51
  • 86
  • Hi, yes I have something similar, I added it to my question <%= link_to 'Back', current_event_path %> Do I need to use the instance variable? If yes, why? Thank you – Naomi K Sep 11 '13 at 14:14
  • Thank you, So I have to specify in controller with a params :id if i want it to link to a specific post – Naomi K Sep 11 '13 at 14:34
  • @NaomiKuntinama, right - just look at the output of rake routes for an idea of what is required – dax Sep 11 '13 at 14:36
  • im not sure I understand completely, now I am trying to fix link for new_current_event_path. I add new_current_event_path(@current_event) but it is a syntax error? can you explain a little how this works? – Naomi K Sep 11 '13 at 14:50
  • check out this 'getting started' guide - i think it will answer a lot of questions for you! http://guides.rubyonrails.org/getting_started.html – dax Sep 11 '13 at 14:54
  • if you were curious i was just missing the comma between 'new event', new_current_event_path – Naomi K Sep 11 '13 at 15:38
  • was that in the question? sorry, should have caught that! – dax Sep 11 '13 at 16:12