RoR的活动记录未定义的方法 [英] RoR Active Record undefined method

查看:141
本文介绍了RoR的活动记录未定义的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是错误即时得到:
未定义的方法`attr_accessible'为#

That is the error im getting:
undefined method `attr_accessible' for #

下面的教程后,我的code是:

after following a tutorial, my code is:

User.rb:

 class User < ActiveRecord::Base

   attr_accessible :email, :password, :password_confirmation

   attr_accessor :password
   before_save :encrypt_password

   validates_confirmation_of :password
   validates_presence_of :password, :on => :create
   validates_presence_of :email
   validates_uniqueness_of :email

users_conroller.rb:

users_conroller.rb:

   class UsersController < ApplicationController
    def new
    @user = User.new
 end

 def create
   @user = User.new(params[:user])
   if @user.save
     redirect_to root_url, :notice => "Signed up!"
   else
     render "new"
   end

 end
 end

请帮助! :(我读的地方,以改变从咖啡的咖啡JS文件只是JS之一,并把一个脚本,让这个工作,但它并没有为我工作。

Help please! :( I read somewhere to change one of the js coffee files from coffee to just js and put a script in to allow this to work but it didn’t work for me

推荐答案

此方法无法在轨道确定4.从模型中取出,并更新控制器,所以它倒像是:

TL;DR:

This method is not defined in rails 4. Remove it from your models and update your controller so it reads like:

class UsersController < ApplicationController
  def new
    @user = User.new
   end

  def create
    @user = User.new(user_params)
    if @user.save
      redirect_to root_url, :notice => "Signed up!"
    else
      render "new"
    end
  end

  private

  def user_params
    params.require(:user).allow(:email, :password, :password_confirmation)
  end
end

说明:

attr_accessible 在轨道3用于限制访问某些PARAMS的方法。其背后的原因是,大多数控制器创建或更新操作是这样的:

Explanation:

attr_accessible is a method used in rails 3 to limit an access to certain params. Reason behind it was, that most controller 'create' or 'update' actions looks like:

@model.assign_attributes(params[:model])

由于PARAMS已发送的用户,它可能包含任何东西,包括我们明确不希望他们能够更改(如字段管理:真正的 )。

Rails 3中解决了说,这标志着与 attr_accessible 只有属性可以被大规模分配,也就是说,如果用户试图通过一个额外的参数:管理员将通过 assign_attributes 的方法(这是在内部使用创建等)。

Rails 3 solved that by saying, that only the attributes marked with attr_accessible can be mass assigned, i.e. if a user tries to pass an extra param :admin it will be rejected by assign_attributes method (which is used internally in new, create etc.).

现在,这是不是最好的主意 - 有些时候,我们希望他们能够设置一些PARAMS根据上下文某些情况下,因此,例如,如果用户创建记录,其实是一个管理员,他可以提名其他用户要管理。因此,该安全逻辑已被移动从模型到控制器,被称为强属性。你可以看到它使用的解决方案,在顶部( user_params 法)。这种方式是更安全的,更难被滥用。

Now, this was not the best idea - there are certain situations when we want them to be able to set some params depending on the context, so for example if user creating the record is in fact an admin he can nominate another user to be admin. Hence, this security logic has been moved from the model to the controller and is known as strong attributes. You can see it uses in the solution at the top (user_params method). This way is much more secure and harder to be misused.

这篇关于RoR的活动记录未定义的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆