2016-05-09 7 views
0

Dies funktioniert perfekt in der Produktion, aber verpatzt die Bereitstellung. Seltsame Sache ist, dass ich ein ähnliches ähnliches Modell mit dem gleichen Code habe, und es funktioniert gut bei der Bereitstellung.Ermitteln eines NoMethodError für user_id, aber nur für deploy (Rails)

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 

Fehler:

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 

Inzwischen gibt in der Produktion, ist kein Problem mit dem Festschreiben:

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 

Ich habe doppelt, um sicherzustellen, hatte ich run rake db: migrate on deploy.

Ich glaube nicht, dass es ein Problem mit current_user ist, da ich definitiv als Benutzer angemeldet bin - ich wäre nicht einmal in der Lage, auf das neue Formular zuzugreifen, da ich current_user das Formular anzeigen muss.

Wirklich schätzen Sie jede Einsicht, die Sie haben könnten.

Antwort

0

Ich schlage vor, user_id als verstecktes Feld wie in neuer Form hinzuzufügen:

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

oder einen besserer Weg, es zu tun:

@task = current_user.task.new(task_params) 

wie es ein besserer Ansatz ist & wird dies vermeiden Fehlersituation.

+1

Vielen Dank, das funktioniert definitiv. Ich wollte verhindern, dass die Benutzer-ID in der Form gesetzt wird, denn selbst wenn sie versteckt ist, können Sie den Wert durch etwas wie die Entwickler-Konsole ändern, aber das wird definitiv funktionieren. Ich wünschte nur, ich könnte herausfinden, warum die Einstellung der Benutzer-ID im Controller für eines meiner Modelle funktionierte, aber nicht für das andere. –

+1

Dann tu es wie '@task = current_user.task.new (task_params)' –

+0

Perfekt, danke! –