本文介绍了轨道4:自定义操作只更新参数与AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails 4的应用程序,我有一个日历发布模式,使用路线:

In my Rails 4 app, I have a Calendar and a Post models, using shallow routes:

resources :calendars do
  resources :posts, shallow: true
end

一个日历的has_many 后期和后 belong_to 日历。

A calendar has_many post and a post belong_to a calendar.

我已经使用上的发表了帖子#更新行动AJAX调用 show.html.erb 视图更新定制:批准的帖子参数:

I am already using an AJAX call on the Posts#Update action in the post show.html.erb view to update the custom :approval param of a post:

  • 的respond_to办|格式| format.js结束在我的帖子#更新控制器
  • 在我有一个 update.js.erb 视图应用程序/视图/职位/ 重新加载相应章节职位 show.html.erb 视图
  • 当然,我的链接设置为远程:真正的
  • I have respond_to do |format| format.js end in my Posts#Update controller
  • I have an update.js.erb view in app/views/posts/ to reload the corresponding section of the post show.html.erb view
  • And of course, my links are set with remote: true

现在,我需要实现类似的功能,更新定制:的批准后参数,但在日历 show.html.erb 图,其中显示了所有的帖子。

Now, I need to implement a similar feature, to update the custom :approval param of a post, but from the calendar show.html.erb view where all posts are displayed.

这是目前我在我的日历 show.html.erb 查看:

This is currently what I have in my calendar show.html.erb view:

<td class="cell_content_center post_approval_section">
  <% if post.approval == "ok" %>
    <span class="ok_green">
  <% else %>
    <span class="approval_blue" %>
  <% end %>
    <%= link_to post_path(:id => post.id, "post[approval]" => "ok"), remote: true, :method => :patch do %>
      <span class="glyphicon glyphicon-ok" data-toggle="tooltip" data-placement="left" title="Approve Post"></span>
    <% end %>
  </span><br/>
  <% if post.approval == "edit" %>
    <span class="edit_yellow">
  <% else %>
    <span class="approval_blue" %>
  <% end %>
    <%= link_to post_path(:id => post.id, "post[approval]" => "edit"), remote: true, :method => :patch do %>
    <span class="glyphicon glyphicon-repeat" data-toggle="tooltip" data-placement="left" title="Require Edits"></span>
  <% end %>
  </span><br/>
  <% if post.approval == "remove" %>
    <span class="remove_red">
  <% else %>
    <span class="approval_blue" %>
  <% end %>
    <%= link_to post_path(:id => post.id, "post[approval]" => "remove"), remote: true, :method => :patch do %>
    <span class="glyphicon glyphicon-remove" data-toggle="tooltip" data-placement="left" title="To Be Deleted"></span>
  <% end %>
  </span>
</td>

这code允许我实际更新后的自定义:从日历 显示批准参数。 html.erb

This code allows me to actually update the post custom :approval param from the calendar show.html.erb.

但我不知道如何重新加载页面,甚至更好,只需重新加载 post_approval_section D 其中的链接的位置。

But I can't figure out how to reload the page, or even better, just reload the post_approval_section td where the links are located.

我无法再用我过去在发表完全相同的方法 show.html.erb 因为我已经有一个JS响应在帖子#更新操作设置。

I cannot re-use the exact same approach I used on the post show.html.erb since I already have a JS response for the Posts#Update action setup.

我怎样才能使这项工作?

How can I make this work?

---

更新:基于长阮答案,这里是我现在

UPDATE: based on Long Nguyen answer, here is where I am now.

#calendars/show.html.erb and calendars/_post_approval.html.erb

<td class="cell_content_center post_approval_section">
  <% if post.approval == "ok" %>
    <span class="ok_green">
  <% else %>
    <span class="approval_blue" %>
  <% end %>
    <%= link_to post_path(:id => post.id, "post[approval]" => "ok"), remote: true, approval_update: true, :method => :patch do %>
      <span class="glyphicon glyphicon-ok" data-toggle="tooltip" data-placement="left" title="Approve Post"></span>
    <% end %>
  </span><br/>
  <% if post.approval == "edit" %>
    <span class="edit_yellow">
  <% else %>
    <span class="approval_blue" %>
  <% end %>
    <%= link_to post_path(:id => post.id, "post[approval]" => "edit"), remote: true, approval_update: true, :method => :patch do %>
    <span class="glyphicon glyphicon-repeat" data-toggle="tooltip" data-placement="left" title="Require Edits"></span>
  <% end %>
  </span><br/>
  <% if post.approval == "remove" %>
    <span class="remove_red">
  <% else %>
    <span class="approval_blue" %>
  <% end %>
    <%= link_to post_path(:id => post.id, "post[approval]" => "remove"), remote: true, approval_update: true, :method => :patch do %>
    <span class="glyphicon glyphicon-remove" data-toggle="tooltip" data-placement="left" title="To Be Deleted"></span>
  <% end %>
  </span>
</td>

#posts_controller.rb

def update
    if params["approval_update"]
      respond_to do |format|
        format.js { render :action => "update_post_approval" }
      end
    else
      respond_to do |format|
        if @post.update(post_params)
          format.html { redirect_to post_path(@post) }
          format.json { render :show, status: :ok, location: @post }
          format.js
        else
          format.html { render :edit }
          format.json { render json: @post.errors, status: :unprocessable_entity }
        end
      end
    end
  end

  def update_post_approval
    respond_to do |format|
      format.js
    end
  end

#update_post_approval.js.erb

$('td.post_approval_section').html('<%= j render(partial: "calendars/post_approval") %>');

在我的链接之一点击更新后的批准,什么都不会发生在浏览器中。

When I click on one of the links to update a post approval, nothing happens in the browser.

不过,在日志/ development.log ,我有:

Started PATCH "/posts/42?post%5Bapproval%5D=edit" for ::1 at 2015-11-19 19:59:51 -0800
Processing by PostsController#update as JS
  Parameters: {"post"=>{"approval"=>"edit"}, "id"=>"42"}
  [1m[36mUser Load (1.1ms)[0m  [1mSELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1[0m  [["id", 1]]
  [1m[35mPost Load (2.2ms)[0m  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1  [["id", 42]]
  [1m[36m (1.1ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (1.1ms)[0m  UPDATE "posts" SET "approval" = $1, "updated_at" = $2 WHERE "posts"."id" = $3  [["approval", "edit"], ["updated_at", "2015-11-20 03:59:51.203371"], ["id", 42]]
  [1m[36mSQL (3.6ms)[0m  [1mINSERT INTO "versions" ("event", "object", "whodunnit", "created_at", "object_changes", "item_id", "item_type") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"[0m  [["event", "update"], ["object", "---\nid: 42\ncalendar_id: 8\ndate: 2015-11-19 20:19:00.000000000 Z\nsubject: Armistice 2\nformat: Image\ncopy: Joyeux 11 novembre tout le monde. ++\ncreated_at: 2015-11-19 20:19:58.244685000 Z\nupdated_at: 2015-11-20 03:59:44.073894000 Z\nimage_file_name: armistice.jpg\nimage_content_type: image/jpeg\nimage_file_size: 35967\nimage_updated_at: 2015-11-19 20:19:57.792047000 Z\nshort_copy: etg'tg' <<\nscore: \nfacebook: true\ntwitter: true\ninstagram: false\npinterest: false\ngoogle: false\nlinkedin: false\ntumblr: \nsnapchat: \napproval: ok\n"], ["whodunnit", "1"], ["created_at", "2015-11-20 03:59:51.203371"], ["object_changes", "---\napproval:\n- ok\n- edit\nupdated_at:\n- 2015-11-20 03:59:44.073894000 Z\n- 2015-11-20 03:59:51.203371000 Z\n"], ["item_id", 42], ["item_type", "Post"]]
  [1m[35m (2.0ms)[0m  COMMIT
  Rendered posts/_approval.html.erb (0.6ms)
  Rendered posts/update.js.erb (2.1ms)
Completed 200 OK in 59ms (Views: 10.9ms | ActiveRecord: 11.1ms)

我在做什么错在这里?

What I am doing wrong here?

---

推荐答案

我拿出2的思想来处理这种情况:

I come up with 2 ideas to handle this situation:

第一种方式:通过当您更新从日历后在另一个参数 show.html.erb ,所以的link_to 将是:

First way: Pass another param in when you update the post from calendar show.html.erb, so the link_to will be:

<%= link_to post_path(:id => post.id, "post[approval]" => "remove", approval_update: true), remote: true, :method => :patch do %>

然后,在你发布控制器更新的动作,就可以过滤此请求由 approval_update 参数:

Then in you Post controller on update action, you can filter this request by approval_update param:

if params["approval_update"]
  respond_to do |format|
    format.js { render :action => "update_post_approval" }
  end
else
  respond_to do |format|
  ...
  end
end

现在,当您从日历 show.html.erb,邮政控制器将加载 RAILS_ROOT /应用/视图/职位/ update_post_approval.js.erb 而不是 update.js.erb

Now when you update Post from Calendar show.html.erb, Post controller will load RAILS_ROOT/app/views/posts/update_post_approval.js.erb instead of update.js.erb

方式二:创建另一个路由更新后的审批属性,这意味着你也将建立在邮政控制器中的另一个动作处理Ajax更新的情况下的日历

Second way: create another route for update the approval attribute of Post, which mean you will also create another action in Post controller to handle the case of AJAX update in Calendar page.

这篇关于轨道4:自定义操作只更新参数与AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 11:24