2012-12-15 70 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