我的目标是创建一个搜索表单,以查看用户列表并在下一页显示。如果未填写f_name和l_name字段,则用户将在下一页上收到flash消息。我使用attr_accessor在模型中设置要在控制器中访问的虚拟属性,但我最终得到以下错误:
ArgumentError in AttendeesController#index
wrong number of arguments (0 for 2)
编辑1 - 或者,当我在rails控制台中键入Attendee.flash('f_name',l_name')时,我经常会收到错误。
NoMethodError: undefined method `flash_notice=' for #<Class:0x007ffe12b5dc28>
为什么说我的参数数量错误?
这是我的控制器:
1 class AttendeesController < ApplicationController
2 # GET /attendees
3 # GET /attendees.json
4 def index
5 @attendees = Attendee.search(params[:f_name], params[:l_name])
6
7 flash[:notice] = @attendees.flash(params[:f_name], params[:l_name])
8
9 respond_to do |format|
10 format.html # index.html.erb
11 format.json { render json: @attendees }
12 end
13 end
14
这是我的模特:
1 class Attendee < ActiveRecord::Base
2 attr_accessor :flash_notice
3
4 def self.search(f_name, l_name)
5 if f_name.presence && l_name.presence
6 where("f_name = ? AND l_name = ?", f_name, l_name)
7
8 else
9 self.flash
10 end
11 end
12
13 def self.flash(f_name, l_name)
14 self.flash_notice = "First name is present but last name is not present." if f_name.presence == true && l_name.presence == nil
15 self.flash_notice = "First name is not present but last name is present." if f_name.presence == nil && l_name.presence == true
16 self.flash_notice = "Both names aren't present!" if f_name.presence && l_name.presence == nil
17 end