Controller#edit中的ActiveRecord :: RecordNotFound

时间:2016-03-20 22:35:55

标签: ruby-on-rails ruby ruby-on-rails-4 activerecord

这可能是一个基本错误,很多人都会提出类似的问题。 但是,他们都没有帮我解决我现在面临的问题。

错误如下;

Processing by SchedulesController#edit as HTML
  Parameters: {"id"=>"123"}
  [1m[35mUser Load (0.2ms)[0m  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 123]]
Completed 404 Not Found in 2ms (ActiveRecord: 0.2ms)

ActiveRecord::RecordNotFound (Couldn't find User with 'id'=123):
  app/controllers/application_controller.rb:17:in `correct_user'

我想要做的是编辑计划内容。 当我输入http://localhost:3000/schedules/123/edit等网址时,会出现上述错误。

application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  include SessionsHelper

  private

    def logged_in_user
      unless logged_in?
        flash[:danger] = "Please log in."
        redirect_to login_url
      end
    end

    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end

end

schedules_controller.rb

class SchedulesController < ApplicationController
  before_action :logged_in_user, only: [:create, :destroy]
  before_action :correct_user,   only: [:edit, :update]
  before_action :set_schedule,  only: %i(show edit update destroy)  

  def new
    @schedule = Schedule.new
    room = @schedule.rooms.build
    room.events.build
  end

  def create
    @schedule = current_user.schedules.build(schedule_params)
    if @schedule.save
      flash[:success] = "Schedule created!"
      redirect_to root_url
    else
      render 'new'
    end
  end

  def edit
  end

  def update
    if @schedule.update(schedule_params)
      flash[:success] = "Schedule updated!"
      redirect_to root_url
    else
      render 'edit'
    end
  end

...  

  private

    def schedule_params
      params.require(:schedule).permit(:title, :departure_date,  room_attributes: [:id, :_destroy, :room, :day])
    end

    def set_schedule
      @schedule = Schedule.find(params[:id])
    end

end

的routes.rb

Rails.application.routes.draw do


  get 'password_resets/new'

  get 'password_resets/edit'

  root  'schedules#index'
  get   'signup'  => 'users#new'
  get   'login'   => 'sessions#new'
  post  'login'   => 'sessions#create'
  delete   'logout'  => 'sessions#destroy'

  resources :users
  resources :account_activations, only: [:edit]
  resources :password_resets,     only: [:new, :create, :edit, :update]
  resources :schedules
...

end

edit.html.erb

<div class="row">
  <div class="col-md-12">
    <p>Edit schedule</p>
    <%= render "schedule_form" %>
  </div>
</div>

_schedule_form.html.erb

<%= simple_nested_form_for(@schedule) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <%= f.label :title %>
  <%= f.text_field :title, class: 'form-control' %>
  <br>
  <div class="input-group date" id="datetimepicker">
    <%= f.label :departure_date %>
    <%= f.text_field :departure_date, class: 'form-control' %>
    <span class="input-group-addon">
      <span class="fa fa-calendar glyphicon-calendar glyphicon"></span>
    </span>
  </div>
  <br>
  <%= f.simple_fields_for :rooms do |a| %>
    <%= a.input :room %>
  <% end %>

  <br>
  <%= f.submit "Create my schedule", class: "btn btn-primary" %>
  <br>

<% end %>

sessions_helper.rb

module SessionsHelper

...

  def current_user
    @current_user ||= User.find_by(id: session[:user_id])
  end

  def current_user?(user)
    user == current_user
  end

end

解决!

我在correct_userusers_controller.rb而不是schedules_controller.rb创建了application_controller.rb

users_controller.rb

    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end

schedules_controller.rb

    def correct_user
      @schedule = current_user.schedules.find_by(id: params[:id])
      redirect_to(root_url) if @schedule.nil?
    end

2 个答案:

答案 0 :(得分:1)

您正尝试使用相同的参数[:id]作为计划

@schedule = Schedule.find(params[:id])

和用户

@user = User.find(params[:id])
在before_action中的

:correct_user

我想您的数据库中没有此类用户记录。您应该为User和Schedule模型发送不同的参数

答案 1 :(得分:0)

错误是因为您可以为模态用户添加范围为none 请参阅您的活动记录和User.rb类,并确保范围不为空。

  

http://edgeguides.rubyonrails.org/active_record_querying.html

看到null关系,你可能会有所了解