我是铁道新手。我正在尝试生成页面表单。但我正在解决这个错误。
我无法理解我的问题在哪里。
这是显示的错误,并且在此行中突出显示
@companies_rates =@user.companies_rates
在这里,我将拥有userpage,companies_rate和page3,其中每个页面都有用户ID,公司费率也有一列同一列也在页面3中退出。现在我想单独显示companies_rate和page 3
NameError in UsersController#show
uninitialized constant User::CompaniesRate
@user = User.find(params[:id])
@companies_mines= @user.companies_mines
**@companies_rates =@user.companies_rates**
@title=@user.name
end
这是我的用户控制器
class UsersController < ApplicationController
before_action :signed_in_user,
only: [:index, :edit, :update, :destroy, :following, :followers]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
def index
@users = User.paginate(page: params[:page])
end
def show
@user = User.find(params[:id])
@companies_mines= @user.companies_mines
@companies_rates =@user.companies_rates
@title=@user.name
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
sign_in @user
flash[:success] = "Welcome to skillable"
redirect_to @user
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to users_url
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
# Before filters
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
def admin_user
redirect_to(root_url) unless current_user.admin?
end
end
这是我的companies_rate控制器
class CompaniesRatesController < ApplicationController
before_action :signed_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def index
@companies_rates = companies_rates.all
end
def show
@companies_rates = companies_rate.find(params[:id])
end
def new
@companies_rates = companies_rates.new
end
def create
@companies_rates = companies_rates .new(params[:company,:r1,:r2,:r3])
if @post.save
redirect_to companies_rates _path, :notice => "Your post was saved"
else
render "new"
end
end
def edit
end
def update
end
def destroy
end
private
def companies_rates_params
params.require(:companies_rates).permit(:company, :r1, :r2, :r3 )
end
def correct_user
@companies_rates = current_user.companies_rates.find_by(id: params[:id])
redirect_to root_url if @ompanies_rates.nil?
end
end
这是我的用户模态
class User < ActiveRecord::Base
attr_accessor :password
#attr_accessible :name, :email, :password, :password_confirmation
has_many :companies_mines
has_many :companies_rates
before_save { self.email = email.downcase }
before_create :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
def User.new_remember_token
SecureRandom.urlsafe_base64
end
def User.encrypt(token)
Digest::SHA1.hexdigest(token.to_s)
end
private
def create_remember_token
self.remember_token = User.encrypt(User.new_remember_token)
end
end
这是我的companies_rates
class CompaniesRates < ActiveRecord::Base
#attr_accessible :company, :r1, :r2, :r3
belongs_to :user
validates :company, presence: true, length: { maximum: 140 }
validates :user_id, presence: true
belongs_to :user
default_scope :order => 'companies_rates.created_at DESC'
end
可以告诉我什么是我的问题以及如何解决它。
答案 0 :(得分:0)
将型号名称更改为CompaniesRate.Model名称应为单数