redirect_to不会重定向到指定的路径(jQueryMobile / Rails 3)

时间:2014-03-13 04:53:06

标签: ruby-on-rails ruby ruby-on-rails-3 jquery-mobile

我在def create中调用redirect_to:

class M::TodosController < ApplicationController
  layout 'm/application'

  def create
    @todo = @goal.todos.build(params[:todo])
     ...
    puts "XXXXX => " +  m_goal_todo_path(@goal, @todo)
    redirect_to m_goal_todo_path(@goal, @todo)
  end
...

我检查routes =&gt;耙路线:

m_goal_todo GET    /m/goals/:goal_id/todos/:id(.:format)                          m/todos#show

在我的日志中:

XXXXX => /m/goals/46/todos/358


Started POST "/m/goals/46/todos" for 127.0.0.1 at 2014-03-13 13:47:14 +0900
Processing by M::TodosController#create as */*
  Parameters: {"todo"=>{"content"=>"23"}, "goal_id"=>"46"}
  SQL (0.4ms)  DELETE FROM "authentication_tokens" WHERE (logged_in_until < '2014-03-13 13:47:14.014105')
  AuthenticationToken Load (0.5ms)  SELECT "authentication_tokens".* FROM "authentication_tokens" WHERE "authentication_tokens"."token" = '5aTg7cWiDXAJj8IHd2NePqat1fNaXc0Nu9kAtS2PF6N8TDR8T5IeZYUZsT4Iy7dkuyy5FkmO_qyDuXX8' LIMIT 1
...

&安培;仍然没有重定向到 - &gt; / m / goals / 46 / todos / 358它来到/ m / goals / 46 /

我正在使用Rails 3.2.13&amp; jQueryMobile

+++ UPDATE +++

尝试

def create
  @todo = @goal.todos.build(params[:todo])
   ...
   puts "XXXXX => " +  m_goal_todo_path(@goal, @todo)
   respond_to do |format|
     format.html { redirect_to m_goal_todo_path(@goal, @todo)}
   end
end

获得相同的行为。

另外,我尝试通过添加rel =“external”来禁用Ajax,而不是更改

<a href="#" class="add center" data-role="button" data-rel="back" data-mini="true" data-inline="true" rel="external">やるべきことを追加</a>

+++ UPDATE 2 +++

new.html.erb

<div class="center">
  <a href="#" class="add center" data-role="button" data-rel="back" data-mini="    true" data-inline="true" data-ajax="false">やるべきことを追加</a>
</div>

和javascript

  $(document).on('click', '.todos_new_page .add', function() {
    var $this = $(this);
    var goalId = $this.closest('.todos_new_page').data('goal-id');
    var $content = $this.closest('[data-role=content]').find('#content');
    if ( $content.val() == "") {
      alert("記入して下さい");
      return false;
    }
    var content = $content.val();
    $.ajax({
      url: '/m/goals/' + goalId + '/todos',
      type: 'POST',
      data: {todo: {content: content}},
      error: defaultAjaxError
    });
  });

2 个答案:

答案 0 :(得分:1)

在Jquery中,移动视图不会发生变化,因为它完全基于Ajax。你需要指定这样的东西

def create
  @todo = @goal.todos.build(params[:todo])
   ...
   puts "XXXXX => " +  m_goal_todo_path(@goal, @todo)
   respond_to do |format|
     format.html { redirect_to m_goal_todo_path(@goal, @todo)}
   end
end

或者您需要为使用data -ajax =&#34; false&#34;

调用create action的表单禁用Ajax

答案 1 :(得分:0)

因为我有这种代码的和平 - &gt; data-rel="back"我被重定向到/m/goals/46/todos/358

所以,我删除了它:)&amp;将⬇添加到我的ajax电话

success: function(link) {
    $.mobile.changePage('/m/'+link.go);
  }

link.go - &gt; render "/m/goals/_update_goal_html", json: { go: "/goals/" + @goal.id.to_s + "/todos/" + @todo.id.to_s}

正常工作:)

Babasaheb Gosavi,谢谢你的帮助。