simple_form_for和nomethoderror的问题

时间:2013-11-17 03:28:31

标签: ruby-on-rails nomethoderror

我是rails的新手,我正在尝试创建一个简单形式的表单,并且在接受控制器,模型和视图时遇到问题。我想使用new_event方法,但起初我想让它工作。我实际上想要更好地格式化它。先感谢您。以下是跟踪错误:

NoMethodError in EventsController#new

undefined method `[]' for nil:NilClass
Rails.root: /home/saasbook/Documents/ronde

Application Trace | Framework Trace | Full Trace
  app/models/event.rb:36:in `validate'
  app/models/event.rb:25:in `new_event'
  app/controllers/events_controller.rb:20:in `new'

以下是模型:

class Event < ActiveRecord::Base

   # Associations
   has_many :invitations
   belongs_to :created_by, :class_name => 'User'
   has_many :attending_users, :through => :invitations, :source => :user, :conditions => "status = 'confirmed'"
   has_many :invited_users, :through => :invitations, :source => :invited_user, :conditions => "status = 'invited'"

   validates_presence_of :description, :location, :name, :start, :created_by
   attr_accessible :description, :end, :location, :name, :public, :start, :created_by, :event_type, :user_id
   EVENT_OPTIONS = %w[food drink other]
   after_initialize :init

   def init
     self.public ||= false
   end

   def new
     self.create(:description, :end, :location, :name, :public, :start, :created_by, :event_type, :user_id)
   end

   def self.new_event(details, user)
      @event = Event.new
      @event.created_by = user
      @flag = validate(details)
      if @flag.empty?
         @event.attributes = details
         @event.save!
      end
      return @event, @flag
   end
   def self.validate(details)
flag = {}
flag['name'] = true if details[:name] == nil or details[:name] == ""
flag['description'] = true if details[:description] == nil or details[:description] == ""
flag['location'] = true if details[:location] == nil or details[:location] == ""
flag['event_type'] = true if details[:event_type] == nil or details[:event_type] == ""
return flag

端 这是我的控制器和视图:

 def new

 end

def create
    @event = Event.new 
    @event.created_by = current_user
    @event.attributes = params[:event]
    @event.save!
    redirect_to user_dashboard_path
end

%body   
 .container
    %h2.title Create New Event 
    = simple_form_for(Event.new, :html => {:class => 'form-horizontal' }) do |form|
        = f.hidden :created_by => current_user
        = f.text_field :name,:placeholder => "What should we do?"
        = f.text_area :description, rows: 3, :placeholder => "Give your friends some more details and convince them to come "
        = f.text_field :location,:placeholder => "Where?"
        = f.collection_select :event_type, Event::EVENT_OPTIONS, :to_s, :humanize, prompt: true
        = f.datetime_select :start, :default => Time.now, :discard_month => true, :discard_year => true, :ampm => true, :minute_step =>15
        = f.datetime_select :end, :default => Time.now, :discard_month => true, :discard_year => true, :ampm => true, :minute_step =>15
        = f.submit value: 'create', class: 'btn btn-large btn-success'

1 个答案:

答案 0 :(得分:0)

为什么要在模型中定义“新”方法?这是那些神圣的铁轨之一,所以你真的不应该重新定义它,特别是因为看起来你实际上并没有做任何不同的事情。此外,你正在使'new'成为一个实例方法而不是类方法,这意味着你不能调用Event.new,而是调用Event.find(1).new,这将无法正常工作,因为那时事件会有已经创建了。有关详细信息:http://www.railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/

所以基本上,摆脱模型中的“新”实例方法。然后我会更改以下代码:

def create
  @event = Event.new params[:event] # this grabs the params (and values) from the form
  ## @event.created_by = current_user - don't need this because you have a hidden field for it
  ## @event.attributes = params[:event] - unnecessary, you need to be defining your strong paramters below anyway if you're using rails 4
  if @event.save!   
    redirect_to user_dashboard_path
  else
    #fail miserably
end