使用另一个模型视图中的form_for重复发布

时间:2012-01-23 00:24:51

标签: ruby-on-rails ruby

首先,我有一些问题,从另一个模型中使用form_for查看每个似乎都说它有效的方式。即。“来自'character / index.html.erb'form_for(@character,@statistic)do ....” 是的,这种方法看起来很简单,我很乐意使用它,但它不适合我,所以显然我做“IT”错了。以下是我目前的设置和问题。首先使用form_for是开始工作的噩梦,但更重要的是,每当我点击表单上的提交时,它都会以某种方式重复发布帖子。

events_controller.rb

...
  def show
    @event = Event.find(params[:id])
    @message = @event.messages.build
    @locations = Location.all
    @city = Location.find_by_city(params[:city])
    @messages = @event.messages.limit(5).order('created_at DESC')
  end 
...

Messages_controller.rb

...
  def create
    @message = Message.create(params[:message])
    @message.event_id = params[:event_id]
    @message.save
    respond_to do |format|
      format.js {@message}
    end
  end
...

事件/ show.html.erb

...
      <%= form_for([@event, @message], :remote => true) do |f|%>
    <%= f.text_field :msg %>
    <%= f.hidden_field :name, :value => current_requester.name %>
    <%= f.submit "send"%>
      <% end %>
...

如果您想知道,js响应是并且正常工作以更新我指向的div。

Development.log

  

在1月22日星期日18:19:57为127.0.0.1开始POST“/ events / 4 / messages”   -0600 2012由MessagesController处理#create as JS参数:{“commit”=&gt;“send”,“event_id”=&gt;“4”,   “authenticity_token”=&gt; “中OjW6XyzwCbCgXfu74Qy1VYjJm5 + YGIjw9f + yH的+ 2pi04 =”,   “utf8”=&gt;“✓”,“message”=&gt; {“name”=&gt;“Chris”,“msg”=&gt;“fd”}} SQL   (0.1ms)BEGIN SQL(0.5ms)描述messages AREL(0.2ms)   INSERT INTO messagescreated_atnameupdated_at,   event_idmsg)VALUES('2012-01-23 00:19:57','Chris','2012-01-23   00:19:57',NULL,'fd')SQL(85.4ms)COMMIT SQL(0.1ms)BEGIN
  AREL(0.4ms)UPDATE messages SET updated_at ='2012-01-23   00:19:57',event_id = 4 WHERE messagesid = 25 SQL(89.6ms)   COMMIT呈现消息/ _message.html.erb(0.2ms)呈现   messages / create.js.erb(1.4ms)在198ms完成200 OK(浏览次数:8.6ms   | ActiveRecord:176.5ms)

     

在1月22日星期日18:19:57为127.0.0.1开始POST“/ events / 4 / messages”   -0600 2012由MessagesController处理#create as JS参数:{“event_id”=&gt;“4”,   “authenticity_token”=&gt; “中OjW6XyzwCbCgXfu74Qy1VYjJm5 + YGIjw9f + yH的+ 2pi04 =”,   “utf8”=&gt;“✓”,“message”=&gt; {“name”=&gt;“Chris”,“msg”=&gt;“fd”}} SQL   (0.1ms)BEGIN SQL(0.3ms)描述messages AREL(0.2ms)   INSERT INTO messagescreated_atupdated_atevent_idmsg,   name)VALUES('2012-01-23 00:19:57','2012-01-23 00:19:57',NULL,   'fd','Chris')SQL(89.6ms)COMMIT SQL(0.1ms)BEGIN AREL   (0.3ms)UPDATE messages SET updated_at ='2012-01-23 00:19:57',   event_id = 4 WHERE messagesid = 26 SQL(64.0ms)COMMIT   呈现的消息/ _message.html.erb(0.2ms)已呈现   messages / create.js.erb(1.2ms)在174ms内完成200 OK(浏览次数:7.1ms   | ActiveRecord:154.6ms)   n0de @ IbeSuperMadtron:〜/工作区/ RadioMint /日志$

添加模型,以防有人问。

class Message < ActiveRecord::Base
  belongs_to :event
end

class Event < ActiveRecord::Base
  include Geocode

  belongs_to :disk_jockey
  belongs_to :venue

  has_many :song_requests
  has_many :fresh_song_requests, :conditions => "played = false", :class_name => "SongRequest"
  has_many :requested_songs, :through => :song_requests, :source => 'song', :order => 'songs.created_at ASC'
  has_one :poll
  has_many :messages
  has_many :event_playlists
  has_many :playlists, :through => :event_playlists

  # I havent figured out the issue as to why messages dont show up when a validation is not met
  # I have used the ':message => "" ' tailed to the end of these validations but they never show up.
  validates_presence_of :name, :disk_jockey_id
  validates :code, :uniqueness => true, :presence => true
...
end

1 个答案:

答案 0 :(得分:0)

我不明白为什么你需要使用form_for将其设为@event。我假设你只是想让它成为为特定事件创建消息的表单?做到这一点:

<%= form_for @message, :remote => true do |f| %>

您已通过分配声明@message@event相关联,因此除非您有@event属性的输入字段,否则您不需要包含{{1}在@event


但如果您确实需要输入事件数据,而不是使用form_for中的两个模型,也许您可​​以尝试以下替代方法。在您的form_for模型文件中,添加:

events.rb

然后在你看来,做这样的事情:

accepts_nested_attributes_for :messages