简体   繁体   中英

Ruby on Rails Form_for error

I'm making a game in Ruby on Rails as a school project but now i'm stuck with this error:

undefined method `storylines_index_path' for #<#:0x007ff34cca8f68>

I'm making an page in which you I wou like to have a form to add an storyline, so I need a form with some fiels in it. I'd like to use the form_for method. But when adding I get this error

Here is my code:

views/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

Rails conventions require that you name your model in a singular form, ie, Storyline not Storylines . Renaming your model name in the class definition and the controllers should fix this.

When you do

form_for(@storyline)

It will try to look for the storylines_index_path , in order to create a Storyline object into your database.

So you need to define the route on the file config/routes.rb , if you already defined resources :storylines that should define the route, if you don't want to create a REST resource, you can create your own route

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

and then on the view

I advise to read the Rails Routing Guide since it explains much better everything related routing on rails

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM