簡體   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