First, I have some issue with using form_for from another models view the way every seems to say it works. ie." from 'character/index.html.erb' form_for(@character, @statistic) do ...." Yeah that method looks simple enough and I'd love to use it but it wont work for me so apparently im doing "IT" wrong. Below is my current setup and issue. Firstly using form_for is a nightmare to get to work and second but more importantly, whenever i click submit on my form it makes duplicate posts somehow.
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
...
events/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 %>
...
In case you are wondering, the js response is and works fine to update the div i pointed it at.
Development.log
Started POST "/events/4/messages" for 127.0.0.1 at Sun Jan 22 18:19:57 -0600 2012 Processing by MessagesController#create as JS Parameters: {"commit"=>"send", "event_id"=>"4", "authenticity_token"=>"OjW6XyzwCbCgXfu74Qy1VYjJm5+YGIjw9f+yH+2pi04=", "utf8"=>"✓", "message"=>{"name"=>"Chris", "msg"=>"fd"}} SQL (0.1ms) BEGIN SQL (0.5ms) describe
messages
AREL (0.2ms) INSERT INTOmessages
(created_at
,name
,updated_at
,event_id
,msg
) 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) UPDATEmessages
SETupdated_at
= '2012-01-23 00:19:57',event_id
= 4 WHEREmessages
.id
= 25 SQL (89.6ms) COMMIT Rendered messages/_message.html.erb (0.2ms) Rendered messages/create.js.erb (1.4ms) Completed 200 OK in 198ms (Views: 8.6ms | ActiveRecord: 176.5ms)Started POST "/events/4/messages" for 127.0.0.1 at Sun Jan 22 18:19:57 -0600 2012 Processing by MessagesController#create as JS Parameters: {"event_id"=>"4", "authenticity_token"=>"OjW6XyzwCbCgXfu74Qy1VYjJm5+YGIjw9f+yH+2pi04=", "utf8"=>"✓", "message"=>{"name"=>"Chris", "msg"=>"fd"}} SQL (0.1ms) BEGIN SQL (0.3ms) describe
messages
AREL (0.2ms) INSERT INTOmessages
(created_at
,updated_at
,event_id
,msg
,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) UPDATEmessages
SETupdated_at
= '2012-01-23 00:19:57',event_id
= 4 WHEREmessages
.id
= 26 SQL (64.0ms) COMMIT Rendered messages/_message.html.erb (0.2ms) Rendered messages/create.js.erb (1.2ms) Completed 200 OK in 174ms (Views: 7.1ms | ActiveRecord: 154.6ms) n0de@IbeSuperMadtron:~/workspace/RadioMint/log$
Adding in the models, in case someone asks.
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