动作'创造'无法找到PostsController

时间:2014-06-24 22:29:09

标签: ruby-on-rails

我正在完成Rails教程,目前正在CRUD部分工作。在创建操作方面,Rails 3到4似乎有一些变化。本教程使用的是Rails 3.x.x,我使用的是Rails 4.1.2。我试图调整我的帖子控制器,但我收到了这个错误。

posts_controller.rb

class PostsController < ApplicationController
def index
    @posts = Post.all
end

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

def new
    @post = Post.new
    @category = Category.all
end

def create
    @post = Post.new(params[:post])
    if @post.save
        redirect_to posts_path, :notice => "Your post has been saved"
    else
        render "new"
    end
    Category.create(category_params)
end

def edit

end

def update

end

def destroy

end

private

def category_params
    params.require(:name).permit(:id)
end

def create
    Post.create(post_params)
end

private

def post_params

    params.require(:title, :body, :category_id, :author_id)

end

end

模型/ posts.rb

class Post < ActiveRecord::Base
belongs_to :category
end

我可以说,后期控制器是错误的,但我尝试过的任何工作都没有。

更新

创建操作的服务器日志

Started POST "/posts" for 127.0.0.1 at 2014-06-24 19:52:22 -0400
Processing by PostsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"w/ViBIFCFaaT1yIZaBkjWhjUjZ0LKXrT+9sOIN0c2q4=", "post"=>{"title"=>"teste", "body"=>"testest", "category_id"=>"4"}, "commit"=>"Add Post"}
Unpermitted parameters: utf8, authenticity_token, post, commit
  [1m[36m (0.0ms)[0m  [1mbegin transaction[0m
  [1m[35mSQL (0.2ms)[0m  INSERT INTO "posts" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", "2014-06-24 23:52:22.825483"], ["updated_at", "2014-06-24 23:52:22.825483"]]
  [1m[36m (1.3ms)[0m  [1mcommit transaction[0m
Redirected to http://domain:3000/posts
Unpermitted parameters: utf8, authenticity_token, post, commit
  [1m[35m (0.0ms)[0m  begin transaction
  [1m[36mSQL (0.2ms)[0m  [1mINSERT INTO "categories" ("created_at", "updated_at") VALUES (?, ?)[0m  [["created_at", "2014-06-24 23:52:22.828618"], ["updated_at", "2014-06-24 23:52:22.828618"]]
  [1m[35m (1.1ms)[0m  commit transaction
Completed 302 Found in 6ms (ActiveRecord: 2.8ms)

3 个答案:

答案 0 :(得分:1)

您有两个创建方法,一个是私有的。因此,红宝石无法访问它。

编辑:我已经添加了params许可证。您似乎没有发送此ID。

您的控制器应如下所示:

class PostsController < ApplicationController
  def index
    @posts = Post.all
  end

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

  def new
    @post = Post.new
    @category = Category.all
  end

  def create
    @post = Post.new(post_params)
    if @post.save
        redirect_to posts_path, :notice => "Your post has been saved"
    else
        render "new"
    end
    Category.create(category_params)
  end

  def edit
  end

  def update
  end

  def destroy
  end

  private

  def category_params
    params.require(:post).permit(:category_id)
  end

  def post_params
    params.require(:post).permit(:title, :body, :category_id, :author_id)
  end

end

答案 1 :(得分:1)

你的控制器很乱。它有两个“创建”动作和两个“私有”声明。使用脚手架生成器为您生成一个Controller。请在此处详细了解:http://guides.rubyonrails.org/command_line.html

答案 2 :(得分:0)

强大的参数

您需要在Rails 4中使用strong params

#app/controllers/posts_controller.rb
Class PostsController < ApplicationController
   def create
      @post = Post.new(post_params)
      @post.save
   end

   private

   def post_params
       parmas.require(:post).permit(:param1, :param2, :param3, :param4)
   end
end 

-

<强>路线

其次,您需要考虑resourceful routes

在Rails中使用路由时,默认设置是创建路径围绕特定的&#34;资源&#34; (I.E围绕控制器/型号)。基本上,您拥有的路线将如下工作:

#config/routes.rb
resources :posts

您可能已经知道这一点,但是当您按照本教程操作时,您将能够看到路由机制的工作原理,从而可以相应地路由您的路径

enter image description here

您需要考虑的是,正如@san所指出的那样,您需要确保每个控制器只有一条足智多谋的路由(您目前有两个create个动作)