Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined method with "_path" while using rails form_for

I'm running into a (I think) routing error while using the Rails form_for helper. I have been searching around and looked at this question, but the plural for "static_event" with pluralize is "static_events" so I am at a loss. Any help would be apprecited. Here are the details....

ActionView::Template::Error (undefined method `static_events_path' for #<#<Class:0x007f9fcc48a918>:0x007f9fcc46fa78>):

My Model:

class StaticEvent < ActiveRecord::Base
attr_accessible :content, :title, :discount, :location, :day_of_week, :start_time

My Controller:

    class StaticEventsController < ApplicationController

  before_filter :authenticate, :only => [:create, :destroy]
  before_filter :authorized_user, :only => [:destroy] 


  def new
    @title = "Share An Event"
    @static_event = StaticEvent.new 
  end

  def create
    @static_event = current_user.static_events.build(params[:event])
    if @static_event.save
      flash[:success] = "Event Shared"
      redirect_to @static_event #this was the old version
    else
      render :new
    end
  end

The route:

match '/static-events/new', :to => 'static_events#new'
match '/static-events/',     :to => 'static_events#index'
match '/static-events/:id', :to => 'static_events#show'

The view

<%= form_for (@static_event) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<%= text_field "static_event", "title", "size" => 48 %>
<%= time_select "static_event", "start_time", {:ampm => true, :minute_step => 15} %>
<%= text_area "static_event", "content", "cols" => 42, "rows" => 5 %>
<%= text_field "static_event", "discount", "size" => 48 %>
<%= text_field "static_event", "location", "size" => 48 %>
<%= text_field "static_event", "day_of_week", "size" => 48 %>
<input name="" type="submit" class="button" value="share on chalkboard" />
<% end %>
like image 696
Alekx Avatar asked Jan 03 '12 00:01

Alekx


3 Answers

Only routes created using the resources method are automatically named.

If you want to name your routes, use the :as option:

match '/static-events/new', :to => 'static_events#new', :as => :new_static_event
match '/static-events/',     :to => 'static_events#index', :as => :static_events
match '/static-events/:id', :to => 'static_events#show', :as => :static_event

However, it's better to use the resources method. You must pass the "true" name of your model as the first parameter, then override the path if you want:

resources :static_events, :path => 'static-events'
like image 72
Fábio Batista Avatar answered Nov 03 '22 09:11

Fábio Batista


First of all, you should define your routes this way:

resources 'static-events', :only => [:new, :create]

This will create a route for new and create methods.

Because when you use a new ActiveRecord object as an argument to form for, it will looks for *s_path like static_events_path in your routes file with the POST verb.

I think the way you have defined your routes doesn't create the static_events_path with POST verb (you can check that by using rake routes as megas said). So don't use match anymore, use resources or get/post/... instead of match in your Rails 3 projects.

EDIT

I did not notice yesterday, but there is no route for create method. Add the route below before static_events#index or remove all your routes and do like I said above.

post '/static-events/', :to => 'static_events#create'
like image 26
basgys Avatar answered Nov 03 '22 10:11

basgys


Run rake routes and you'll see the list of your routes. Then you can fix the route file to have appropriate route path.

like image 6
megas Avatar answered Nov 03 '22 10:11

megas