Rails:没有路由匹配{:action =>"编辑",:controller =>"讨论"

时间:2015-02-03 00:11:19

标签: ruby-on-rails ruby ruby-on-rails-3 routes

我的讨论嵌套在我的项目中。项目是一个基本的CRUD,我试图使讨论相同。他们最初工作,但现在我试图添加&#34;编辑&#34;和&#34;销毁&#34;我收到此错误:No route matches {:action=>"edit", :controller=>"discussions", :format=>nil, :id=>nil, :project_id=>#<Discussion id: 24, title: "hello", description: "hello", created_at: "2015-02-02 20:58:53", updated_at: "2015-02-02 20:58:53", project_id: 12>} missing required keys: [:id]

这里的代码行<%= link_to "Edit", edit_project_discussion_path(item) %>

discussions_controller.rb

class DiscussionsController < ApplicationController

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

  def create
    @project = Project.find(params[:project_id])
    @discussion = @project.discussions.build(discussion_params)
    if @discussion.save
      redirect_to new_project_discussion_path(@project)
    end
  end

  def edit
    @discussions = Discussion.find(params[:project_id])
  end

  def update
    @discussions = Discussion.find(params[:project_id])

    if @discussions.update_attributes(discussion_params)
      redirect_to new_project_discussion_path
    else
      render "edit"
    end
  end

  def destroy
    @discussions = Discussion.find(params[:project_id])
    @discussions.destroy
    redirect_to new_project_discussion_path
  end

  def discussion_params
    params.require(:discussion).permit(:project_id, :title, :description)
  end

end

_form.html.erb

<%= form_for [@project, @discussion] do |f| %>
  <div class="container">

    Project: <%= @project.title %> <%= link_to "Go back?", projects_path %> 
    <hr>
    <div class="form-group">
      <%= f.label :title %>
      <%= f.text_field :title, class: "form-control" %>
    </div>

    <div class="form-group">
      <%= f.label :description %>
      <%= f.text_area :description, class: "form-control" %>
    </div>

    <div class="form-group">
      <%= f.submit "Submit discussion", class: "btn btn-primary" %>
    </div>
  </div>
<% end %>

edit.html.erb

<%= render "form" %>

new.html.erb

<div class="container">
  <div class="page-header">
    <h1>Discussions<small> Discuss the project.</small></h1>
  </div>
</div>


<%= render "form" %>


<% if !@project.discussions.blank? %>
  <% for item in @project.discussions %>
  <div class="container">
  <div class="panel panel-default">
  <div class="panel-heading">
    <%= item.title %>
  </div>
  </div>
  <div class="panel-body">
  <p>
    <%= item.description %> <br>
    <%= link_to "Comment", new_discussion_comment_path(item) %>
    <%= link_to "Delete", item, :method => :delete %> 
    <%= link_to "Edit", edit_project_discussion_path(item) %> |
  </p>
  </div>
  </div>
  <% end %>
<% end %>

路线:

Controller#Action
        new_user_session GET    /users/sign_in(.:format)                                devise/sessions#new
            user_session POST   /users/sign_in(.:format)                                devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                               devise/sessions#destroy
           user_password POST   /users/password(.:format)                               devise/passwords#create
       new_user_password GET    /users/password/new(.:format)                           devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)                          devise/passwords#edit
                         PATCH  /users/password(.:format)                               devise/passwords#update
                         PUT    /users/password(.:format)                               devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                                 devise/registrations#cancel
       user_registration POST   /users(.:format)                                        devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)                                devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                                   devise/registrations#edit
                         PATCH  /users(.:format)                                        devise/registrations#update
                         PUT    /users(.:format)                                        devise/registrations#update
                         DELETE /users(.:format)                                        devise/registrations#destroy
                   homes GET    /homes(.:format)                                        homes#index
                         POST   /homes(.:format)                                        homes#create
                new_home GET    /homes/new(.:format)                                    homes#new
               edit_home GET    /homes/:id/edit(.:format)                               homes#edit
                    home GET    /homes/:id(.:format)                                    homes#show
                         PATCH  /homes/:id(.:format)                                    homes#update
                         PUT    /homes/:id(.:format)                                    homes#update
                         DELETE /homes/:id(.:format)                                    homes#destroy
     project_discussions GET    /projects/:project_id/discussions(.:format)             discussions#index
                         POST   /projects/:project_id/discussions(.:format)             discussions#create
  new_project_discussion GET    /projects/:project_id/discussions/new(.:format)         discussions#new
 edit_project_discussion GET    /projects/:project_id/discussions/:id/edit(.:format)    discussions#edit
      project_discussion GET    /projects/:project_id/discussions/:id(.:format)         discussions#show
                         PATCH  /projects/:project_id/discussions/:id(.:format)         discussions#update
                         PUT    /projects/:project_id/discussions/:id(.:format)         discussions#update
                         DELETE /projects/:project_id/discussions/:id(.:format)         discussions#destroy
                projects GET    /projects(.:format)                                     projects#index
                         POST   /projects(.:format)                                     projects#create
             new_project GET    /projects/new(.:format)                                 projects#new
            edit_project GET    /projects/:id/edit(.:format)                            projects#edit
                 project GET    /projects/:id(.:format)                                 projects#show
                         PATCH  /projects/:id(.:format)                                 projects#update
                         PUT    /projects/:id(.:format)                                 projects#update
                         DELETE /projects/:id(.:format)                                 projects#destroy
     discussion_comments GET    /discussions/:discussion_id/comments(.:format)          comments#index
                         POST   /discussions/:discussion_id/comments(.:format)          comments#create
  new_discussion_comment GET    /discussions/:discussion_id/comments/new(.:format)      comments#new
 edit_discussion_comment GET    /discussions/:discussion_id/comments/:id/edit(.:format) comments#edit
      discussion_comment GET    /discussions/:discussion_id/comments/:id(.:format)      comments#show
                         PATCH  /discussions/:discussion_id/comments/:id(.:format)      comments#update
                         PUT    /discussions/:discussion_id/comments/:id(.:format)      comments#update
                         DELETE /discussions/:discussion_id/comments/:id(.:format)      comments#destroy
             discussions GET    /discussions(.:format)                                  discussions#index
                         POST   /discussions(.:format)                                  discussions#create
          new_discussion GET    /discussions/new(.:format)                              discussions#new
         edit_discussion GET    /discussions/:id/edit(.:format)                         discussions#edit
              discussion GET    /discussions/:id(.:format)                              discussions#show
                         PATCH  /discussions/:id(.:format)                              discussions#update
                         PUT    /discussions/:id(.:format)                              discussions#update
                         DELETE /discussions/:id(.:format)                              discussions#destroy
                    root GET    /                                                       homes#index

我不确定是否需要任何我的projects_controller.rb信息,但如果需要,我会更新。

1 个答案:

答案 0 :(得分:1)

edit_project_discussion_path需要ProjectDiscussion

/projects/:project_id/discussions/:id/edit(.:format)

您只是将讨论item作为project参数发送。

<%= link_to "Edit", edit_project_discussion_path(item) %>

您需要更改它以包含项目:

<%= link_to "Edit", edit_project_discussion_path(@project, item) %>