新建和编辑控制器操作重定向到主页,'覆盖现有方法'

时间:2011-11-07 19:47:40

标签: ruby-on-rails-3 redirect overwrite

我遇到一个问题,Rails由于某种原因将new和edit方法重定向到主页,但index和show方法返回正常。

控制器或路线没有什么特别之处,但我会将它们包括在下面。

我得到的错误是

Started GET "/recipes/new" for 192.168.5.46 at 2011-11-07 11:40:06 -0800
  Processing by RecipesController#new as HTML
Creating scope :page. Overwriting existing method Recipe.page.
  SQL (4.0ms)  SHOW TABLES
Creating scope :page. Overwriting existing method User.page.
  SQL (3.5ms)  SHOW TABLES
  User Load (1.1ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 2) LIMIT 1
  CACHE (0.1ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 2) LIMIT 1
Creating scope :page. Overwriting existing method RoleUser.page.
Creating scope :page. Overwriting existing method Role.page.
  Role Load (2.5ms)  SELECT `roles`.* FROM `roles` INNER JOIN `role_users` ON `roles`.id = `role_users`.role_id WHERE ((`role_users`.user_id = 2))
Completed   in 887ms
Redirected to http://localhost:3000

我假设问题在于这个'覆盖'位,但我不知道它为什么要这样做,然后重定向到家。

我的食谱控制器是

class RecipesController < ApplicationController
 load_and_authorize_resource
before_filter :require_user
respond_to :html, :js

  def index
    @recipes = Recipe.find_all_by_author_id(current_user.id)
  end

  def show
   @recipe = Recipe.find(params[:id])

  end

  def new
  return render :text =< 'new recipe'
    @recipe = Recipe.new

  end

  def create
    @recipe = Recipe.new(params[:recipe])
    @recipe.author_id=current_user.id
    if @recipe.save
      flash[:notice] = "Successfully created recipe."
      redirect_to @recipe
    else
      render :action =< 'new'
    end
  end
end

即使new上的渲染文本没有返回,它也会直接进入重定向。

为什么会出现这种情况以及解决问题的建议?

1 个答案:

答案 0 :(得分:1)

方法:require_user中的内容是什么? 如果您发表评论load_and_authorize_resource是否有效?