我有一个模型,学生可以输入自己的信息,其他人可以查看。由于有很多个人资料,我也添加了一个搜索部分。如果他们只想要CS专业,他们可以从下拉列表中选择。我有像大学,大学这样的东西...今天我添加了#34;等级",这是教育水平,我得到一个错误(错误显示在最后)。我知道这很长,但任何帮助都非常有用。谢谢!
( Userinfo是学生模型)
我的控制器看起来像这样:
Function returnBrands() As Object
brands.Fill(db.brands) 'my brandsTableAdapter
Dim q = From pc In db.brands
Select pc.NAME_BRAND, pc.ID_BRAND
Order By NAME_BRAND
Return q.ToArray()
End Function
ComboBox1.DataSource = returnBrands()
ComboBox1.DisplayMember = "NAME_BRAND"
ComboBox1.ValueMember = "ID_BRAND"
我的信息填写表格(请注意edu的主要和级别的输入方式有何不同,也许这就是问题?):
class UserinfosController < ApplicationController
before_action :find_userinfo, only: [:show, :edit, :update, :destroy, :log_impression]
def index
@userinfors = Userinfo.search(params[:search])
@search = Search.new
@college = Userinfo.uniq.pluck(:college)
@major = Userinfo.uniq.pluck(:major)
@level = Userinfo.uniq.pluck(:level)
end
def show
end
def new
@userinformation = current_user.build_userinfo
end
def create
@userinformation = current_user.build_userinfo(userinfo_params)
if @userinformation.save
redirect_to userinfo_path(@userinformation)
else
render 'new'
end
end
def edit
end
def update
if @userinformation.update(userinfo_params)
redirect_to userinfo_path(@userinformation)
else
render 'edit'
end
end
def destroy
@userinformation.destroy
redirect_to root_path
end
private
def userinfo_params
params.require(:userinfo).permit(:name, :email, :college, :gpa, :major, :token, :skills, :level)
end
def find_userinfo
@userinformation = Userinfo.friendly.find(params[:id])
end
end
搜索控制器:
<%= simple_form_for @userinformation, :html => { :multipart => true } do |f| %>
<%= f.input :name, required: true, label: 'Full name' %>
<%= f.input :college, required: true, label: 'Name of college' %>
<%= f.input :gpa, required: true, label: 'Enter GPA' %>
<%= f.input :email, required: true, label: 'Enter email address' %>
<%= f.input :major, required: true, label: 'Major' %>
<%= f.input :level, collection: ["College undergrad", "College grad"], required: true, label: 'Level of education' %>
<%= f.input :skills, required: true, label: 'Skills' %>
<%= f.button :submit, 'Create account' %>
<% end %>
搜索模型:
class SearchesController < ApplicationController
def new
@search = Search.new
@college = Userinfo.uniq.pluck(:college)
@major = Userinfo.uniq.pluck(:major)
@level = Userinfo.uniq.pluck(:level)
end
def create
@search = Search.create(search_params)
redirect_to @search
end
def show
@search = Search.find(params[:id])
end
private
def search_params
params.require(:search).permit(:keyword, :college, :min_gpa, :major, :token, :level )
end
end
这是显示搜索部分的位置(索引视图):
class Search < ActiveRecord::Base
def search_userinfos
userinfos = Userinfo.all
userinfos = userinfos.where(["name LIKE ?","%#{keyword}%"]) if keyword.present?
userinfos = userinfos.where(["college LIKE ?", college]) if college.present?
userinfos = userinfos.where(["cast(gpa as numeric) >= ?", min_gpa]) if min_gpa.present?
userinfos = userinfos.where(["major LIKE ?", major]) if major.present?
userinfos = userinfos.where(["level LIKE ?", level]) if level.present?
userinfos = userinfos.order("gpa")
return userinfos
end
end
我尝试访问索引视图时遇到的错误:
<%= simple_form_for @search do |s| %>
<%= s.input :keyword, label: 'Name' %>
<label>College</label>
<%= s.select :college, options_for_select(@college), :include_blank => true %>
<%= s.input :min_gpa, label: "Minimum GPA" %>
<label>Major</label>
<%= s.select :major, options_for_select(@major), :include_blank => true %>
<label>Job type</label>
<%= s.select :level, options_for_select(@level), :include_blank => true %>
<%= s.button :submit, "Search" %>
<% end %>
第25行是:
NoMethodError in Userinfos#index
Showing app-path/index.html.erb where line #25 raised:
undefined method `level' for #<Search:0x000000139d6c38>
如果删除该行,一切都有效。当我在用户个人资料页面中显示教育级别时,它可以很好地工作。
搜索架构:
<%= s.select :level, options_for_select(@level), :include_blank => true %>
答案 0 :(得分:1)
正如您所评论的,您的level
模型中没有Search
属性。
rails g migration add_level_to_searches level:string
当然
rails db:migrate
乐意帮忙!