Rails form_for fields_for不提交参数

时间:2015-08-02 23:43:02

标签: ruby-on-rails

我无法获得提交所需参数的表单。我有一个表单需要接受一个hidden_​​field的模型通知。 这是视图中的表单:

<%= 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

日志:

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">]

为什么没有提交active_comment_relationship参数?

2 个答案:

答案 0 :(得分:0)

您很可能不构建此active_comment_relationship对象。您没有提供控制器操作或此表单的模型,因此很难说清楚。您可以在html中查看源代码并查看此隐藏字段是否存在。或者您可以尝试添加以下行:

RewriteEngine On
RewriteRule (.*)lang,fr-fr/ $1 [L]

如果缺少active_comment_relationship,它将构建它。

假设您的通知具有has_one active_comment_relationship关联。

答案 1 :(得分:0)

好的,我有一个解决方案。只需更改

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

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

做了这个伎俩。参数现在包括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

(请注意,multipart已更改为multiple,仅因为文档几乎没有提及multipart,但两者都有效。

在相关说明中,将上述内容更改为实例变量不起作用(同样未提交active_comment_relationship参数):

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

其变化也不起作用:

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

我不知道为什么实例变量版本不起作用。如果有人知道为什么听到你的声音会很棒!