开发者

No Method Error – Undefined Method for Class

So this has just been driving me nuts! I have looked through tons of posts and can't quite get my app to work.. i am hoping the SO community can.

So, I have a RoR App where I can create a new event.

When I go to localhost:3000/events and localhost:3000/events/new both load correctly and look great. Here is the problem. When I submit an event at localhost:3000/events/new, the form info is added to the database, but I get the following error:

undefined method `event_path' for #<#:0x00000100a15800>

Here are some (hopefully) relative code bits:

views/events/create.html.erb

<%= simple_form_for(@event) do |f| %>
<%= f.input :name %>
<%= f.input :date %>
<%= f.input :location %>
<%= f.input :organization %>
<%= f.input :description %>
<%= f.submit "Create Event"%>

<% end %>

controllers/events_controller.rb

class EventsController < ApplicationController
respond_to :html

  def index
    @all_events = Event.all
  end

  def show
      @event = Event.find_by_name(params[:id])
  end

  def new
    respond_with(@event = Event.new)
  end

  def edit
    @event = Event.find_by_name(params[:id])
  end

  def create
    @event = Event.create(params[:event])
    if @event.valid?
        flash[:notice] = "Event successfully created"
    end
   end

  def update
@event = Event.find_by_name(params[:id])
@event.update_attributes(params[:event])
if @event.valid?
    flash[:notice] = "Event successfully updated"
end
respond_with(@event)
  end

  def destroy
    @event = Event.find_by_name(params[:id])
    if @event.destroy
        flash[:notice] = "Event successfully deleted"
    end
    respond_with(@event)
  end
end

models/event.rb开发者_JS百科

   class Event < ActiveRecord::Base
    validates_presence_of :name
    has_and_belongs_to_many :organizations

    attr_accessible :name, :date, :location, :organization, :description
    attr_accessor :organizations

    end

config/routes.rb (only relevant part)

resources :events, :only => [:index, :new, :create, :edit]

Really appreciate any help that can be provided... I am new to Ruby on Rails, so I am just trying to figure this whole thing out.

Thanks!

Kevin


After your create or update methods, you redirect to @event, which will redirect to the show page of your event. You have not generated the route for the show, update, destroy pages, by using the :only clause. You can verify this by running rake routes on your console. Remove the :only clause and it would work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜