未定义的方法或我的活动记录错误 [英] undefined method or my Active Record mistakes

查看:19
本文介绍了未定义的方法或我的活动记录错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的问题很愚蠢,很抱歉,但是Rails对我来说是新的。我做了两个型号和两个控制器。我的问题是在创建第二个模型并添加对第一个模型的引用后开始出现的。

class SentencesController < ApplicationController
    before_action :find_story

    def create      
        @sentence = find_story.sentences.build(sentence_params)
        if @sentence.save
           flash[:success] = "You wrote the continuation!"
           render 'stories/show'
        else
          render 'stories/show'
        end
    end

  private

    def sentence_params
      params.require(:sentence).permit(:content)
    end

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

和这个:

class StoriesController < ApplicationController 

........

    def show
        @story = Story.find(params[:id])
        @sentence = @story.sentences.build
    end 

.........


end
我在定义实例变量@STORY=Story.find(params[:id])时遇到了问题。错误:SentencesController#CREATE中的ActiveRecord::RecordNotFound。我尝试了许多组合。

这是我的迁移文件:

class CreateStories < ActiveRecord::Migration[5.1]
  def change
    create_table :stories do |t|
      t.string :title
      t.text :content

      t.timestamps
    end
  end
end

class CreateSentences < ActiveRecord::Migration[5.1]
  def change
    create_table :sentences do |t|
      t.text :content
      t.references :story, foreign_key: true

      t.timestamps
    end
    add_index :sentences, [:story_id, :created_at]
  end
end

我做错了什么?

编辑(路由):

Rails.application.routes.draw do
    root 'stories#index'
    get 'stories/show'
  get 'stories/new'
  resources :stories
  resources :sentences, only: [:create]
end

和架构:

ActiveRecord::Schema.define(version: 20180322121215) do

  create_table "sentences", force: :cascade do |t|
    t.text "content"
    t.integer "story_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["story_id"], name: "index_sentences_on_story_id"
  end

  create_table "stories", force: :cascade do |t|
    t.string "title"
    t.text "content"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

推荐答案

如评论中所述,您可能希望您的路由如下所示:

resources :stories do 
  resources :sentences, only: [:create]
end

这将为您提供:

 story_sentences POST   /stories/:story_id/sentences(.:format)   sentences#create
         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

您可以使用如下内容:

<%= form_tag story_sentences_path(@story) do %>
  ...
<% end %>

然后,如Matt所说,将您的find更改为:

@story = Story.find(params[:story_id])

这篇关于未定义的方法或我的活动记录错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆