gpt4 book ai didi

javascript - rails 4 : update model attribute with AJAX call

转载 作者:行者123 更新时间:2023-11-27 23:39:21 25 4
gpt4 key购买 nike

这是 Rails 4: custom action to update only one param with AJAX 的后续问题和 Rails 4: select DOM element with dynamically generated id .

我无法从我得到的好的答案中找出如何让事情顺利进行,所以我想从头开始。

在我的 Rails 4 应用程序中,我有一个 Calendar和一个 Post模型,设置浅路线:

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

日历has_many帖子和帖子belong_to日历。

在日历中show.html.erb View ,我显示属于日历的所有帖子。

从此日历 show.html.erb View 中,我需要使用 AJAX 调用更新帖子的 :approval 参数,以便仅刷新与此特定帖子的 :approval 相关的 DOM 元素,而不是整个页面。

这是日历show.html.erb中的代码部分这允许我更新:approval帖子的内容:

<tr id="post_row_<%= post.id%>"> 
[...] # Truncated for brivety
<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", approval_update: true), 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", approval_update: true), 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", approval_update: true), remote: true, :method => :patch do %>
<span class="glyphicon glyphicon-remove" data-toggle="tooltip" data-placement="left" title="To Be Deleted"></span>
<% end %>
</span>
</td>
[...] # Truncated for brivety
</tr>

然后,在 posts_controller.rb :

def update
if params["approval_update"]
respond_to do |format|
if @post.update(post_params)
format.js { render :action => "update_post_approval" }
else
format.html { redirect_to :back }
end
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

注意:在这里,我无法使用默认的 Posts#Update操作,因为它已被另一个 AJAX 调用用来更新 :approval帖子中的参数show.html.erb View ,这就是我们渲染 update_post_approval 的原因自定义操作。

然后,我有以下app/views/posts/update_post_approval.js.erb文件:

$("tr#post_row_#{post.id} > td.post_approval_section").html('<%= j render(partial: "calendars/post_approval") %>');

最后,我有以下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", approval_update: true), 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", approval_update: true), 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", approval_update: true), remote: true, :method => :patch do %>
<span class="glyphicon glyphicon-remove" data-toggle="tooltip" data-placement="left" title="To Be Deleted"></span>
<% end %>
</span>
</td>

当我单击 <td class="cell_content_center post_approval_section"> 中的三个链接之一时更新帖子的部分 :approval param,参数实际上已更新,我们可以在服务器日志中看到:

Started PATCH "/posts/38?approval_update=true&post%5Bapproval%5D=ok" for ::1 at 2015-11-20 08:03:50 -0800
Processing by PostsController#update as JS
Parameters: {"approval_update"=>"true", "post"=>{"approval"=>"ok"}, "id"=>"38"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Post Load (0.7ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1 [["id", 38]]
(0.6ms) BEGIN
SQL (1.1ms) UPDATE "posts" SET "approval" = $1, "updated_at" = $2 WHERE "posts"."id" = $3 [["approval", "ok"], ["updated_at", "2015-11-20 16:03:50.888587"], ["id", 38]]
SQL (1.3ms) INSERT INTO "versions" ("event", "object", "whodunnit", "created_at", "object_changes", "item_id", "item_type") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["event", "update"], ["object", "---\nid: 38\ncalendar_id: 6\ndate: 2015-11-17 22:09:00.000000000 Z\nsubject: Subject\nformat: Link\ncopy: https://www.wid.la/\ncreated_at: 2015-11-17 22:09:28.713416000 Z\nupdated_at: 2015-11-20 16:03:20.613597000 Z\nimage_file_name: \nimage_content_type: \nimage_file_size: \nimage_updated_at: \nshort_copy: ''\nscore: \nfacebook: true\ntwitter: false\ninstagram: false\npinterest: false\ngoogle: false\nlinkedin: false\ntumblr: \nsnapchat: \napproval: remove\n"], ["whodunnit", "1"], ["created_at", "2015-11-20 16:03:50.888587"], ["object_changes", "---\napproval:\n- remove\n- ok\nupdated_at:\n- 2015-11-20 16:03:20.613597000 Z\n- 2015-11-20 16:03:50.888587000 Z\n"], ["item_id", 38], ["item_type", "Post"]]
(1.6ms) COMMIT
Rendered calendars/_post_approval.html.erb (0.1ms)
Rendered posts/update_post_approval.js.erb (1.5ms)
Completed 200 OK in 27ms (Views: 9.9ms | ActiveRecord: 6.3ms)

但是,除非我手动刷新页面,否则 View 上不会发生任何变化。

我现在在这个问题上花了几个小时,但无法确定问题是由 Ruby 还是 JS 代码引起的。

如果有必要,我很乐意分享更多代码。

知道我在这里做错了什么吗?

最佳答案

错误在这里:

$("tr#post_row_#{post.id}> td.post_approval_section").html('<%= j render(partial: "calendars/post_approval") % >');

已更新应该是:

$("tr#post_row_<%= @post.id %> > td.post_approval_section").html('<%= j render(partial: "calendars/post_approval", locals: {post: @post}) %>');

关于javascript - rails 4 : update model attribute with AJAX call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33833531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com