Change redirect_to format to :js in Rails

rails redirect_to
rails format.js redirect
format json rails
rails render js
format js rails
rails redirect_to format
rails respond_to

I have a controller action that should only be accessed by users that are logged in. If the user is not logged in, I want to redirect them to the login form, which an ajax-powered lightbox. Although the initial request is in html format, I need to change is to js. Right now, I'm trying it like this:

def new
  if user_signed_in?
    @company =
    redirect_to new_user_session_path, format: 'js'

Unfortunately, it's still treating the redirect as though it's in the html format. Is there someway to get the redirect to be treated as js?

I was try this in rails 3.2.13 n it's works, i hope it's can solve

if user_signed_in?
  if request.xhr?
    flash[:notice] = "Please login."
    render :js => "window.location = #{new_user_session_path}"

Rails: how to set json format for redirect_to, I read apidock some more It was quite simple. I just should specify format in path helper like this: redirect_to user_path(@user, format: :json). Working with JavaScript in RailsThis guide covers the built-in Ajax/JavaScript functionality of Rails (and more); it will enable you to create rich and dynamic Ajax applications with ease!After reading this guide, you will know: The basics of Ajax. Unobtrusive JavaScript. How Rails' built-in helpers assist you. How to handle Ajax on the server side. The Turbolinks gem. How to include your

I think you might be looking for a respond_to block along with that before filter mentioned earlier:

class CompaniesController < ApplicationController
  before_filter :login

  def new 
    @company =

    render json: @company, layout: false # layout false if you do not want rails to render a page of json


  def login
    if !user_signed_in?
     #your login code here

Don't forget to set your dataType ajax option to json:

    url: "company/new" //your rails url, what is there is just my best guess
    data: query, //whatever paramaters you have
    dataType: "json", // or html, whichever you desire
    type: "GET",
    success: function (data) {
        //your code here

Rails 5 not responding with :js after redirect · Issue #26279 · rails , On rails 4.2 I used to submit my form via ajax, and redirect to another action for your controller action, as some rendering behavior has changed for Rails 5. as js #in rails 5.0 - responsed as html else respond_to do |format|  If you are using a gem that reads to URI for certain processing (like simple-navigation) the new URI will be '/users' even though you are at the '/welcome' URI (highlight is lost in simple-navigation and submenus are not rendered).

For anyone coming to this and using Rails 5 I did this and this works inside the youractionname.js.erb file if one sets a @redirect variable inside the controller youractionname function when one wants to redirect.

# ruby control flow
<% if @redirect != nil %>
  // javascript call to use rails variable as redirect destination.
<% end %>

Redirecting with 'remote: true' - Arielle Sullivan, I set the form up with remote: true and a js.erb file to send the messages. to successfully redirect with the flash message after implementing the change below: Interpolating the rails path in the request is preferable to hardcoding the url,  Types array shorthand. You can have respond_to blocks that look like this:. respond_to do | format | format. html format. xml end. Here each individual format doesn’t receive a block and so Rails automatically tries to render the appropriate view for the mime type (e.g. action.html.erb, action.xml.erb or as a last resort action.erb)

Redirection in format.json - Rails, I have presently a render :json which modify the current page if if @request.​save format.html { redirect_to(@request) } format.json { render :json => { :result => 'success', :request redirects to :show with a format .js. I thought  First we’ll go through the basics of setting up the Rails app, then adding jQuery and Ajax to add items without reloading the page. Finally, we’ll refactor with remote: true. Feel free to code along! The Basic Rails Todo App. Before getting to Ajax, here are the steps to create the basic rails todo app we’ll be building on: 1) rails new

Layouts and Rendering in Rails, render file: "/path/to/rails/app/views/books/edit.html.erb" JSON is a JavaScript data format used by many Ajax libraries. Rails can render vanilla JavaScript: redirect_to and redirect_back do not halt and return immediately from method  In this case, the only change we will need to make is to the redirect_to statement, to handle successful updates. Update it to redirect to shark_post_path(@shark), which will redirect to the index view of the selected shark’s posts:

Working with JavaScript in Rails, Working with JavaScript in RailsThis guide covers the built-in Ajax/JavaScript functionality of Rails (and more); it will How Turbolinks Works; Page Change Events format.html { redirect_to @user , notice: 'User was successfully created.' }. Because of Unobtrusive JavaScript, the Rails "Ajax helpers" are actually in two parts: the JavaScript half and the Ruby half. Unless you have disabled the Asset Pipeline, rails.js provides the JavaScript half, and the regular Ruby view helpers add appropriate tags to your DOM. 3.1 form_for. form_for is a helper that assists with writing forms.

  • Have you tried a before filter?
  • Thanks for the tip! That cleans up my code a lot, but it doesn't address the main problem I'm having, which is changing the format from html to js. If have any more tips about that, please let me know.
  • I think you might have to redirect them to a page and then trigger the light osx box modal.
  • @simonmorley This is the approach I ended up using.
  • Unfortunately, in 4.0 this doesn't work. The js is still rendered as html, so all I get is an unformatted page with the text render :js => "window.location = #{new_user_session_path}". Thanks though.
  • Thanks for the reply. Unfortunately, the problem is that the link to "New Company" is not remote: true, so I'm sending a request in html format, not js. Somehow, I need to tell Rails to return the request as js instead.
  • If you just want json resturned from this controller method, all you have to do is a render json: @company, layout: false. I edited my answer to include this code.
  • Unfortunately, this just isn't working. Perhaps something has changed for Rails 4?