checkbox_tag rails 4 中的友好 ID 路径

friendly id path in checkbox_tag rails 4

我目前在我的复选框数据中得到这个-url“/todos?id=f483e4a8cb1a728f”,而它应该只是“/todos/f483e4a8cb1a728f”我正在使用友好的随机 ID鼻涕虫。

目前我将其称为数据:{ remote: true, url: url_for(controller: :todos, id: todo), method: "PATCH" } id : todo 我试过 todo.id 但这给了我我不想要的 post 的数量 - 我想要 slug.

有谁知道我该如何解决这个问题?

谢谢各位好心的先生

编辑:根据要求提供更多上下文

<%= check_box_tag 'todo[completed]', todo.id, todo.completed, data: { remote: true, url: url_for(controller: :todos, id: todo), method: "PATCH" }, id: todo.id %>
<%= label_tag todo.id, "COMPLETE", :class => 'strikethrough' %>

我就是这样称呼它的——因为我想在索引上完成我的待办事项,而不是进入并更新待办事项/:id/edit。但是,当我单击复选框时它会给我一个错误,因为 URL 就像这样“/todos?id=f483e4a8cb1a728f”,而它应该只是“/todos/f483e4a8cb1a728f”

编辑:

我的行动

def completed
    if @todo.update_attributes(:completed => params[:completed])
      flash[:success] = "Wowzers."
      redirect_to dashboard_path
    else
      flash.now[:error] = "Not so wowzers..."  
      render :new  
    end
  end

我的路线

resources :todos do
    member do
      # post 'completed'
      patch 'todos/:id' => 'todos#completed'
    end 
  end

首先,你需要一个配置为patch '/todos/:id'的路由。

如果您已经有,请将操作的名称放在 url_for 参数中,例如:

url_for(controller: :todos, action: :something, id: todo.id)

如果您没有该操作,则必须创建它。

url_for 正在返回 /todos?id=f483e4a8cb1a728f,因为它将 index 操作视为默认操作,并且由于此路径在路径中没有 :id 参数,助手将其作为参数(查询字符串)。