Ruby in Rails,destroy在编辑时不起作用。路由显示

时间:2014-02-23 16:16:29

标签: ruby-on-rails ruby routing edit destroy

我遇到了破坏行为的问题。 它在index.html.erb中工作得非常好。 但是我无法在edit.html.erb中使用它 即使没有要求许可,它也会路由到节目。

这里的许多解决方案都说它与jQuery有关。嗯,你可以看到,我尝试了所有我能找到的。

<%= stylesheet_link_tag    "application" %>
    <!--Delete Problem in Edit-->
    <%#= javascript_include_tag :all %>
    <%#= javascript_include_tag :application %>
    <%#= javascript_include_tag :default %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>

是否有适合我情况的解决方案?

这是编辑中的删除按钮:

<%= content_tag(:a, :href => contact_path(@contact), :class => "btn btn-warning pull-right", :style => "margin:0; margin-right:15px;", confirm: 'Are you sure?', method: :delete) do %>
<%= t "list.button_delete" %>
        <%#= link_to I18n.t(".list.delete"), contact, confirm: 'Are you sure?', method: :delete %>
        <%#= link_to 'delete', contact_path(@contact), :method => :delete %>
<% end %>

我也尝试了很多东西。

以下是行动:

def destroy
    #@contact = Contact.find(params[:id])
    @contact = current_user.contacts.find(params[:id])
    @contact.destroy
    redirect_to contacts_url,
                            alert: 'Successfully deleted the contact'
end

2 个答案:

答案 0 :(得分:1)

destroy操作中替换:

@contact = current_user.contacts.find(params[:id])

使用:

@contact = @current_user.contacts.find(params[:id])

答案 1 :(得分:1)

替换

<%= content_tag(:a, :href => contact_path(@contact), :class => "btn btn-warning pull-right", :style => "margin:0; margin-right:15px;", confirm: 'Are you sure?', method: :delete) do %>

<%= content_tag(:a, :href => contact_path(@contact), :class => "btn btn-warning pull-right", :style => "margin:0; margin-right:15px;", data: {confirm: 'Are you sure?'}, data: {method: :delete}) do %>

应该是data: {confirm: 'Are you sure?'}而不是confirm: 'Are you sure?'以及data: {method: :delete}而不是method: :delete

其中,link_to方法将confirm: 'Are you sure?'插入为data-confirm="Are you sure?",将method: :delete插入为data-method="delete"

但是content_tag方法将confirm: 'Are you sure?'插入为confirm="Are you sure?",将method: :delete插入method="delete",以便不会调用您的javascript调用。