保持接收"未知属性= user_id"错误

时间:2014-04-08 18:58:23

标签: ruby-on-rails

第一次发布海报,很久以前潜伏在这里。我有一个用户模型和控制器,用于我正在制作的Rails的小型视频游戏应用程序。所以我在这里就这个问题阅读了几个答案,但没有一个答案真的对我有帮助。人们建议在我的Users表中添加一个“user_id”列,但我的观点是,我认为“user_id”是在Rails中自动生成的?即使我使用user.inspect,我仍然会在页面上看到user_id=7。但是,在尝试创建游戏并分配给当前用户时,我仍然会收到未知属性错误。在查明原因和解决方案时,将非常感谢任何帮助。谢谢!

应用程序/控制器/ users_controller.rb:       

  class UsersController < ApplicationController
  skip_before_filter :require_authentication, only: [:new, :create]

  def index
    @users = User.all
  end

  def show
  end

  def new
    @user = User.new
  end

  def edit
    @user = current_user
  end

  def create
    @user = User.create!(user_params)
    session[:user_id] = @user.id
    redirect_to users_path, notice: "Hi #{@user.username}! Welcome to DuckGoose!"
  end

  def update
    current_user.update_attributes!(user_params)
    redirect_to users_path, notice: "Successfully updated profile."
  end

  def destroy
    @user = User.find(params[:id])
    @user.destroy
    redirect_to users_url, notice: 'User was successfully destroyed.'
  end

  private
    def user_params
      params.require(:user).permit(:username, :firstname, :lastname, :email, :password, :password_confirmation)
    end
end

应用程序/配置/ routes.rb中:         

    NkuProject::Application.routes.draw do
      resources :users do
      resources :games
     end
     resources :sessions
     resources :games

     get "sign_out", to: "sessions#destroy"
     get "profile", to: "users#edit"

     root to: "sessions#new"
    end

应用程序/控制器/ games_controller.rb

class GamesController < ApplicationController
  def new
    @game = Game.new
  end

  def index
    @games = Game.all
  end

  def destroy
    @game = Game.find(params[:id])
    @game.destroy
    redirect_to games_url, notice: 'Game was successfully deleted.'
  end

  def create
    @game = current_user.games.build(game_params)
    if @game.save
      redirect_to @game, notice: "Game successfully added"
    else
      render :new
    end
  end

  def show
    @game = Game.find(params[:id])
  end

  private
  def game_params
    params.require(:game).permit!
  end
end

应用程序/控制器/ application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_filter :require_authentication

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


  def require_authentication
    if current_user
      true
    else
      redirect_to new_session_path
    end
  end
end

我确定我错过了一些代码供参考,但如果我需要其他任何内容请告诉我。

1 个答案:

答案 0 :(得分:2)

查看控制器操作的定义方式,我可以肯定地说UserGame有一个1-M relationship,即

class User < ActiveRecord::Base
  has_many :games
end

class Game < ActiveRecord::Base
  belongs_to :user
end

现在,基于games表必须有一个名为user_id的字段。除非您指定Rails,否则Rails不会为您创建它。您需要在user_id表中添加字段games,方法是为其创建迁移。现在,在user_id表中看起来好像没有games foreign_key字段。因此,保存games记录时出错。