cat
cat

Reputation: 749

Is it possible to use redirect_to when it's called by remote ajax?

Now this action in this controller is called by remote ajax call.
Only when the checkbox[:call] was checked and it's submit,
I'd like make it to redirect to ......_path with flash message.

Is it possible? if possible, how?

controller

.....
if params[:comment][:call] == "1"
    flash[:notice] = "you checked and submit!"
    redirect_to ....._path
else
    ajax page reload
end

view Form (remote with ajax)

<%=form_for(([@community, @comment]), :remote => true, :class => 'form' ) do |f| %>
    <%= f.check_box :call, :label => 'call' %> call
        <div class="form-actions">
            <div class="input-append">
                <%= f.text_field :body, :id => "input", :class => "box" %>  
            <button type="submit" class="btn">submit</button>
        </div>
    </div>
<% end %>

Comment model

attr_accessible :icon, :call
....
def call
    @call ||= "0"
end

def call=(value)
    @call = value
end

Upvotes: 3

Views: 1738

Answers (2)

Aman Garg
Aman Garg

Reputation: 4218

You will have to write this code in js instead of controller.

In controller:

def action_name
  ....
  respond_to do |format|
    format.js
  end
end

Make a action_name.js.erb file and write this code:

<% if params[:comment][:call] == "1" %>
  <% flash[:notice] = "you checked and submit!" %>
  window.location.href = <%= some_url %>
<% else %>
  $("#some_div_id").html("something")    //ajax page reload
<% end %>

Hope this will work for you.

Upvotes: 4

Lazarus Lazaridis
Lazarus Lazaridis

Reputation: 6029

No, it is not possible.

You have to render javascript that will change the window location:

window.location.href = 'http://your new location';

Of course, in your js view, you are able to use dynamic value for the url using:

window.location.href = '<%= @a_controller_variable %>';

Upvotes: 4

Related Questions