Search code examples
ruby-on-railsruby-on-rails-5

Rails returning 500 status after rendering fine and without any explanations


Rendering views that belongs to another action (:show and :edit) from one called :profile. Everything is rendering fine, but the status is set to 500.

I've tried rendering it directly (and get this issue) and also tried redirections (but it does change the URL... and this is not what I want). This happens in both format :html and :json.

Here is my code:

  # GET /users/profile
  # GET /users/profile.json
  def profile
    @user = current_user
    @profile = @user.profile

    authorize @user

    respond_to do |format|
      format.html { render :edit, status: :edit, location: @user }
      format.json { render :show, status: :show, location: @user }
      # format.json { redirect_to user_path(@user, format: :json), location: @user }
    end
  end

Is Rails not working this way anymore Oo ??
I've been looking on the Internet seems to be OK, I don't get it.

If I set some debugs after the respond_to call its appearing fine.
I'd like to have at least some logs tracks explaining what's the issue.

Here are some additional logs showing the error (from development environment):

Started GET "/users/1/edit" for 127.0.0.1 at 2019-03-23 16:00:47 +0100
Processing by UsersController#edit as HTML
  Parameters: {"id"=>"1"}
  User Load (0.9ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Profile Load (0.6ms)  SELECT  "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 LIMIT $2  [["user_id", 1], ["LIMIT", 1]]
  CACHE (0.1ms)  SELECT  "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 LIMIT $2  [["user_id", 1], ["LIMIT", 1]]
  Rendering users/edit.html.slim within layouts/application
  Rendered devise/registrations/_edit.html.slim (7.2ms)
  Rendered users/_form.html.slim (19.2ms)
  Rendered users/edit.html.slim within layouts/application (24.2ms)
Completed 200 OK in 96ms (Views: 77.9ms | ActiveRecord: 2.1ms)


Started GET "/users/profile" for 127.0.0.1 at 2019-03-23 16:01:00 +0100
Processing by UsersController#profile as HTML
  User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Profile Load (0.4ms)  SELECT  "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 LIMIT $2  [["user_id", 1], ["LIMIT", 1]]
  Rendering users/edit.html.slim within layouts/application
  Rendered devise/registrations/_edit.html.slim (7.7ms)
  Rendered users/_form.html.slim (20.5ms)
  Rendered users/edit.html.slim within layouts/application (25.4ms)
Completed 500 Internal Server Error in 85ms (Views: 77.7ms | ActiveRecord: 1.0ms)

Solution

  • In your respond_to block, you set the :status option with :edit and :show.
    These are not valid status codes.

    You can check the list here: Layouts and rendering: the status option