redirect_to不使用ajax

时间:2015-06-04 17:19:22

标签: ruby-on-rails ajax ruby-on-rails-4 redirect

我在bootstrap模式中创建了一个ajax表单,它在我的数据库中创建了一个新帖子。表格是:

<%= form_for @post, remote:true, :html => {:id => "share-needs-form"} do |f| %>
    <div class="modal-body">
        <div id="shareNeedsModalFirstStep">
        <div class="row" style="margin-bottom: 10px">
            <ul class="error-alert hidden share-needs-form-errors col-lg-10 col-centered" id="share-needs-error-alerts"></ul>
    </div>
    <div class="row"style="margin-bottom: 10px;">
        <div class="col-lg-10 col-centered">
            <%= f.text_field(:title, :class => "form-control", :placeholder => "Add a title for e.g I need a designer, in return I can code for you", :id => "title") %>
        </div>
    </div>
    <div class="row" style="margin-bottom: 10px;">
        <div class="col-lg-10 col-centered">
            <%= f.text_area(:description, :rows => "5", :class => "form-control", :id => "description", :placeholder => "Write some description about your idea, so that people who can offer an exchange know what you want to accomplish") %>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-10 col-centered">
            <%= f.text_field(:keywords, :class => "form-control", :id => "keywords", :placeholder => "Add some keywords (not more than 5 and comma(,) separated)") %>
        </div>
    </div>
    </div>
    <div class="row hidden" id="shareNeedsModalSecondStep" >
        <div class="well" style="max-height: 250px; overflow-y: scroll;">
            <div>
            <a href="#" style="font-size: 17px;">I need a designer, in return I can code for you</a>
            <p>I need a designer for a game project, I am working on a small 2D game, I can provide my coding skills for you. I am good at making web applications, and...</p>
            </div>
            <hr />
            <a href="#" style="font-size: 17px;">I need a designer, in return I can code for you</a>
            <p>I need a designer for a game project, I am working on a small 2D game, I can provide my coding skills for you. I am good at making web applications, and...</p>
            <hr />
            <a href="#" style="font-size: 17px;">I need a designer, in return I can code for you</a>
            <p>I need a designer for a game project, I am working on a small 2D game, I can provide my coding skills for you. I am good at making web applications, and...</p>
        </div>
    </div>
    </div>
    <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button type="button" class="btn btn-warning hidden" id="shareNeedsModalBackButton">Back</button>
    <button type="button" class="btn btn-warning" id="shareNeedsModalContinueButton">Continue</button>
    <%= submit_tag "Share", :class => "btn hidden btn-warning", :id => "shareNeedsModalFormSubmitButton" %>
    </div>
<% end %>

这是相应的控制器:

def create
    @post = Post.new(post_params)
    if @post.save
        redirect_to post_path(@post.id)
    else
        @errors = @post.errors.full_messages
        render :json => @errors.to_json
    end
end

如您所见,create方法通过从post_params收到的表单字段创建新帖子。 else块工作正常,这意味着使用以下javascript返回错误并显示在我的视图中:

$("#share-needs-form").bind("ajax:complete", function(evt, data, status, xhr) {
    console.log(data.responseText);
    if(typeof data == "object") { //errors were returned as a json "object"
        $("#shareNeedsModalBackButton").click();
        var form_errors_holder = $("#share-needs-error-alerts");
        form_errors_holder.removeClass("hidden");
        form_errors_holder.html("");
        var errors = data;
        for(var i = 0; i < errors.length; i++) {
            form_errors_holder.append("<li>" + errors[i] + "</li>");
        }
    }
});

但是在控制器成功保存帖子时,它不会重定向到我的post_path,而是返回我post_path页面的整个html。为什么重定向不起作用?我该如何解决这个问题?请帮忙。

1 个答案:

答案 0 :(得分:4)

实际上,您正在发送js请求。因此,服务器以js格式向您发送响应。 为此,您必须在create方法中编写响应格式,如下所示。

def create
  @post = Post.new(post_params)
  respond_to do |format|
    if @post.save
      format.html { redirect_to @post }
      format.js
    else
      format.html { render :new }
      format.js
    end
  end
end

然后,创建文件app / views / posts / create.js.erb并编写以下代码。

<% if @post.errors.empty? %>
   window.location = "<%= post_path(@post) %>";
<% end %>