Rails Ajax完成200 OK,但记录没有更新

时间:2015-12-31 21:27:22

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

我是Rails的新手,我正在尝试使用AJAX更新我的应用中的“里程碑”项目。里程碑属于项目。我已经能够成功实现创建和删除的AJAX请求,但我遇到了更新问题。

当我提交表单时,它说它由MilestonesController#update作为JS处理,状态为Completed 200 OK,并且create.js.erb运行,但里程碑实际上并没有更新。

以下是相关文件,请告知我是否可以提供任何其他背景信息,并提前感谢您提供的任何帮助!

从终端,选中复选框以更新已完成为真:

Started PATCH "/projects/clustertruck-branding-design-and-development/milestones/24" for 127.0.0.1 at 2015-12-31 16:12:04 -0500
Processing by MilestonesController#update as JS
Parameters: {"utf8"=>"✓", "milestone"=>{"project_id"=>"7", "completed"=>"true", "completed_date"=>"2015-12-31"}, "commit"=>"✓", "project_id"=>"clustertruck-branding-design-and-development", "id"=>"24"}
User Load (0.1ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
Project Load (0.1ms)  SELECT  "projects".* FROM "projects"  WHERE "projects"."slug" = 'clustertruck-branding-design-and-development'  ORDER BY "projects"."id" ASC LIMIT 1
Milestone Load (0.1ms)  SELECT  "milestones".* FROM "milestones"  WHERE "milestones"."project_id" = ? AND "milestones"."id" = ? LIMIT 1  [["project_id", 7], ["id", 24]]
ActsAsTaggableOn::Tag Load (0.1ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)  [["taggable_id", 24], ["taggable_type", "Milestone"]]
  Rendered milestones/_milestone.html.erb (7.2ms)
  Rendered milestones/update.js.erb (11.3ms)
Completed 200 OK in 18ms (Views: 14.7ms | ActiveRecord: 0.3ms)

的routes.rb

get 'tags/:tag', to: 'milestones#index', as: :tag

root :to => "projects#index"
resources :memberships
resources :projects do
  resources :milestones
end

update.js.erb

<% if @milestone.valid? %>
    $('li#milestone-<%= @milestone.id %>').first().after("<%= j render @milestone %>");
    $('li#milestone-<%= @milestone.id %>').first().remove();
    $('li#month-marker-<%= @milestone.id %>').hide();
    $('li#month-marker-<%= @milestone.id %>').first().show();
    $(".modal").modal("hide");
    $("body").removeClass("modal-open");
    $(".modal-backdrop").remove();
<% else %>
    alert("<%= @milestone.errors.full_messages.join(', ').html_safe %>");
<% end %>

milestones_controller.rb     class MilestonesController&lt; ApplicationController中

  before_action :require_user, only: [:index, :show]
  before_action :require_editor, only: [:new, :create, :edit, :update, :destroy]
  before_filter :load_project
  before_filter :load_milestone, only: [:show, :edit, :update, :destroy]

  def index
    @milestones = Milestone.all
  end

  def show
  end

  def edit
  end

  def update
  end

  def new
    @milestone = Milestone.new
  end

  def create
    @milestone = Milestone.new(milestone_params)
    @milestone.save
  end

  def destroy
    @milestone.destroy
  end

  private
    def milestone_params
      params.require(:milestone).permit(:name, :description, :completed, :date, :completed, :completed_date, :project_id, :tag_list)
    end

    def load_project
      @project = Project.friendly.find(params[:project_id])
    end

    def load_milestone
      @milestone = @project.milestones.find(params[:id])
    end

end

_milestone.html.erb

<% if @monthmarker != milestone.date.strftime("%B") %>
  <% @monthmarker = milestone.date.strftime("%B") %>
  <li class="month-marker" id="month-marker-<%= milestone.id %>"><%= @monthmarker %></li>
<% end %>

<% completion_classes = ""
completion_classes << " complete" if milestone.milestone_complete?
completion_classes << " incomplete" if milestone.milestone_incomplete?
completion_classes << " overdue" if milestone.milestone_overdue?
%>
<li id="milestone-<%= milestone.id %>" class="card timeline-card<%= completion_classes %>">
  <div class="assignment-bubble">
    <% if milestone %>
    <% else %>
    <% end %>
  </div>
  <div class="card-header">
    <% if current_user && current_user.editor? %>
      <a href="#" class="card-header-edit-link" data-toggle="modal" data-target="#edit-milestone-modal-<%= milestone.id %>">Edit</a>
    <% end %>
    <% if milestone.date.present? && !milestone.completed %>
      Due: <%= milestone.date.strftime("%A, %B %d, %Y") %>
    <% end %>
    <% if milestone.completed && milestone.completed_date.present? %>
      Completed: <%= milestone.completed_date.strftime("%A, %B %d, %Y") %>
    <% end %>
  </div>
  <div class="card-body">
    <% if current_user && current_user.editor? %>
      <% if milestone.completed %>
        <%= form_for [milestone.project,milestone], :remote => true do |f| %>
          <%= f.hidden_field(:project_id) %>
          <%= f.hidden_field(:completed, value: false) %>
          <%= f.submit "✓", :class => "status-checkbox #{completion_classes} mark-complete" %>
        <% end %>
      <% else %>
        <%= form_for [milestone.project,milestone], :remote => true do |f| %>
          <%= f.hidden_field(:project_id) %>
          <%= f.hidden_field(:completed, value: true) %>
          <%= f.hidden_field(:completed_date, value: Date.current) %>
          <%= f.submit "✓", :class => "status-checkbox #{completion_classes} mark-complete" %>
        <% end %>
      <% end %>
    <% else %>
      <div class="status-checkbox view-only-status<%= completion_classes %>">✓</div>
    <% end %>
    <h3 class="name"><%= milestone.name %></h3>
    <p class="description"><%= milestone.description %></p>
    <% if milestone.tag_list.present? %>
      <ul class="tags">
        <li class="tag"><%= raw milestone.tag_list.map { |t| link_to t, tag_path(t) }.join('</li><li class="tag">') %></li>
      </ul>
    <% end %>
  </div>

  <div class="modal fade" id="edit-milestone-modal-<%= milestone.id %>" tabindex="-1" role="dialog" aria-labelledby="milestoneModalLabel">
    <%= form_for [milestone.project,milestone], :remote => true do |f| %>
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="modal-close-btn" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="milestoneModalLabel">Edit Milestone</h4>
          </div>
          <div class="modal-body">
            <% flash.each do |key, value| %>
              <div class="alert alert-<%= key %>"><%= value %></div>
            <% end %>
            <div class="field">
              <%= f.label :milestone %>
              <%= f.text_field :name, :required => true %>
            </div>
            <div class="field">
              <%= f.label :description %>
              <%= f.text_area :description, :required => true %>
            </div>
            <div class="field">
              <%= f.label :date %>
              <%= f.date_field :date, "data-format" => "MM/dd/yyyy", :required => true %>
            </div>
            <div class="field">
              <%= f.label :completed %>
              <%= f.select :completed, options_for_select([['Incomplete' ,''], ['Complete', true]], milestone.completed), :required => true %>
            </div>
            <div class="field">
              <%= f.label :completed_date %>
              <%= f.date_field :completed_date, "data-format" => "MM/dd/yyyy" %>
            </div>
            <div class="field">
              <%= f.label :tag_list, "Tags (separated by commas)" %>
              <%= f.text_field :tag_list %>
            </div>
          </div>
          <div class="modal-footer">
            <% if current_user && current_user.editor? %>
              <%= link_to [milestone.project,milestone], method: :delete,  data: { confirm: 'Are you sure you want to delete this milestone?' }, :class => "btn btn-delete", :remote => true do %>Delete Milestone<% end %>
            <% end %>
            <%= f.submit "Save Milestone", :class => "btn btn-primary" %>
          </div>
        </div>
      </div>
    <% end %>
  </div>

</li>

1 个答案:

答案 0 :(得分:1)

将更新方法更改为以下内容:

def update
  @milestone.update_attributes(milestone_params)
end

现在你的更新方法没有逻辑,所以它显示正常并加载你的里程碑/编辑器,但实际上什么都没有。

您还应该考虑将来扩展它:

respond_to do |format|
  if @milestone.update_attributes(milestone_params)
    format.js   {} #this simply display your javascript
  else
    # Insert some kind of logic here where it displays errors
  end
end