获取user_id的NoMethodError,但仅限于部署(Rails)

时间:2016-05-09 19:22:02

标签: ruby-on-rails ruby authorization

这在生产中完美运行,但一直在搞乱部署。奇怪的是,我使用相同的代码设置了类似的另一个模型,它在部署时工作正常。

控制器:

  def new
        @task = Task.new
      end

    def create
        @task = Task.new(task_params)
        @task.user_id = current_user[:id]
        respond_to do |format|
          if @task.save
            format.html { redirect_to line_url(@task.lines_id), notice: 'Task was successfully created.' }
            format.json { render :show, status: :created, location: @task }
          else
            format.html { render :new }
            format.json { render json: @task.errors, status: :unprocessable_entity }
          end
        end
      end

 private
    # Use callbacks to share common setup or constraints between actions.
    def set_task
      @task = Task.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def task_params
      params.require(:task).permit(:title, :description, :priority, :lines_id, :scene, :icon, :icon_color, :fulltext, :doclink, :user_id)
    end
  end

错误:

2016-05-09T19:09:50.769530+00:00 heroku[router]: at=info method=GET path="/tasks/new?lines_id=9" host=pacific-tor-26709.herokuapp.com request_id=3dc355bb-90b3-4aac-b93d-330f5cb1561c fwd="72.24.207.125" dyno=web.1 connect=1ms service=126ms status=200 bytes=11300
2016-05-09T19:09:53.112272+00:00 heroku[router]: at=info method=POST path="/tasks" host=pacific-tor-26709.herokuapp.com request_id=8ddd3304-7a25-4378-94c3-efc87fb4ac07 fwd="72.24.207.125" dyno=web.1 connect=18ms service=81ms status=500 bytes=1754
2016-05-09T19:09:53.050151+00:00 app[web.1]: Started POST "/tasks" for 72.24.207.125 at 2016-05-09 19:09:53 +0000
2016-05-09T19:09:53.100064+00:00 app[web.1]: Completed 500 Internal Server Error in 26ms
2016-05-09T19:09:53.107108+00:00 app[web.1]:
2016-05-09T19:09:53.107138+00:00 app[web.1]:
2016-05-09T19:09:53.073387+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"gRLRjpaHGhNWLsJP4HpWb8Yv0hgjLLa0dTzQgSSb6MI=", "task"=>{"icon_color"=>"", "scene"=>"0", "title"=>"test", "description"=>"", "fulltext"=>"", "doclink"=>"", "icon"=>"", "priority"=>"9", "lines_id"=>"9"}, "commit"=>""}
2016-05-09T19:09:53.107137+00:00 app[web.1]:   app/controllers/tasks_controller.rb:28:in `create'
2016-05-09T19:09:53.107139+00:00 app[web.1]:
2016-05-09T19:09:53.107135+00:00 app[web.1]: NoMethodError (undefined method `user_id=' for #<Task:0x007fe6f34a60c8>):
2016-05-09T19:09:53.073169+00:00 app[web.1]: Processing by TasksController#create as HTML

同时,在生产中,提交没有问题:

Started POST "/tasks" for 127.0.0.1 at 2016-05-09 13:18:38 -0600
Processing by TasksController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"tCxI70ziZPsxsJQB1PCqHFBrA8KIlnqNOPdsDoUnElE=", "task"=>{"icon_color"=>"", "scene"=>"0", "title"=>"B", "description"=>"", "fulltext"=>"", "doclink"=>"", "icon"=>"", "priority"=>"2", "lines_id"=>"11"}, "commit"=>""}
  User Load (0.5ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1 LIMIT 1
   (0.0ms)  begin transaction
  SQL (1.0ms)  INSERT INTO "tasks" ("created_at", "description", "doclink", "fulltext", "icon", "icon_color", "lines_id", "priority", "scene", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", "2016-05-09 19:18:38.261089"], ["description", ""], ["doclink", ""], ["fulltext", ""], ["icon", ""], ["icon_color", ""], ["lines_id", 11], ["priority", 2], ["scene", "f"], ["title", "B"], ["updated_at", "2016-05-09 19:18:38.261089"], ["user_id", 1]]
   (133.6ms)  commit transaction

我仔细检查以确保我在部署时运行了rake db:migrate。

我不认为这是current_user的问题,因为我肯定以用户身份登录 - 我甚至无法访问新表单,因为我需要current_user来查看表单。

真的很感激你可能有的任何见解。

1 个答案:

答案 0 :(得分:0)

我建议将user_id添加为新形式的隐藏字段,如:

<%= f.hidden_field :user_id, current_user.id %>

或者更好的方法:

@task = current_user.task.new(task_params)

因为这是一种更好的方法&amp;将避免这种错误情况。