在使用Wicegrid的Rails中,如何创建link_to来销毁记录并返回到网格?

时间:2023-02-04 01:19:39

I am using Rails 4 with Wicegrid, I want to have a link on each row of the grid to be able to delete a record, I have created the link_to but when I pressed it I get the error: No route matches [DELETE] "/".

我正在使用带有Wicegrid的Rails 4,我想在网格的每一行上都有一个链接,以便能够删除一条记录,我已经创建了link_to,但是当我按下它时,我得到了一个错误:没有路径匹配[delete]“/”。

I don't want to send it the user to the a delete view, I want to delete the record and refresh the grid and stay in the grid.

我不想将用户发送给a delete视图,我想删除记录并刷新网格并保持在网格中。

My controller looks like:

我的控制器的样子:

def destroy
    @risk = current_user.risks.find(params[:id])
     @risk.destroy
    respond_to do |format|
      format.html { redirect_to risks_url }
      format.json { head :no_content }
    end
end

My Index view looks like this:

我的索引视图是这样的:

<%= 
    grid(@risks_grid) do |g|
       g.column name: 'Id', attribute: 'id'

       g.column name: 'Title', attribute: 'title' do |p|
         p.title if p.title
       end
      .

       g.column do |p|
         link_to("Delete", :back, :data => { :confirm => "Are you sure?" }, :action => :destroy, :id => p.id, :method => :delete, :class => 'button-xs')                

       end
    end
 %>

Thanks, Leon

谢谢,莱昂

1 个解决方案

#1


0  

Your syntax for link_to looks a bit off. You're saying that the action is :back and then specifying specific parts of another action (action, and id). I've never used wicegrid but I would think that something like the following would suffice

link_to的语法有点不太对劲,您说的动作是:返回,然后指定另一个动作(动作,和id)的特定部分。我从来没有使用过wicegrid,但我认为以下内容就足够了。

link_to("Delete", p, data: { confirm: "Are you sure?" }, method: :delete, class: 'button-xs')

If you want to do the deletion via AJAX so that the user doesn't leave the page you'll need to use remote: true in your data attributes:

如果您想通过AJAX进行删除,以便用户不离开页面,您需要在数据属性中使用remote: true:

link_to("Delete", p, data: { remote: true, confirm: "Are you sure?" }, method: :delete, class: 'button-xs')

Then in your destroy.js.coffee or destroy.js.erb you can use jQuery or something similar to make the appropriate updates to the DOM.

然后在你的destroy.js。咖啡或destroy.js。您可以使用jQuery或类似的方法对DOM进行适当的更新。

#1


0  

Your syntax for link_to looks a bit off. You're saying that the action is :back and then specifying specific parts of another action (action, and id). I've never used wicegrid but I would think that something like the following would suffice

link_to的语法有点不太对劲,您说的动作是:返回,然后指定另一个动作(动作,和id)的特定部分。我从来没有使用过wicegrid,但我认为以下内容就足够了。

link_to("Delete", p, data: { confirm: "Are you sure?" }, method: :delete, class: 'button-xs')

If you want to do the deletion via AJAX so that the user doesn't leave the page you'll need to use remote: true in your data attributes:

如果您想通过AJAX进行删除,以便用户不离开页面,您需要在数据属性中使用remote: true:

link_to("Delete", p, data: { remote: true, confirm: "Are you sure?" }, method: :delete, class: 'button-xs')

Then in your destroy.js.coffee or destroy.js.erb you can use jQuery or something similar to make the appropriate updates to the DOM.

然后在你的destroy.js。咖啡或destroy.js。您可以使用jQuery或类似的方法对DOM进行适当的更新。