2012-12-15 67 views
1

我正在用Ruby遊戲on Rails的一所學校的項目,但現在我堅持了這個錯誤:Ruby on Rails的錯誤的form_for

未定義的方法`storylines_index_path」爲#<#:0x007ff34cca8f68>

我正在製作一個頁面,您可以在其中添加一個故事情節表單,因此我需要一個包含一些惡意內容的表單。我想使用form_for方法。但是,當添加我得到這個錯誤

這裏是我的代碼:

的意見/ new.html.erb

<% provide(:title, 'Overzicht Storylines') %> 
<h1>Voeg nieuwe storyline toe</h1> 
<%= form_for(@storyline) do |f| %> 
    <%= render 'shared/error_messages' %> 

    <%= f.label :title %> 
    <%= f.text_field :title%> 

    <%= f.submit "Create my account", class: "btn btn-large btn-primary" %> 

<% end %> 

storylines_controller.rb

class StorylinesController < ApplicationController def index 
    @storylines = Storylines.find(:all) end 

    def show 
    @storyline = Storylines.find(params[:id]) 
    end 

    def new 
    @storyline = Storylines.new end end 

storylines.rb

class Storylines < ActiveRecord::Base 
    attr_accessible :title, :text 
end 

r outes.rb

StoryLine::Application.routes.draw do 
    get "users/new" 
    get "storylines/new" 

    resources :users 
    resources :storylines 
    resources :sessions, only: [:new, :create, :destroy] 

    match '/signup', to: 'users#new' 
    match '/signin', to: 'sessions#new' 
    match '/signout', to: 'sessions#destroy', via: :delete 

    root to: 'static_pages#home' 

    match '/help', to: 'static_pages#help' 
    match '/contact', to: 'static_pages#contact' 
    match '/about', to: 'static_pages#contact' 
    match '/home', to: 'static_pages#home' 

end 
+0

通常它與路由和/或傳遞給'form_for'的對象有關。你可以在'new.html.erb'中發佈你的'routes.rb'和'form_for'這行嗎? –

+0

我已添加routes.rb – RickRamone

回答

3

導軌慣例要求您以單數形式命名模型,即Storyline而不是Storylines。在類定義中重命名模型名稱和控制器應解決此問題。

+0

謝謝你現在工作! – RickRamone

0

當你

form_for(@storyline) 

它會嘗試去尋找storylines_index_path,以創造一個故事情節對象插入到數據庫中。

所以,你需要定義文件的config/routes.rb中的路線,如果你已經定義資源:故事情節應該定義路由,如果你不希望創建一個REST資源,你可以創建自己的路線

match 'storylines/create', to: 'storylines#create', as: :storylines_create 

,然後在視圖

我勸讀Rails Routing Guide,因爲它說明了在軌道上更好的一切相關的路由

+0

我已添加資源:故事情節 – RickRamone

+0

好的,我同意Ahmad Sherif,將storylines.rb重命名爲storyline.rb,並將課程Storylines重命名爲Storyline。或者只是創建一個名爲storylines_index_path的路線指向Storylines#index(非常不好的練習,但它會起作用) – rorra

+0

謝謝你現在在工作! – RickRamone