繁体   English   中英

Ruby on Rails 6:表单通过隐藏字段提交散列数组但被阻止<ActionController::Parameters ..... permitted: false>

[英]Ruby on Rails 6: form submits an array of hashes via hidden field but gets blocked by <ActionController::Parameters ..... permitted: false>

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

正在接收正确的参数,包括 user_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>

Simple_form_for @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 %>

帖子控制器

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 Post < ApplicationRecord

  belongs_to :user, dependent: :destroy


end

任何帮助将不胜感激

你没有使用你的post_params方法,改变这个:

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

对此:

@post = current_user.posts.build(post_params)

编辑:请注意,将 user_id 作为参数传递存在安全风险,有人可以更改隐藏字段值。 使用 current_user id 而不是从表单接收 id。

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

编辑 2:如果您想在 :items_bought 参数中包含一个散列数组,您需要使用多个具有特定名称的隐藏字段,以便通过 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'

提交后, params[:items_bought]将是:

{
  '1' => {
    id: '1',
    name: 'Foo',
    some_attr: 'Lorem'
  },
  '2' => {
    id: '2',
    name: 'Bar',
    some_attr: 'Impsum'
  }
}

请注意,它不一样,它是散列的散列,如果每个散列作为键,您就有 ID(这是隐藏字段名称items_bought[1]items_bought[2]的第一部分,id 是字符串,而不是数字.

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

hidden_field_tag :items_bougth, my_hash.to_json

然后在控制器上做

param_hash = JSON.parse(params[:items_bought]

但我不建议这样做,它表明某些事情做得比它需要的更复杂,您需要解析参数,使用额外的库等。

请注意,您不应该对像 LineItem 这样的复杂对象按原样执行任何操作,您谈论的是散列数组,LineItem 数组不是散列数组,您必须先将对象转换为散列,这两种方法才能工作始终如一。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM