Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Providing status as option doesn't work in 3.1.0 if has_errors? #243

Open
unavailabl3 opened this issue Apr 13, 2023 · 0 comments
Open

Providing status as option doesn't work in 3.1.0 if has_errors? #243

unavailabl3 opened this issue Apr 13, 2023 · 0 comments

Comments

@unavailabl3
Copy link

After upgrading from 3.0.1 to 3.1.0 I found some changes in logic regarding providing status to respond_with.
I need to change status for a couple of actions for Hotwire/Turbo usage. There is new configuration option responders.error_status, but there is no need to set it as a default for the whole application in some cases.
I have update action in my controller:

def update
  @product.update(product_params) #it will add errors on invalid params

  respond_with @product, location: [:dashboard], action: :show, status: 422
end

When I was using 3.0.1 if it has errors it returns status 422, as provided.
After moving to 3.1.0 this doesn't work and status set as default 200 OK.
I didn't find in changelog that providing status via options doesn't work any more.

I found possible problem inside error_rendering_options:

def to_html
  default_render
rescue ActionView::MissingTemplate => e
  navigation_behavior(e) # <-- here
end

def navigation_behavior(error)
  if get?
    raise error
  elsif has_errors? && default_action
    render error_rendering_options # <-- here
  else
    redirect_to navigation_location, status: redirect_status
  end
end

def error_rendering_options
  if options[:render]
    options[:render]
  else
    { action: default_action, status: error_status } # <-- here
  end
end

It works as previously when I tried to change it locally to:

{ action: default_action, status: options[:status] || error_status }

And probably for redirects there is possibility of the same issue and, it could be:

redirect_to navigation_location, status: (options[:status] ||  redirect_status)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant