Rails控制器中未定义的局部变量

时间:2014-10-01 06:25:23

标签: ruby-on-rails

未定义的局部变量或方法`dashboard_user'为# 提取的来源(第3行):

class AdminController < ApplicationController
def user_creation
dashboard_user.create(:username => params[:username])
dashboard_user.create(:password => params[:password])
dashboard_user.create(:lastname => params[:lastname])
dashboard_user.create(:firstname => params[:firstname])

我如何解决这个问题,谢谢我是ruby的新手

修好后我得到了这个 未定义的方法`attr_accessible&#39;对

class DashboardUser < ActiveRecord::Base
self.table_name = "dashboard_user"
attr_accessible :username
attr_accessible :password
attr_accessible :lastname
attr_accessible :firstname

完全控制器

class AdminController < ApplicationController
  def user_creation
     DashboardUser.create(:username => params[:username])
     DashboardUser.create(:password => params[:password])
     DashboardUser.create(:lastname => params[:lastname])
     DashboardUser.create(:firstname => params[:firstname])
     DashboardUser.create(:middlename => params[:middlename])
     DashboardUser.create(:phone => params[:phone])
    redirect_to :action => 'user_creation'
  end
  private
  def dashboard_params
  params.require(:dashboard_user).permit(:id, :username, :password, :lastname, :firstname, :middlename , :phone)
  end
end

create_table "dashboard_user", primary_key: "USER_ID", force: true do |t|
    t.string   "USER_NAME",            limit: 50,  null: false
    t.string   "NORMALIZED_USER_NAME", limit: 50
    t.string   "PASSWORD",             limit: 50
    t.string   "LAST_NAME",            limit: 50
    t.string   "FIRST_NAME",           limit: 50
    t.string   "MIDDLE_NAME",          limit: 50
    t.string   "PHONE",                limit: 15
    t.string   "EMAIL_ID",             limit: 100
    t.integer  "SEQ_QUES_ID"
    t.string   "SEQ_QUES_ANSWER",      limit: 100
    t.string   "EXPIRE_PASSWORD_IND",  limit: 1
    t.date     "EXPIRE_PASSWORD_DATE"
    t.string   "DEACTIVATED_IND",      limit: 1
    t.date     "DEACTIVATED_DATE"
    t.integer  "ROLE_ID"
    t.string   "CREATED_BY",           limit: 50
    t.datetime "CREATED_DATE"
    t.string   "UPDATED_BY",           limit: 50
    t.datetime "UPDATED_DATE"
  end

模式

2 个答案:

答案 0 :(得分:0)

关于Rails中的重要规则是:约定优于配置

按照惯例,您的模型文件名为 dashboard_user ,但是当您打开该文件时,您的模型类名称应为 DashboardUser ,因此在更改代码后应解决问题:< / p>

class AdminController < ApplicationController

    def user_creation
       DashboardUser.create(:username => params[:username])
       DashboardUser.create(:password => params[:password])
       DashboardUser.create(:lastname => params[:lastname])
       DashboardUser.create(:firstname => params[:firstname])

    end

end

答案 1 :(得分:0)

在您的控制器中,您应该使用新的allowed_pa​​rams(如使用Rails 4)

它应该在控制器的底部看起来像

private

def dashboard_params
  params.require(:dashboard_user).permit(:id, :user_name, :first_name, :last_name, :password)
end