How do I prevent rails from redirecting to request referrer after create

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How do I prevent rails from redirecting to request referrer after create

fugee ohu
I'm broadcasting to a javascript channel in my create action instead of redirecting but rails redirects to request referrer

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1b7ecb28-06c2-4638-9a37-221bf911da28%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: How do I prevent rails from redirecting to request referrer after create

Walter Lee Davis
You can define (in your controller) what the create action is after a successful save, and if you only want to do that for one format, and not the others, you can use the usual method for doing that in a controller, with:

if @foo.create(foo_params)
  respond_to do |format|
    if format.js
      render status: :ok
    else
      redirect_to @foo
    end
  end
else
  render :new
end

Using render status: :ok on success will not let anything else happen, no redirect, nothing changes on screen.

Walter


> On Jan 27, 2020, at 8:48 PM, fugee ohu <[hidden email]> wrote:
>
> I'm broadcasting to a javascript channel in my create action instead of redirecting but rails redirects to request referrer
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1b7ecb28-06c2-4638-9a37-221bf911da28%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0DFFE05C-4FD0-468A-9086-2FD0B6420749%40wdstudio.com.
Reply | Threaded
Open this post in threaded view
|

Re: How do I prevent rails from redirecting to request referrer after create

fugee ohu


On Monday, January 27, 2020 at 10:52:28 PM UTC-5, Walter Lee Davis wrote:
You can define (in your controller) what the create action is after a successful save, and if you only want to do that for one format, and not the others, you can use the usual method for doing that in a controller, with:

if @foo.create(foo_params)
  respond_to do |format|
    if format.js
      render status: :ok
    else
      redirect_to @foo
    end
  end
else
  render :new
end

Using render status: :ok on success will not let anything else happen, no redirect, nothing changes on screen.

Walter


> On Jan 27, 2020, at 8:48 PM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="fSu7f0VYEgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
> I'm broadcasting to a javascript channel in my create action instead of redirecting but rails redirects to request referrer
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="fSu7f0VYEgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/1b7ecb28-06c2-4638-9a37-221bf911da28%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/1b7ecb28-06c2-4638-9a37-221bf911da28%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/1b7ecb28-06c2-4638-9a37-221bf911da28%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/1b7ecb28-06c2-4638-9a37-221bf911da28%40googlegroups.com.

Thanks I solved that problem by removing the respond_to do |format| block and putting  respond_to :js, only: [:create] after my callbacks

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f920ca6e-b9a1-4387-84e4-0b07526ae26c%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: How do I prevent rails from redirecting to request referrer after create

fugee ohu
In reply to this post by Walter Lee Davis


On Monday, January 27, 2020 at 10:52:28 PM UTC-5, Walter Lee Davis wrote:
You can define (in your controller) what the create action is after a successful save, and if you only want to do that for one format, and not the others, you can use the usual method for doing that in a controller, with:

if @foo.create(foo_params)
  respond_to do |format|
    if format.js
      render status: :ok
    else
      redirect_to @foo
    end
  end
else
  render :new
end

Using render status: :ok on success will not let anything else happen, no redirect, nothing changes on screen.

Walter


> On Jan 27, 2020, at 8:48 PM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="fSu7f0VYEgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
> I'm broadcasting to a javascript channel in my create action instead of redirecting but rails redirects to request referrer
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="fSu7f0VYEgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/1b7ecb28-06c2-4638-9a37-221bf911da28%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/1b7ecb28-06c2-4638-9a37-221bf911da28%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/1b7ecb28-06c2-4638-9a37-221bf911da28%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/1b7ecb28-06c2-4638-9a37-221bf911da28%40googlegroups.com.


Cool Thanks I had solved this by removing respond_to and rails automatically returned 204 No content because there was no view Maybe that's not a good hack though with no respond_to

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/408898fb-3122-4cc4-9df6-72a6e6333086%40googlegroups.com.