在rails中,链接重定向到SHOW而不是DESTROY

时间:2015-03-20 10:17:13

标签: ruby-on-rails rest

在相应的控制器中销毁

def destroy
  if GovtSubsidy.find(params[:id]).destroy 
    redirect_to :action => 'index'
    flash[:success] = "Successfully Deleted!"
  else
    flash[:error] = "Sorry! Could not complete the request, please try again!"
    redirect_to :action => 'index'
  end
end 

这是index.html.erb

       <% @govtsubsidy.each do |f| %>
        <tr>

            <td><%= f.id %></td>
            <td><%= f.society.name %></td>
            <td><%= f.opening_balance %></td>
            <td><%= f.recieved_balance %></td>
            <td><%= f.total_oustanding %></td>

            <td>
                <%= link_to "<span class='glyphicon glyphicon-edit'></span>".html_safe,   edit_admin_govt_subsidy_path(f) %>
            </td>
            <td>
                <%= link_to "<span class='glyphicon glyphicon-trash'></span>".html_safe, admin_govt_subsidy_path(f), :method => :delete, :title => "Delete Government Assistance Category", "data-confirm" => "Do you really want to delete?" %>
            </td>
        </tr>
        <% end %>

在routes.rb

          resources :govt_subsidies

在佣金路线

 admin_govt_subsidies GET    /admin/govt_subsidies(.:format)                                          admin/govt_subsidies#index
                                      POST   /admin/govt_subsidies(.:format)                                     admin/govt_subsidies#create
                 new_admin_govt_subsidy GET    /admin/govt_subsidies/new(.:format)                        admin/govt_subsidies#new
              edit_admin_govt_subsidy GET    /admin/govt_subsidies/:id/edit(.:format)                   admin/govt_subsidies#edit
                   admin_govt_subsidy GET    /admin/govt_subsidies/:id(.:format)                        admin/govt_subsidies#show
                                      PATCH  /admin/govt_subsidies/:id(.:format)                        admin/govt_subsidies#update
                                      PUT    /admin/govt_subsidies/:id(.:format)                        admin/govt_subsidies#update
                                      DELETE   /admin/govt_subsidies/:id(.:format)                          admin/govt_subsidies#destroy

编辑 - 为删除链接呈现html

<a data-confirm="Do you really want to delete?" 
   data-method="delete" 
   href="/admin/govt_subsidies/2" 
   rel="nofollow" 
   title="Delete Government Assistance Category">
     <span class='glyphicon glyphicon-trash'></span>
</a>

已包含Javascript。以下是所有包含的javascript文件。有些正在重复。这就是为什么js文件冲突并请求转到GET而不是DESTROY

      <link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
        <link href='http://fonts.googleapis.com/css?family=Exo+2:400,700' rel='stylesheet' type='text/css'>
       <link href='http://fonts.googleapis.com/css?family=Raleway:600,400,300' rel='stylesheet' type='text/css'>
  <link data-turbolinks-track="true" href="/assets/dataTables/bootstrap/3/jquery.dataTables.bootstrap.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/dataTables/jquery.dataTables.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/jquery-ui/theme.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/jquery-ui/core.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/jquery-ui/datepicker.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/normalize.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/bootstrap.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/main.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/tabs.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/tabstyles.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/style.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/application.css?body=1" media="all" rel="stylesheet" />

  <script data-turbolinks-track="true" src="/assets/jquery.min.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/turnBox.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/main.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/retina.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/bootstrap.min.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/jquery.easing.1.3.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/smoothscroll.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/jquery-func.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/dots.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/modernizr-2.6.2.min.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/turbolinks.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/jquery-ui/core.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/jquery-ui/datepicker.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/jquery.js?body=1"></script>
**<script data-turbolinks-track="true" src="/assets/jquery_ujs.js?body=1"></script>**
<script data-turbolinks-track="true" src="/assets/dataTables/jquery.dataTables.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/dataTables/bootstrap/3/jquery.dataTables.bootstrap.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/data-confirm-modal.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/taluks.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/ceos.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/progress_charters.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/societies.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin-layout.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/a_class_members.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/a_class_reserved_counts.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/a_class_type_counts.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/accrued_funds.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/annual_general_bodies.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/assettypes.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/b_government_members.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/bordelection_dates.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/borrowings.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/branches.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/c_nominal_members.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/collected_deposits.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/d_associate_members.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/dashboard.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/dcb_all_loans.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/detail_of_directors.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/election_dates.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/employee_counts.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/establishment_details.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/goverment_assistances.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/government_assistance_categories.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/govt_loans.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/govt_subsidies.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/investments.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/loan_advance_categories.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/major_activities.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/non_credit_businesses.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/owned_assets.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/rented_assets.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/self_helf_groups.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/self_help_groups.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/social_activities.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/society_assets.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/society_category.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/society_status.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/users.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/villages.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/yashashwini_health_scheme.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/admin/yashaswini_health_schemes.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/annual_general_bodies.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/cbpFWTabs.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/election_dates.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/index.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/interact.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/modernizr.custom.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/npm.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/plugins.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/societies.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/application.js?body=1"></script>

1 个答案:

答案 0 :(得分:0)

好像您忘记按jquery-rails gem中的建议加载jquery-ujs资产。 jquery-ujs在客户端是handle data-method属性,并向服务器发出对应的请求类型。

所以,如果没有它,你的链接就会发出GET请求而不是删除。

Here是更多详情。