1

学校のプロジェクトとして Ruby on Rails でゲームを作成していますが、次のエラーで立ち往生しています。

未定義のメソッド `storylines_index_path' for #<#: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

ストーリーライン.rb

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

ルート.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
4

2 に答える 2

3

Rails の慣例では、モデルに単数形の名前を付ける必要があります。つまり、 でStorylineはありませんStorylines。クラス定義とコントローラーでモデル名を変更すると、これが修正されるはずです。

于 2012-12-15T11:20:00.110 に答える
0

あなたがするとき

form_for(@storyline)

データベースに Storyline オブジェクトを作成するために、 storylines_index_pathを検索しようとします。

したがって、ファイルconfig/routes.rbでルートを定義する必要があります。ルートを定義する必要があるリソース :storylinesを既に定義している場合は、REST リソースを作成したくない場合は、独自のルートを作成できます。

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

そしてビューで

Rails Routing Guideを読むことをお勧めします。これは、 Rails のルーティングに関連するすべてのことをよりよく説明しているためです。

于 2012-12-15T11:03:35.590 に答える