【问题标题】:Rails 4: Toggle link_to with Ajax and jQuery (nested resources)Rails 4:使用 Ajax 和 jQuery 切换 link_to(嵌套资源)
【发布时间】:2015-02-03 08:02:41
【问题描述】:

我在“想法”上有“完整”和“不完整”链接。我想在不重新加载页面的情况下切换它们。

这是我当前的代码:

_idea.html.erb 部分:

<li id="<%= dom_id(idea) %>">
    <%= idea.description %>
    <% if idea.completed? %>
           <%= link_to "Incomplete", complete_project_idea_path(idea.project, idea), method: :patch, remote: true %>
      <% else %>
           <%= link_to "Complete", complete_project_idea_path(idea.project, idea), method: :patch, remote: true %>
      <% end %>
 </li>  

这是我的控制器操作(我正在使用嵌套资源):

def complete
  @project = Project.find(params[:project_id])
  @idea = @project.ideas.find(params[:id])
    respond_to do |format|
     @idea.toggle_completion!
         format.html {redirect_to root_path}
         format.js { }
    end
end

在idea.rb模型中:

def completed?
   !completed_at.blank?
end

def toggle_completion!
    if completed?
         update_attribute(:completed_at, nil)
    else
         update_attribute(:completed_at, Time.now)
    end
end

完整的.js.erb 文件;

$('#<%= dom_id(@idea)%>').html("What here? How can I toggle between the complete and incomplete links?");

我也尝试将 link_to 放在新的部分中,但这变得太复杂了(因为它们是子部分)并且也不起作用。

我应该在我的 complete.js.erb 文件中尝试什么?

编辑:谢谢@Fer!我做了以下更改,但它还没有完全工作。

我把 complete.js.erb 改成:

$("#toggle_completion_link").text("<%= @idea.completed? ? 'Incomplete' : 'Complete' %>")

我将想法改为@idea,因为没有它我得到了一个'未定义的局部变量或方法`idea''错误。

同样在projects/idea.html.erb下的_idea.html.erb部分,我改为:

 <%= link_to (idea.completed? ? 'Incomplete' : 'Complete'), complete_project_idea_path(idea.project, idea), method: :patch, remote: true, id: 'toggle_completion_link' %>

但它还不能正常工作,尽管我确实收到了“完成 200 ok”的消息。

Started PATCH "/projects/1/ideas/4/complete" for ...
Processing by IdeasController#complete as JS
Parameters: {"project_id"=>"1", "id"=>"4"}
User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
Project Load (0.2ms)  SELECT  "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT 1  [["id", 1]]
Idea Load (0.2ms)  SELECT  "ideas".* FROM "ideas" WHERE "ideas"."project_id" = $1 AND "ideas"."id" = $2  ORDER BY priority ASC  LIMIT 1  [["project_id", 1], ["id", 4]]
Rendered ideas/complete.js.erb (0.5ms)
Completed 200 OK in 9ms (Views: 3.3ms | ActiveRecord: 1.8ms)

【问题讨论】:

  • 到底发生了什么?

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


【解决方案1】:

好吧,在您的情况下,您只需要更改链接文本,因为两个链接都指向相同的 url 和方法。

我会有点像在_idea.html.erb 中关注它

<li id="<%= dom_id(idea) %>">
    <%= idea.description %>
    <%= link_to (idea.completed? ? 'Incomplete' : 'Complete'), complete_project_idea_path(idea.project, idea), method: :patch, remote: true, id: 'toggle_completion_link' %>
 </li>

注意最后链接的id 属性。

如果您显示多个项目并且您有 id 问题,我会使用 dom_id(idea) 作为链接的 id(或类似 dom_id(idea) + '_link' 的一些变体),而不是包含它的 li

然后,作为您的 ajax 请求的答案,在您的 complete.js.erb 文件中:

$("#toggle_completion_link").text("<%= idea.completed? ? 'Incomplete' : 'Complete' %>")

您也可以替换整个内容,但我看不到这里的重点,因为您只需要切换链接文本。无论如何,对于教学,您可以将整个部分呈现如下:

$("#<%= dom_id(idea) %>").replace("<%= escape_javascript(render 'idea', object: @idea %>")

你可能需要调整部分路径,因为你没有说它在哪里

【讨论】:

  • 谢谢@Fer!我试过你的解决方案,还不能让它工作。正如你所说,问题可能是我展示了多个项目并且有 id 问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2011-07-09
  • 2017-08-05
  • 1970-01-01
相关资源
最近更新 更多