Rails新模型Ajax响应

I'm trying to make a simple example on a new form to send the notice of success when creating a new item using ajax. I'm reciving a response from the server with 500 internal error.

new.js.erb

$("#flash-messages").html("<%= notice %>")

new.html.erb

<h1>New Ajax</h1>

<%= form_for(@ajax, remote: true) do |f| %>
<div id="flash-messages" >
</div>
<% if @ajax.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@ajax.errors.count, "error") %> prohibited this ajax from being saved:</h2>

  <ul>
  <% @ajax.errors.full_messages.each do |message| %>
    <li><%= message %></li>
  <% end %>
  </ul>
</div>
  <% end %>

<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

controller

  def create
  @ajax = Ajax.new(ajax_params)

respond_to do |format|
  if @ajax.save
    format.html { redirect_to @ajax, notice: 'Ajax was successfully created.' }
    format.json { render :show, status: :created, location: @ajax }
    format.js { redirect_to new_ajax_path, flash[:notice] = "Fudeu" }
  else
    format.html { render :new }
    format.json { render json: @ajax.errors, status: :unprocessable_entity }
  end
end

end

查看全部
weixin_33695450
weixin_33695450
2016/02/26 13:26
  • ruby-on-rails
  • ajax
  • javascript
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信

1个回复