简体   繁体   中英

In Rails using Wicegrid, how do I create link_to to destroy a record and return to the grid?

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] "/".

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.

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

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("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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM