Arrays RubyonRails6:form通过隐藏字段提交哈希数组,但被<;ActionController::参数。。。。。允许:错误>;

Arrays RubyonRails6:form通过隐藏字段提交哈希数组,但被<;ActionController::参数。。。。。允许:错误>;,arrays,ruby-on-rails,forms,hash,actioncontroller,Arrays,Ruby On Rails,Forms,Hash,Actioncontroller,在POST表单错误上,它显示“用户必须存在” 正在接收正确的参数,包括用户id,但仍拒绝所有参数。 我怀疑这与哈希数组有关,因为它们来自另一个控制器,但通过一个表单 因此,当我检查Post参数时,它会说: <ActionController::Parameters {"email"=>"test@test.com", "user_id"=>"2", "items_bought"=>"[#<LineItem id: 3, product_id: 1, cart_id:

在POST表单错误上,它显示“用户必须存在”

正在接收正确的参数,包括用户id,但仍拒绝所有参数。 我怀疑这与哈希数组有关,因为它们来自另一个控制器,但通过一个表单

因此,当我检查Post参数时,它会说:

<ActionController::Parameters {"email"=>"test@test.com", "user_id"=>"2", "items_bought"=>"[#<LineItem id: 3, product_id: 1, cart_id: 3, quantity: 1>, #<LineItem id: 4, product_id: 2, cart_id: 3, quantity: 4>]"} permitted: false>
”test@test.com“,“用户id”=>“2”,“购买的物品”=>“[#,#]”}允许:false>
@post的简单表单

<%= simple_form_for @post, :url => user_posts_path do |f| %>

  <%= f.error_notification %>

  <ul>

    <%= @post.errors.full_messages.each do |message| %>
      <li><%= message %></li>
    <%end%>

  </ul>

   <%= f.input :email, :input_html => { value: "#{current_user.email}" } %>
   <%= f.input :user_id, :as => :hidden, :input_html => { value: "#{current_user.id}" } %>
   <%= f.input :items_bought, :as => :hidden, :input_html => { value: "#{current_cart.line_items.to_a}" } %>

   <%= f.error :base %>
   <%= f.button :submit %>

<% end %>
user_posts_path do|f|%>
{value:“{current_user.email}”}%> :hidden,:input_html=>{value:“{current_user.id}”}%> :hidden,:input_html=>{value:{current_cart.line_items.to_a}}%>
发布控制器

class PostsController < ApplicationController

 def new
    @user = current_user
    @post = Post.new
 end

 def create
    @user = current_user
    @post = current_user.posts.build(params[:post_params])

  if @post.save 
    render plain:
    params[:post].inspect
  else
    render 'new'
  end

  end

  private

  def post_params
    params.require(:post).permit(:email, :user_id, items_bought: [:LineItem_id [], :product_id[], :cart_id[], :quantity[]])
  end
end
class PostsController
后期模型

class Post < ApplicationRecord

  belongs_to :user, dependent: :destroy


end
class Post

如果您没有使用
post_params
方法,请更改以下内容,我们将不胜感激

@post = current_user.posts.build(params[:post_params])
为此:

@post = current_user.posts.build(post_params)
编辑:请注意,将用户id作为参数传递是一种安全风险,有人可以更改隐藏字段的值。使用当前用户id,而不是从表单接收id

实际上,所有隐藏的信息都是从当前用户检索的,您实际上不需要使用隐藏字段,因为如果您有权访问当前用户,您就已经有权访问所有这些信息(id、购物车项目)

编辑2:如果希望在:items_bunded参数中包含一个哈希数组,则需要使用多个具有特定名称的隐藏字段,以便rails将其解析为哈希

假设你有这样的结构:

[
  {
    id: 1,
    name: 'Foo',
    some_attr: 'Lorem'
  },
  {
    id: 2,
    name: 'Bar',
    some_attr: 'Impsum'
  }
]
如果将其作为参数发送,则需要多个隐藏字段:

hidden_field_tag 'items_bought[1][id]', 1
hidden_field_tag 'items_bought[1][name]', 'Foo'
hidden_field_tag 'items_bought[1][some_attr]', 'Lorem'
hidden_field_tag 'items_bought[2][id]', 2
hidden_field_tag 'items_bought[2][name]', 'Bar'
hidden_field_tag 'items_bought[2][some_attr]', 'Impsum'
提交时,
参数[:购买的物品]
将为:

{
  '1' => {
    id: '1',
    name: 'Foo',
    some_attr: 'Lorem'
  },
  '2' => {
    id: '2',
    name: 'Bar',
    some_attr: 'Impsum'
  }
}
注意,这不一样,它是散列的一个散列,如果每个散列都是键,那么就有ID(这是隐藏字段名
items\u bunded[1]
items\u bunded[2]
,ID是字符串,而不是数字)

您还可以将哈希序列化为字符串:

hidden_field_tag :items_bougth, my_hash.to_json
然后在控制器上执行

param_hash = JSON.parse(params[:items_bought]
但我不建议这样做,这是一个比需要做的更复杂的事情的标志,你需要解析参数,使用一个额外的库,等等


请注意,您不应该对复杂对象(如LineItem)执行任何操作,因为您谈论的是散列数组,LineItems数组不是散列数组,您必须先将对象转换为散列才能使这两种方法一致工作。

这可能是由于_post.json.jbuilder.rb……json.extract!post,:user_id,:email,items_bunded:[:LineItem_id[],:product_id[],:cart_id[],:quantity[],:created_at,:updated_at json.url用户发布url(用户,发布,格式::json)这看起来像是一次真正失败的尝试,应该使用嵌套属性或拆分为多个控制器操作。您能否重新表述问题,询问您实际试图实现的目标,而不是您认为的解决方案是什么?谢谢您的及时回复@arieljoud,您是对的,我做了更改您建议。这样做是为了查看会话ie当前_用户将传递什么。问题在于将哈希数组传递到数据库。我明白您在控制器中而不是表单上传递该信息的意思。但是有没有通过更改post_参数方法来传递哈希数组的方法?您不应该这样做不要将散列数组作为一个参数传递,这里只有一个行项数组的字符串表示,这不是http参数的工作方式,我将编辑我的答案,向您展示在您的情况下应该如何做,但我不认为这是一个用例,这很难,因为这不是正确的方法,谢谢@arieljuod for p把我从兔子洞里拉出来。我将通过控制器来传递数据。顺便说一句,使用隐藏的字段标记:items\u bouth,my\u hash.to\u json是一个令人讨厌的东西。我将尝试一下,看看它是如何进行的。