Rails form_for fields_for doesn't send parameter

I cannot get the form to submit the parameters I want. I have a form that needs to accept a hidden field to notify a model. This is the form in 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

      

Magazines:

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 parameter active_comment_relationship

not being sent?

+3


source to share


2 answers


Chances are you won't create this active_comment_relationship object. You haven't exposed your controller or model action for this form, so it's hard to tell. You can view the source in html and see if there is a hidden field. Or you can try adding the following line:

<% f.object.build_active_comment_relationship unless f.object.active_comment_relationship %>
<%= f.fields_for :active_comment_relationship do |ff| %>

      



It will build active_comment_relationship if missing.

Assuming your notification has has_one active_comment_relationship.

0


source


Ok, I have a solution. Simple change

<%= form_for(:notice, url: :notices, method: :post, html: { multipart: true, class: "comment_form" } ) do |f| %>

      

to

<%= form_for(:notice, url: :notices, method: :post, multiple: true, html: { class: "comment_form" } ) do |f| %>

      

did the trick. The options now include active_comment_relationship:

authenticity_token: oqkA9bI2Jnt87CtSW0gNYdWJVzrmB63D/fsvoeskmNz0jct40W5jGtSa0wABPv9aDkjVWs6gdPL6Kf8kQPDj8w==
callsign: bazzer
notice: !ruby/hash:ActionController::Parameters
  active_comment_relationship: !ruby/hash:ActionController::Parameters
    commentee_id: '15071'
  latitude: '51.7559402747204'
  longitude: '-0.7470703125'
  content: jjj
commit: Post
controller: notices
action: create

      



(Note that multipart

changed to multiple

, just because the docs hardly mention multipart

, although both work).

On a related note, changing the above instance variable does not work (again, the active_comment_relationship parameter was not sent):

<%= form_for(@notice, url: :notices, method: :post, multiple: true, html: { class: "comment_form" } ) do |f| %> <!-- FAIL -->

      

Variations also don't work:

<%= form_for(@notice, url: :notices, multiple: true) do |f| %> <!-- FAIL -->
<%= form_for(@notice, multiple: true) do |f| %> <!-- FAIL -->

      

I have no idea why the instance variable version is not working. If anyone knows why it would be great to hear from you!

0


source







All Articles