hartl教程ParameterMissing在GuidesController #edit中

时间:2013-09-20 14:47:37

标签: ruby-on-rails-4 railstutorial.org

我正在关注hartl教程来构建我的应用, 但我没有使用User作为我的模型,而是使用Guide。

现在我在第9.2.1章 当我链接到edit_path时,导致错误:

  

GuidanceController中的ActionController :: ParameterMissing#edit

和aplication trace是

  

app / controllers / guides_controller.rb:42:in guide_params' app/controllers/guides_controller.rb:29:in edit'

我的guides_controller.rb是

class GuidesController < ApplicationController
  before_action :signed_in_guide, only: [:edit, :update, :index]
  before_action :correct_guide,   only: [:edit, :update]

  def index
    @guides = Guide.all
  end

  def new
    @guide = Guide.new
  end
  def show
    @guide = Guide.find(params[:id])
  end
  def create
    @guide = Guide.new(guide_params)
    if @guide.save
        # successful
      guide_sign_in @guide
        flash[:success] = "guide signup success!"
        redirect_to @guide
    else
        flash[:success] = "guide signup failed!"
        render 'new'
    end
  end
  def edit
    @guide = Guide.find(params[:id])
    if @guide.update_attributes(guide_params)
      flash[:success] = "Profile updated"
      redirect_to @guide
    else
      render 'edit'
    end
  end




  private
      def guide_params
        params.require(:guide).permit(:name, :email,
        :password, :password_confirmation)
      end

    def signed_in_guide
      unless guide_signed_in?
        store_location
        redirect_to guidesignin_url, notice: "Please sign in."
      end
    end

    def correct_guide
      @guide = Guide.find(params[:id])
      redirect_to(root_url) unless current_guide?(@guide)
    end


end

我不明白为什么这些参数会丢失...... 谁能帮助我?非常感谢!

1 个答案:

答案 0 :(得分:1)

我认为您的代码应如下所示:

def edit
  @guide = Guide.find(params[:id])
end

def update
  @guide = Guide.find(params[:id])
  if @guide.update_attributes(guide_params)
    flash[:success] = "Profile updated"
    redirect_to @guide
  else
    render 'edit'
  end
end

如果你想使用@guide = Guide.find(params[:id])

,你可以从这些方法中移除before_action :correct_guide, only: [:edit, :update]