4

私はを通してレールを学んでいます

http://guides.rubyonrails.org/getting_started.html .

コントローラーでセーブデータを実行中にエラーが発生しました。ブログの実行時に表示されるエラーは、「PostsController のアクション 'show' が見つかりませんでした」です。

**

posts_controller.rb の私のコードは

**

class PostsController < ApplicationController
def new
end
def create
@post=Post.new(params[:post].permit(:title,:text))
@post.save
redirect_to @post
end

private
def post_params
params.require(:post).permit(:title,:text)
end

def show
@post=Post.find(params[:id])
end
end

**

show.html.rb の私のコードは

**

<p>
<strong> Title:</strong>
<%= @post.title %>
</p>
<p>
<strong> Text:</strong>
<%= @post.text %>
</p>

**

create_posts.rb のコード

**

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :title
      t.text :text

      t.timestamps
    end
end

このエラーが発生する理由を教えてください

4

2 に答える 2

1

ショーのアクションを非公開にしたのはなぜですか? プライベートから出すだけ。

def new
  @post = Post.new
end
def create
 @post=Post.new(params[:post].permit(:title,:text))
 if @post.save
   redirect_to @post
 else
   render 'new'
 end
end

def show
  @post=Post.find_by_id(params[:id])
  if @post.blank?
    flash[:error] = 'The Post not found in the database'
    redirect_to root_path 
  end
end

private

def your_private_functions

end
于 2013-08-01T11:21:08.547 に答える