34035
記事についたコメントを編集出来るようにしたいのです。  
コメントをformに持っていき編集して更新ボタンをクリックすると  
No route matches [PATCH] "/articles/1/comments"  
のようなエラーが出てきてrotesなどを色々変えて見たのですが全然、治らないので見ていただけないでしょうか？  
_form.html.erb

```

    <%= form_for(comment, url: article_comments_path) do |f| %>
    <% if comment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(comment.errors.count, "error") %></h2>
      <ul>
      <% comment.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
    <% end %>
    <div class="field">
    <%= f.label :content %>
    <%= f.text_area :content %>
    </div>
    <div class="field">
    <%= f.label :article_id %>
    <%= f.number_field :article_id %>
    </div>
    <div class="actions">
    <%= f.submit %>
    </div>
    <% end %>
    
```

routes.rb

```

    rails.application.routes.draw do
    
    get '/users' => 'users#index'
    get '/users/:id' => 'users#show'
    devise_for :users
    resources :articles do
      resources :comments
    end
    
    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
    end
    
```

comments_controller.rb

```

    class CommentsController < ApplicationController
      before_action :set_comment, only: [:show, :edit, :update, :destroy]
    
      # GET /comments
      # GET /comments.json
      def index
        @comments = Comment.all
      end
    
      # GET /comments/1
      # GET /comments/1.json
      def show
      end
    
      # GET /comments/new
      def new
        @comment = Comment.new
      end
    
      # GET /comments/1/edit
      def edit
      end
    
      # POST /comments
      # POST /comments.json
      def create
        @article = Article.find(params[:article_id])
        @comment = Comment.new(comment_params)
        redirect_to article_path(@article)
    
        respond_to do |format|
          if @comment.save
            format.html { redirect_to @article, notice: 'Comment was successfully created.'}
            format.json { render :show, status: :created, location: @article } and return
    
          else
            format.html { render :new }
            format.json { render json: @comment.errors, status: :unprocessable_entity }
          end
        end
      end
    
      # PATCH/PUT /comments/1
      # PATCH/PUT /comments/1.json
      def update
        respond_to do |format|
          if @comment.update(comment_params)
            format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
            format.json { render :show, status: :ok, location: @comment }
          else
            format.html { render :edit }
            format.json { render json: @comment.errors, status: :unprocessable_entity }
          end
        end
      end
    
      # DELETE /comments/1
      # DELETE /comments/1.json
      def destroy
        @comment.destroy
        respond_to do |format|
          format.html { redirect_to comments_url, notice: 'Comment was successfully destroyed.' }
          format.json { head :no_content }
        end
      end
    
      private
        # Use callbacks to share common setup or constraints between actions.
        def set_comment
          @comment = Comment.find(params[:id])
        end
    
        # Never trust parameters from the scary internet, only allow the white list through.
        def comment_params
          params.require(:comment).permit(:content, :article_id)
        end
    end
    
```
CC BY-SA 3.0
2017-04-15T04:37:28.513
34031
2017-04-15T08:52:21.360
2017-04-15T06:45:02.290
5288
22565
question
ruby-on-rails ruby
エラーが取れないです。No route matches [PATCH] "/articles/1/comments"
まず、`new` で、`@comment` が article と関連付けられていません。  
以下のようにすると関連付けられます。

```

    def new
      @article = Article.find(params[:article_id])
      @comment = @article.comments.build
    end
    
```

同様に `edit` でも `@article` を設定しておきます。

```

    def edit
      @article = @comment.article
    end
    
```

で、`No route matches [PATCH] "/articles/1/comments"` については、

```

    <%= form_for([article, comment]) do |f| %>
    
```

で治ります。  
ただし、article も渡してやる必要があります。これは、`new.html.erb` や `edit.html.erb` で

```

    <%= render 'form', comment: @comment %>
    
```

といった感じになっていると思いますので、以下のように追加します。

```

    <%= render 'form', comment: @comment, article: @article %>
    
```

最後に、`update` メソッドの

```

        format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
    
```

でエラーが出ますので、

```

        format.html { redirect_to article_comment_path(@comment), notice: 'Comment was successfully updated.' }
    
```

と修正すると解決します。
CC BY-SA 3.0
2017-04-15T08:52:21.360
34035
2017-04-15T08:52:21.360
5288
34031
answer