Ruby on rails rails中未定义的方法错误?

Ruby on rails rails中未定义的方法错误?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个模型,学生可以输入自己的信息,其他人可以查看。因为有很多个人资料,我也添加了一个搜索部分。如果他们只想要CS专业,他们可以从下拉列表中选择。我有专业,大学。。。今天我添加了“level”,即教育水平,我得到一个错误(错误显示在末尾)。我知道这很长,但任何帮助都会非常有用。谢谢 (Userinfo是学生模型) 我的控制器如下所示: class UserinfosController < ApplicationController before_action :find_use

我有一个模型,学生可以输入自己的信息,其他人可以查看。因为有很多个人资料,我也添加了一个搜索部分。如果他们只想要CS专业,他们可以从下拉列表中选择。我有专业,大学。。。今天我添加了“level”,即教育水平,我得到一个错误(错误显示在末尾)。我知道这很长,但任何帮助都会非常有用。谢谢

(Userinfo是学生模型)

我的控制器如下所示:

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

正如您所评论的,您的
搜索
模型中没有
级别
属性

rails g migration add_level_to_searches level:string
当然

rails db:migrate

很乐意帮忙

你能为
搜索
模型设置模式吗?@Jeremie done。我想我看到了问题所在。我必须做“rails g迁移添加_level_到_search level:string”。是吗?是的,您的模型中还没有level属性,请创建迁移,然后运行
rails db:migrate
。谢谢各位,如果有人需要积分,请写下它作为答案。
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 %>
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  %>
  create_table "searches", force: :cascade do |t|
    t.string   "keyword"
    t.string   "college"
    t.decimal  "min_gpa"
    t.string   "major"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
rails g migration add_level_to_searches level:string
rails db:migrate