Aj Burt Aj Burt - 6 months ago 22
Ruby Question

Getting a NoMethodError for user_id, but only on deploy (Rails)

This is working perfectly in production, but keeps messing up on deploy. Strange thing is that I have another model set up similarly with the same code, and it works fine on deploy.

Controller:

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


Error:

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


Meanwhile, in production, there is no issue with the commit:

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


I double checked to make sure I had run rake db:migrate on deploy.

I do not think it is an issue with current_user as I am definitely logged in as a user – I would not even be able to access the new form as I'm requiring current_user to view the form.

Really appreciate any insight you might have.

Answer

I suggest to add user_id as hidden field in new form like :

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

OR a better way to do it like :

@task = current_user.task.new(task_params)

as it is a better approach & will avoid this error situation.

Comments