【问题标题】:ActiveRecord::RecordNotFound in StoriesController#edit在 StoriesController#edit 中的 ActiveRecord::RecordNotFound
【发布时间】:2016-12-01 10:45:30
【问题描述】:

我已经在我的控制器中正确定义了参数。它还说No route matches {:action=>"edit", :controller=>"stories", :id=>nil} missing required keys: [:id]'

但是我没有定义它来在我的控制器下面的编辑方法中找到“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:

    <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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-5


    【解决方案1】:

    在你看来,

    &lt;td&gt;&lt;%= link_to 'Edit', edit_story_path(@story) %&gt;&lt;/td&gt;

    应该是,

    &lt;td&gt;&lt;%= link_to 'Edit', edit_story_path(story) %&gt;&lt;/td&gt;

    因为,你的loop 是,

    你应该在循环中有故事变量,因为你已经使用了 |story|在参数内部为 |story|

    所以,你会的。

    <% @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>
    

    【讨论】:

    • 谢谢。为什么是故事而不是@story?
    • 在你的for循环中,你循环的是story局部变量而不是@story
    • 现在检查答案,它有一些解释
    • 太棒了,谢谢!当我等了 5 分钟后,我会在计时器用完时将答案标记为正确!
    • 请立即查看最新答案,还添加了删除链接。
    猜你喜欢
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    相关资源
    最近更新 更多