添加<%= f.input :schedule, as: :datetime, input_html: {id: 'schedule'} %>
以添加日期時間選擇器後,出現奇怪的錯誤。 我在想,問題是什麼?表單創建方法的NoMethodError
NoMethodError in Messages#new
和 undefined method ``schedule' for #<Message:0x000000066fa1e8>
我的創建控制器是
def create
@lists = current_user.lists.all
@message = Message.new(params[:message])
lists = params[:message][:lists]
schedule = params[:message][:schedule]
if @message.save
if schedule == [""]
MessageWorker.perform_async(@message.id, lists, current_user.id)
else
Schedule.create(execution_time: schedule, lists: lists, message_id: @message_id, user_id: current_user.id)
end
else
render action: "new"
flash[:notice] = "Messages Not Sent"
end
end
和視圖是;
<%= simple_form_for(@message, :html => {:multipart => true}) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :from, placeholder: "Max 11 characters" , input_html: {id: 'sms_from'} %>
<%= f.input :to, :input_html => {:value => params[:to], :class => "to"}, placeholder: "Separate Numbers with Comma", hint: "Include atleast one number in this section. Separate the numbers with commas." %>
<div id="add_lists"> <span class="button"> Add List </span></div>
<div id="all_lists"> <%= f.collection_check_boxes :lists, @lists, :id, :name, as: :select %></div>
<%= f.input :message, :as => :text, :input_html => {:cols => 60, :rows => 7, id: "compose_message", placeholder: 'Type your message here!'} %>
<div>
<p>
<span id="char_count">160 characters left</span>
<span id="number_of_messages">1 message(s)</span>
</p>
</div>
<%= f.input :schedule, as: :datetime, input_html: {id: 'schedule'} %>
<%= f.input :user_id, :as => :hidden, :input_html => {:value => @current_user.id} %>
<%= f.input :status, :as => :hidden, :input_html => {:value => "Queued"} %>
<%= f.button :submit, "Send Message" %>
<% end %>
和模型頂部看起來像這樣;
class Message < ActiveRecord::Base
attr_accessible :message, :phone, :status, :to, :from, :user_id, :schedule
validates :message, :presence => true
validates :from, :presence => true
validates :to, :presence => true
validates :status, :presence => true
validates_length_of :message, :maximum => 1600, :allow_blank => true
validates_length_of :from, :maximum => 11, :allow_blank => false
belongs_to :user
一切都很好,直到我加入;
<%= f.input :schedule, as: :datetime, input_html: {id: 'schedule'} %>
實際上,消息模型中的':schedule'只是一個急速修復。我不打算存儲計劃字段,因爲我保存在計劃模型db – acacia
然後attribute_accessor:計劃在模型中嗎? –
@Victor即使沒有它,它不工作,我覺得奇怪。 – acacia