簡體   English   中英

rails 3 - link_to以銷毀無法正常工作

[英]rails 3 - link_to to destroy not working

我正在嘗試創建一個銷毀鏈接到我的用戶控制器,我也在使用設計。

這是我的代碼 -

視圖

<%= link_to 'Delete User?', child, :confirm => "Are you sure you want to delete #{child.full_name}?", :method => :delete, :class => "user-additional", :style => "font-size:12px;font-weight:normal;" %>

調節器

def destroy
 if @user = User.find(params[:id])
  @user.destroy
  respond_to do |format| 
    format.html { redirect_to account_index_path } 
    format.xml { head :ok } 
  end
 end
end

路線

devise_for :users 
resources :users, :except => [:new]

該鏈接轉換為localhost:3000 / users / 10

單擊此按鈕將打開用戶顯示而不是刪除它們

有任何想法嗎 ?

破壞性行為應作為表格提交執行 - http://www.w3.org/2001/tag/doc/whenToUseGet.html#checklist

使用button_to (傳遞:method => :delete )來適當地設置按鈕的樣式。

實際上我昨天遇到了完全相同的問題

嘗試這個:

<%= button_to "delete", your_object, :method=>:delete, :class=>:destroy %>

它起作用(至少對我來說)

如果您使用的是jQuery而不是Prototype,則可能缺少javascript文件。

您可以從jquery-ujs GitHub頁面或Railscasts的第205集中找到有關如何將其添加到項目的詳細信息。

猜測我認為這是因為在Rails 3中,不顯眼的javascript現在用於此類功能(Rails 2會為您的代碼輸出一堆令人討厭的內聯javascript,Rails 3將javascript放在外部文件中,並使用HTML5 data- attributes與之交互。)

要解決此問題,您需要在頁眉中包含<%= csrf_meta_tags %>以引用外部JavaScript。 它還涉及XSS問題。

這里有一些細節: 刪除鏈接在Rails 3視圖中發送“Get”而不是“Delete”

  1. 按照安裝部分rails / jquery-ujs中的步驟操作
  2. 在布局文件中添加<%= javascript_include_tag“application”%>

如果你沒有在你的應用程序中包含jquery和jquery-ujs,默認的link_to默認隨腳手架一起工作!

我有同樣的問題。包括這兩個js后它解決了!

此外,如果您在生產模式中遇到此問題,可能是因為您尚未編譯資產。 請參閱http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets

為我工作的確認信息。

<%= button_to 'Destroy', {action: :destroy, id: version.id},  onclick: 'return confirm("Are you sure?")', method: :delete %>

如果您使用的是jQuery,請確保您擁有以下內容:

<script type="text/javascript">
  // this allows jquery to be called along with scriptaculous and YUI without any conflicts
  // the only difference is all jquery functions should be called with $j instead of $
  // e.g. $jQ('#div_id').stuff instead of $('#div_id').stuff
  var $jQ = jQuery.noConflict();
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM