0

I am trying to delete my interface,and it said

No route matches {:action=>"show", :controller=>"interfaces", :project_id=>#}, missing required keys: [:id].

Because I do not know which arguments should be put into <%=link_to '删除', project_interface_path()%>

I have tried many different arguments into a path.

interface_controller.rb

def destroy
  @interface = @project.interfaces.find(params[:project_id])
  redirect_to project_interfaces_path if @interface.destroy
end

def index
  @interfaces = Interface.all
  @project = Project.find(params[:project_id])
end

def new
  @project = Project.find(params[:project_id])
  @interface = Interface.new
end

def create
  @project = Project.find(params[:project_id])
  @interface = @project.interfaces.new(interface_params)
  if @interface.save
    redirect_to project_interfaces_path
  else
    render :new
  end
end

interface/index.html.erb

<% @interfaces.each do |interface| %>
  <tr>
    <th scope="row">1</th>
    <td><%=interface.name %></td>
    <td><%=interface.desp %></td>
    <td><%=interface.method_type_id %></td>
    <td><%=interface.request_url %></td>
    <td><%=interface.request_eg %></td>
    <td><%=interface.response_eg %></td>
  </tr>
  <td><%= link_to '删除', project_interface_path(interface),method: :delete, data:{confirm:'确定要删除吗?'}, class: 'badge badge-dark' %></td>

This is my route file:

Rails.application.routes.draw do

  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root "method_types#index"

  resources :method_types
  resources :field_types
  resources :projects do
    resources :interfaces
  end

end
lenz
  • 5,658
  • 5
  • 24
  • 44
Hao Wen
  • 25
  • 7

1 Answers1

0

Try this:

<%= link_to '删除', project_interface_path(@project.id, interface.id),method: :delete, data:{confirm:'确定要删除吗?'}, class: 'badge badge-dark' %></td>
Pragya Sriharsh
  • 529
  • 3
  • 6
  • Your answer is correct and it has shown delete button. But after clicking delete button it shows undefined method `interfaces' for nil:NilClass – Hao Wen Apr 29 '19 at 18:56
  • @HaoWen that is because you do not set `@project` in the destroy action like you do in say the new action ` @project = Project.find(params[:project_id])` – engineersmnky Apr 29 '19 at 18:59
  • It means @project contain nil value. Debug and check the value of this. May be you are not including :destroy in your callback that find the project_id and assigned to theinstance variable. Otherwise you can do this in destroy function too. – Pragya Sriharsh Apr 29 '19 at 19:03
  • @project = Project.find(params[:project_id]) ........... interface = @project.interfaces.find(params[:id]) ........... interface.destroy – Pragya Sriharsh Apr 29 '19 at 19:07