【问题标题】:Undefined method error in rails?rails中未定义的方法错误?
【发布时间】:2017-07-18 00:46:55
【问题描述】:

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

(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

我的信息填写表格(请注意输入的方式在专业和教育级别上有什么不同,也许这就是问题?):

<%= 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 %>

我尝试访问索引视图时遇到的错误:

NoMethodError in Userinfos#index
Showing app-path/index.html.erb where line #25 raised:

undefined method `level' for #<Search:0x000000139d6c38>

第 25 行是:

<%= 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

【问题讨论】:

  • 您能把Search 模型的架构放到一边吗?
  • @Jeremie 完成。我想我看到了问题。我必须做“rails g migration add_level_to_searches level:string”。对吗?
  • 是的,您的模型中还没有 level 属性,创建迁移然后运行rails db:migrate
  • 谢谢大家,如果有人需要积分,请写下来作为答案。

标签: ruby-on-rails ruby


【解决方案1】:

正如您所评论的,您的 level 模型中没有 Search 属性。

rails g migration add_level_to_searches level:string

当然

rails db:migrate

乐于助人!

【讨论】:

    猜你喜欢
    • 2013-12-27
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多