Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
963 views
in Technique[技术] by (71.8m points)

rails 4 strong params + dynamic hstore keys

I'm having a problem overcoming the new strong params requirement in Rails 4 using Hstore and dynamic accessors

I have an Hstore column called :content which I want to use to store content in multiple languages, ie :en, :fr, etc. And I don't know which language upfront to set them in either the model or the controller.

store_accessor :content, [:en, :fr] #+226 random other il8n languages won't work.

How can I override strong params (or allow for dynamic hstore keys) in rails 4 for one column?

  params.require(:article).permit(
    :name, :content,
    :en, :fr #+226 random translations
  )

Short of...

params.require(:article).permit!

which of course does work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If I understand correctly, you would like to whitelist a hash of dynamic keys. You can use some ruby code as follows to do this:

params.require(:article).permit(:name).tap do |whitelisted|
  whitelisted[:content] = params[:article][:content] 
end

This worked for me, hope it helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...