Story#new中的NoMethodError

时间:2016-11-30 10:53:11

标签: ruby-on-rails ruby

它说undefined method stories_path'`。

嗨,我一直试图找出上述错误的解决方案一段时间,但无济于事。我也在终端收到错误:

ActionView::Template::Error (undefined method `stories_path' for #<#<Class:0x005557c1a40560>:0x005557c1885cc0>
Did you mean?  story_path):
    1: <%= form_for @story do |f| %>
    2:   <%= render 'shared/errors', object: @story %>
    3: 
    4:   <div class="form-group">

我感觉可能与我的路线有关,但我不确定如何更改配置。

我的控制器:

class StoryController < ApplicationController
    before_action :find_story, only: [:destroy, :show, :edit, :update]


def index
    @stories = Story.order('created_at DESC')
end

def new
    @story = Story.new
end

def create
    @story = Story.new(story_params)
    if @story.save
        flash[:success] = "Your beautiful story has been added!"
        redirect_to root_path
    else
        render 'new'
    end
end

def edit
end

def update
    if @story.update.attributes(story_params)
        flash[:success] = "More knowledge, more wisdom"
        redirect_to root_path
    else
        render 'edit'
    end
end

def destroy
    if @story.destroy
        flash[:success] = "I think you should have more confidence in your storytelling"
    else
        flash[:error] = "Can't delete this story, sorry"
    end
end

def show
    @stories = Story.all
end

private

def story_params
    params.require(:story).permit(:title, :body)
end

def find_story
    @story = Story.find(params[:id])
end


end

new.html.erb:

<%= form_for @story do |f| %>

 <%= render 'shared/errors', object: @story %>

  <div class="form-group">
    <%= f.label :title %>
    <%= f.text_field :title, class: 'form-control', required: true %>
  </div>

  <div class="form-group">
    <%= f.label :body %>
    <%= f.text_area :body, class: 'form-control', required: true, cols: 3 %>
  </div>

  <%= f.submit 'Post', class: 'btn btn-primary' %>
<% end %>

我的routes.rb文件:

Rails.application.routes.draw做

devise_for :users
resources :story
root to: 'story#index'
end

最后是/shared/_errors.html.erb

<% if object.errors.any? %>
  <div class="panel panel-danger">
    <div class="panel-heading">
      <h3 class="panel-title">The following errors were found while submitting the form:</h3>
    </div>

    <div class="panel-body">
      <ul>
        <% object.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  </div>
<% end %>

添加了信息:rake routes | grep stories不起作用。但是,rake routes | grep story会显示以下结果:

  story_index GET    /story(.:format)               story#index
                         POST   /story(.:format)               story#create
               new_story GET    /story/new(.:format)           story#new
              edit_story GET    /story/:id/edit(.:format)      story#edit
                   story GET    /story/:id(.:format)           story#show
                         PATCH  /story/:id(.:format)           story#update
                         PUT    /story/:id(.:format)           story#update
                         DELETE /story/:id(.:format)           story#destroy
                    root GET    /                              story#index

2 个答案:

答案 0 :(得分:2)

最简单的方法是遵循“约定优于配置”并使用Rails期望的默认值。

所以你的routes.rb

resources :stories
root to: 'stories#index'

app/controllers/story_controller.rb重命名为app/controllers/stories_controller.rb并将类定义更改为

class StoriesController < ApplicationController

其余的都应该有用。

答案 1 :(得分:2)

这里的主要问题是你使用控制器和路由的单数名称

您需要更改以下

<强>的routes.rb

resources :stories

<强> stories_controller.rb

class StoriesController < ApplicationController
end

将为您提供以下路线

stories    GET    /stories(.:format)          stories#index
           POST   /stories(.:format)          stories#create
new_story  GET    /stories/new(.:format)      stories#new
edit_story GET    /stories/:id/edit(.:format) stories#edit
story      GET    /stories/:id(.:format)      stories#show
           PATCH  /stories/:id(.:format)      stories#update
           PUT    /stories/:id(.:format)      stories#update
           DELETE /stories/:id(.:format)      stories#destroy

然后,在创建新故事时,表单将正确stories_path