3

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
4

2 回答 2

4

不,这是不可能的。

您必须呈现将更改窗口位置的 javascript:

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

当然,在您的 js 视图中,您可以使用以下方法为 url 使用动态值:

window.location.href = '<%= @a_controller_variable %>';
于 2013-04-18T22:26:12.497 回答
4

您将不得不在 js 而不是控制器中编写此代码。

在控制器中:

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

创建一个 action_name.js.erb 文件并编写以下代码:

<% 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 %>

希望这对你有用。

于 2013-04-19T06:56:48.547 回答