简体   繁体   中英

How do I insert html into database for ruby on rails app

Hi I want to add the following input to my application into [Task[:color]]. Any ideas how I can do this such that the value for the color picker gets passed in to the :color attribute?

It is a jQuery plugin I installed from: https://github.com/carloscabo/jquery-palette-color-picker

<input class= "color_picker" 
 type="text" 
 id ="task_color" 
 name="task[color]" 
 data-palette='["#0F8DFC","rgba(135,1,101)","#F00285","hsla(190,41%,95%,1)","#94B77E","#4C060A","#053F32","#ED8074","#788364"]' 
 value="#053F32">

Alternatively, is there a better way I can add a color palette to a form. I would like to be able to choose the colours showed up in the color palette as well.

If you put that input in a form of your views when is submitted the value is send in params, so you can get the value on your action like this:

def process_data
  color = Task.new(color: params[:task][:color])
end

You should really not store html/css in a db. You could always use partials instead. Use your db to persist & store data/variables related to models & not html/css. Place for your html is views, the place to make choices as to which html to show is in your controllers. Use your controller to to decide what partial/view is loaded and when and how.

Also, reading html from a db will slow down your apps responsiveness & Rails tends to make text from a db html safe, meaning it will not behave like html right away. This is a security practice, as it is very common to insert 'harmful/bad' code into your db which is executed when you recover it & supply it to an unsuspecting user.

Please refer: https://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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