开发者

Why Destroy Action is NOT being fired?

I am using Rails 3 and trying to see why the Destroy 开发者_运维知识库action is not being fired! My html fired is defined as follows:

  <%= link_to "Delete", :action => "destroy", :id => article, :method => :delete, :confirm => "are u sure?" %>

And here is the ArticlesController:

def destroy

    @article = Article.find(params[:id]) 
    @article.destroy 

    respond_to do |format|
      format.html { redirect_to articles_url }
    end

  end

When I click on the "Delete" link it takes me to the show action. I am not sure why is that?


Your link_to should be this:

<%= link_to "Delete", @article, :method => :delete, :confirm => "are u sure?" %>

This will generate the correct URL for your article and go to the destroy action.


Have you deleted the javascripts directory which resides in the public directory of your rails app?

If yes, create a new rails project and copy the javascripts directory in your actual project


Make sure your application.js has included this line:

//= require jquery_ujs

That solved my issue.


That first line should be

@article = Article.find(params[:id])

Once you make this change, you won't be able to redirect to @article, since it will be gone; you'll probably need to replace that last line with redirect_to articles_url. The whole new method will be:

def destroy
  @article = Article.find(params[:id]) 
  @article.destroy 

  respond_to do |format|
    format.html { redirect_to articles_url}
  end
end

If you have trouble with stuff like this, try creating a scaffolded model with rails generate scaffold Test to see what the default options are.


It could also be this Why are default javascript files required to create a destroy link in rails?.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜