我正在使用Ruby on Rails作为学校项目制作游戏,但现在我遇到了这个错误:
#<#:0x007ff34cca8f68>
的未定义方法`storylines_index_path'我正在制作一个页面,你我想在其中添加一个表单来添加一个故事情节,所以我需要一个包含一些fiels的表单。我想使用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
的routes.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 :(得分:3)
Rails约定要求您以单数形式命名模型,即Storyline
而不是Storylines
。在类定义和控制器中重命名模型名称应解决此问题。
答案 1 :(得分:0)
当你这样做时
form_for(@storyline)
它会尝试查找 storylines_index_path ,以便在数据库中创建一个Storyline对象。
所以你需要在文件 config / routes.rb 上定义路线,如果你已经定义了应该定义路线的资源:故事情节,如果你不是我想创建一个REST资源,你可以创建自己的路径
match 'storylines/create', to: 'storylines#create', as: :storylines_create
然后在视图
上我建议阅读Rails Routing Guide,因为它更好地解释了轨道上所有相关路由