Я создал форму 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
, вместо этого он возвращает весь html моей страницы post_path
. Почему не работает редирект и как это исправить? Пожалуйста помоги.