2016-12-01 6 views
0

コントローラーで正しくパラメーターを定義しました。それはまたNo route matches {:action=>"edit", :controller=>"stories", :id=>nil} missing required keys: [:id]'と言います。StoriesControllerのActiveRecord :: RecordNotFound#edit

私のコントローラでは、以下の編集方法で 'id'を見つけるために定義しませんでしたか?/

class StoriesController < ApplicationController 
    before_action only: [:destroy, :show, :edit, :update] 


def index 
    @stories = Story.order('created_at DESC') 
end 

def new 
    @story = current_user.stories.build 
end 

def create 
    @story = current_user.stories.build(story_params) 
    if @story.save 
     flash[:success] = "Your beautiful story has been added!" 
     redirect_to root_path 
    else 
     render 'new' 
    end 
end 

def edit 
    @story = Story.find(params[:id]) 
end 

def update 
    if @story.update.attributes(story_params) 
     flash[:success] = "More knowledge, more wisdom" 
     redirect_to root_path 
    else 
     render 'edit' 
    end 
end 

def destroy 
    if @story.destroy 
     flash[:success] = "I think you should have more confidence in your storytelling" 
    else 
     flash[:error] = "Can't delete this story, sorry" 
    end 
end 

def show 
    @stories = Story.all 
end 

private 

def story_params 
    params.require(:story).permit(:name, :description) 
end 



end 

Index.html.erb:なぜ私のdestroyメソッドはどちらか動作していない。また、私は理解していない

<p id="notice"><%= notice %></p> 


    <h1>This is a list of posts</h1> 

    <table> 
     <thead> 
      <tr> 
       <th>Name</th> 
       <th>Description</th> 
       <th>User</th> 
       <th colspan="3"></th> 
      </tr> 
     </thead> 

     <tbody> 
      <% @stories.each do |story| %> 
      <tr> 
      <td><%= story.name %></td> 
      <td><%= story.description %></td> 
      <td><%= story.user.email %></td> 
      <td><%= link_to 'Show', story %></td> 
      <% if user_signed_in? %> 
      <td><%= link_to 'Edit', edit_story_path(@story) %></td> 
      <td><%= link_to 'Destroy', story, method: :delete, data: { confirm: 'Are you sure?'} %></td> 
      <% end %> 
      </tr> 
      <% end %> 

     </tbody> 
    </table> 

    <%= link_to 'New Story', new_story_path %> 

routes.rbを:

Rails.application.routes.draw do 

resources :stories 
devise_for :users 
root to: 'stories#index' 
end 

レーキルート:

Prefix Verb URI Pattern     Controller#Action 
       stories GET /stories(.:format)    stories#index 
         POST /stories(.:format)    stories#create 
       new_story GET /stories/new(.:format)   stories#new 
       edit_story GET /stories/:id/edit(.:format) stories#edit 
        story GET /stories/:id(.:format)   stories#show 
         PATCH /stories/:id(.:format)   stories#update 
         PUT /stories/:id(.:format)   stories#update 
         DELETE /stories/:id(.:format)   stories#destroy 

答えて

2

あなたの意見では、

<td><%= link_to 'Edit', edit_story_path(@story) %></td>

あなたが撮影しているので、あなたのloopあなたは、ループ内のストーリーの変数を持つべきである、

ある、ので、

<td><%= link_to 'Edit', edit_story_path(story) %></td>

であるべき|物語| |ストーリー|

ですので、フォームになります。今

<% @stories.each do |story| %> 
     <tr> 
      <td><%= story.name %></td> 
      <td><%= story.description %></td> 
      <td><%= story.user.email %></td> 
      <td><%= link_to 'Show', story %></td> 
      <% if user_signed_in? %> 
        <td><%= link_to 'Edit', edit_story_path(@story) %></td> 
        <td><%= link_to 'Destroy', story_path(story),method: :delete,data: { confirm: 'Are you sure?' } %></td> 
      <% end %> 
     </tr> 
<% end %> 

、削除リンクは

<td><%= link_to 'Destroy', story_path(story),method: :delete,data: { confirm: 'Are you sure?' } %></td> 
+0

ありがとう、このようにする必要があります。なぜそれはストーリーではなく、@ストーリーではないのですか? – Benjamints

+0

あなたのforループでは、 'story'ローカル変数をループしていますが、' @ story'ではありません – Sravan

+0

答えを確認してください、それにいくつかの説明があります – Sravan

関連する問題