I cannot get a form to submit the desired parameters. I have a form which needs to accept a hidden_field for the model notice. This is the form in the view:
<%= form_for(:notice, url: :notices, method: :post, html: { multipart: true, class: "comment_form" } ) do |f| %>
<%= hidden_field_tag :callsign, @character.callsign %>
<%= f.fields_for :active_comment_relationship do |ff| %>
<%= ff.hidden_field :commentee_id, value: notice.id %>
<% end %>
<%= f.hidden_field :latitude, value: notice.latitude, id: "comment_notice_latitude" %>
<%= f.hidden_field :longitude, value: notice.longitude, id: "comment_notice_longitude" %>
<%= f.text_area :content, rows: 1, id: "commentField-#{notice.id}", class: "comment_area" %>
<%= f.submit( "Post", class: 'btn btn-default btn-xs',
onclick: "return validateCommentForm('#commentField-#{notice.id}');" ) do %>
<span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
<% end %>
<%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>f
<% end %>
notices_controller.rb:
def notice_params
params.require(:notice).permit( :content, :picture, :latitude, :longitude, active_comment_relationship_attributes: [:commentee_id] )
end
notice.rb:
has_one :active_comment_relationship, class_name: "Commentrelationship",
foreign_key: "commenter_id",
dependent: :destroy
has_one :supernotice, through: :active_comment_relationship, source: :commentee
accepts_nested_attributes_for :active_comment_relationship
The logs:
Started POST "/notices" for ::1 at 2015-08-03 12:12:35 +0100
Processing by NoticesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ERlV0sDP7QWVimD1iErH7ICWMo7z5u7ASJBaFbotoL5HPZ5fo5eoZD38mKfSPDXXW1ew7ttBN/FPQoqQEfnbkQ==", "callsign"=>"bazzer", "notice"=>{"latitude"=>"51.80182150078305", "longitude"=>"-0.54107666015625", "content"=>"jhgf"}, "commit"=>"Drop"}
Character Load (0.6ms) SELECT "characters".* FROM "characters" WHERE "characters"."callsign" = $1 LIMIT 1 [["callsign", "bazzer"]]
User Load (2.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "characters".* FROM "characters" WHERE "characters"."callsign" = $1 LIMIT 1 [["callsign", "bazzer"]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
(1.5ms) BEGIN
SQL (29.1ms) INSERT INTO "notices" ("content", "latitude", "longitude", "character_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["content", "jh"], ["latitude", 51.80182150078305], ["longitude", -0.54107666015625], ["character_id", 1], ["created_at", "2015-08-03 11:12:35.249337"], ["updated_at", "2015-08-03 11:12:35.249337"]]
(9.2ms) COMMIT
--- !ruby/hash:ActionController::Parameters
utf8: ✓
authenticity_token: ERlV0sDP7QWVimD1iErH7ICWMo7z5u7ASJBaFbotoL5HPZ5fo5eoZD38mKfSPDXXW1ew7ttBN/FPQoqQEfnbkQ==
callsign: bazzer
notice: !ruby/hash:ActionController::Parameters
latitude: '51.80182150078305'
longitude: '-0.54107666015625'
content: jhgf
commit: Drop
controller: notices
action: create
Completed 500 Internal Server Error in 87ms (ActiveRecord: 42.4ms)
NoMethodError (undefined method `[]' for nil:NilClass):
app/controllers/notices_controller.rb:15:in `create'
Cannot render console with content type multipart/form-dataAllowed content types: [#<Mime::Type:0x000001052aae80 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, #<Mime::Type:0x000001052aab60 @synonyms=[], @symbol=:text, @string="text/plain">, #<Mime::Type:0x000001052b2ab8 @synonyms=[], @symbol=:url_encoded_form, @string="application/x-www-form-urlencoded">]
Why is the active_comment_relationship
parameter not being submitted?