setting hstore in rails4, dynamic key/values

前端 未结 3 1030
名媛妹妹
名媛妹妹 2021-01-01 01:52

I\'m playing around with Hstore for the first time in a rails4 app, and I am using javascript in a form to build out dynamic form fields for the hstore column (:schema)

相关标签:
3条回答
  • 2021-01-01 02:22

    found this here: http://guides.rubyonrails.org/action_controller_overview.html#more-examples

    and in my controller I used:

    def app_params
      params.require(:app).permit(:name, :title).tap do |whitelisted|
        whitelisted[:schema] = params[:app][:schema]
      end
    end
    
    0 讨论(0)
  • 2021-01-01 02:29

    Here's a way that also allows deleting of the hstore keys by submitting empty parameters.

    In your ApplicationController add this method:

    # Returns the hstore keys to be whitelisted.
    #
    # @param key [Symbol] the name of the hstore field
    # @param params [Hash] the parameters for the hstore field
    #
    # @return [{Symbol => Array<Symbol>}, Symbol]
    def permit_hstore_params(key, hstore_params)
      keys = hstore_params.try(:keys)
    
      # Return key if params are empty, 
      # this allows the hstore key to be removed.
      return key if keys.blank?
    
      # Otherwise, return the keys to be whitelisted
      { key => keys }
    end
    

    Example:

    class DynamicRecord < ActiveRecord::Base
      store_accessor :metadata
    end
    
    class DynamicRecordController < ApplicationController
      # ...
    
      def dynamic_model_params
        params
          .require(:dynamic_model)
          .permit(:name, permit_hstore_params(:metadata, params[:dynamic_model][:metadata]))
      end
    end
    
    0 讨论(0)
  • 2021-01-01 02:45

    I think Rails must have simplified this in recent versions (current as of 5.2.3 at least)... and much cleaner/easier:

    params.require(:parent).permit(:name, :whatever, data: {})

    This will allow and store any/all attributes of data into an hstore field. An example of POSTing or PUTing a data nested attribute via HTML:

    <input type="text" name="parent[data][your_super_custom_nested_data] />`

    4th example down: https://guides.rubyonrails.org/action_controller_overview.html#more-examples

    0 讨论(0)
提交回复
热议问题