javascript - 每个循环内表单上的 Rails Ajax 具有嵌套资源和 html 类

标签 javascript jquery html ruby-on-rails ajax

因此,我尝试在目标内创建任务列表,为了完成它们,我创建了一个带有隐藏字段的 form_for,该隐藏字段将在单击提交时更新每个任务记录的值。我正在尝试将 ajax 添加到表单中,以便当用户点击提交时,它将更新任务的记录,并呈现一个部分,用一个 div 表示“完成!”来替换此表单。

goals_controller.rb

def show
  @goal = Goal.where(user_id: current_user, id: params[:id]).first
  @goal_tasks = @goal.tasks

  # This is where the AJAX request will happen
  respond_to do |format|
    format.html {}
    format.js {}
  end
end

show.html.erb 目标

    <% @goal_tasks.each do |task| %>
        <div class="complete-button">
            <% if task.completed == false %>
                <%= form_for([@goal,task], remote: true) do |f| %>
                    <%= f.hidden_field :completed , :value => true %>
                    <%= f.submit "Complete" , :class => "submit-complete" %>
                <% end %>
            <% else %>
                <%= render partial: "goals/taskcompleted"  %>
            <% end %>
        <div>
    <% end %>

show.js.erb

$(".complete-button").html("<%= j(render partial: 'goals/taskcompleted') %>")

正在发生的行为是,如果有多个任务,则有多个具有唯一 id="edit_task_unique_id" 的表单。在 html 中,但因为我正在抓取 .complete-button show.js.erb 中的类。它将所有形式替换为完整的 div。我试图以某种方式获取表单为每个任务创建的唯一 ID,但没有成功。任何帮助或反馈表示赞赏!

最佳答案

Show是GET类型,你所做的是PUT类型。您可以使用更新方法或创建自定义方法。您甚至不需要表单,因为您不希望用户输入任何内容。最重要的是,我将使用 :completed_at 字段和 datetime 类型。如果为 nil,则未完成,如果不为 nil(有日期时间),则已完成。

路线.rb

resources :goals do
  resources :tasks do
    member do
      patch :complete
    end
  end
end

目标/show.html.erb

 ....
 <%= render @tasks %> # this is a rails convention. Basically it iterates thru the @tasks and renders each task based on _task.html.erb which represents a single task.
 ....

任务/_task.html.erb

#You use task instead of @task as you do within a @tasks.each |task| do block.

<%= task.name %>
<%= task.content %>
<% if task.completed_at.nil? %>
  #here you only need to pass task.id to the path (rails convention again)
  <%= link_to "Complete Task", complete_goal_task_path(id: task.id), action: :complete, remote: true, method: :patch %>
<% end %>

目标 Controller

def show
  # I don't know why you wrote this:
  @goal = Goal.where(user_id: current_user, id: params[:id]).first
  #It should be just this since you are on the show page (You should define the current_user in the view link helper that takes the user on this page.)
  @goal = Goal.find(params[:id])
  @tasks = @goal.tasks
end

任务 Controller

def complete
  @task = Task.find(params[:id])
  @task.update_attribute(:completed_at, Time.zone.now)
  respond_to do |format|
    format.html
    format.js
  end
end

任务/complete.js.erb

//You remove the completed task from the DOM (I guess you wanna do that.)
$("#task_<%= @task.id %>").fadeOut(400, function(){
  $(this).remove();
});
//For flash checkout a stackoverflow question. Something like "how to use flash in js.erb"

架构.rb

create_table "tasks", force: :cascade do |t|
  t.integer  "goal_id",  null: false
  t.string   "name"
  t.text     "content",      null: false
  t.datetime "deadline",     null: false
  t.datetime "completed_at"
  t.datetime "created_at",   null: false
  t.datetime "updated_at",   null: false
end

关于javascript - 每个循环内表单上的 Rails Ajax 具有嵌套资源和 html 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36522306/

相关文章:

javascript - 解决在 Chrome 中使用 jQuery 实时过滤 1500 多个项目的问题

javascript - 如何自动调整/调整 html5 textarea 元素的大小以适合初始内容?

javascript - 从数组中删除值,使用新数组总计更新 html,如果没有总计,则更新 '$'

html - 以特定大小覆盖媒体查询CSS

javascript - Webpack,弄清楚在哪里将 Handlebar 助手注册到我的项目

html - 如何用`display :table-cell` over another div with `display:table-cell?` 重叠一个div的子元素

javascript - 如何访问具有相同索引级别但位于不同父 DIV 中的两个元素?

javascript - 无类别 react 状态?

javascript - colorbox 内的 jQuery 不工作

javascript - 在运行时更改 Cluetip 背景图像