从 db 获取选项 Active Storage 变体的最佳方法是什么?
What's the best way to get options Active Storage variant from db?
我需要获取变体 Active Storage(Ruby on Rails 的选项(例如,resize_to_limit: [300, 222],kuwahara: '3%') 6.1) 来自数据库。
我的决定:
app/admin/slideshow.rb
form do |f|
f.inputs 'Slideshow' do
f.input :name
f.input :options,
input_html: { value: f.object.options || "{ resize_to_limit: [300, 222], kuwahara: '3%' }" },
label: 'Options. For example: { resize_to_limit: [300, 222], monochrome: true }'
f.input :images, as: :file, input_html: { multiple: true }
end
f.actions
end
app/controllers/slideshow_controllers.rb
def options
@options = proc {
$SAFE = 1
eval(Slideshow.take.options) if slideshow
}.call
end
def slideshow
Slideshow.published.take
end
index.html.erb
<% if slideshow_present? %>
<% @slideshow.images.each do |x| %>
<a href="<%= path_to_file(x) %>" data-lightbox="photo" class="col-sm-4">
<%= image_tag x.variant(@options),
:class => "img-fluid" %>
</a>
<% end %>
<% end %>
app/helpers/slideshows_helper.rb
def path_to_file(x)
Rails.application.routes.url_helpers.rails_blob_path(x, only_path: true)
end
但我认为使用 eval 并不是最好的方法。也许你可以为我推荐最好的解决方案?
P.S。使用 Ruby 安全水平是否可取?
很简单...
app/admin/slideshow.rb
form do |f|
f.inputs 'Slideshow' do
f.input :name
f.input :options,
input_html: { value: f.object.options || '{ "resize_to_limit": [300, 222], "kuwahara": "3%", "quality": 15 }' },
label: 'Options. For example: { "resize_to_limit": [300, 222], "monochrome": true }'
f.input :images, as: :file, input_html: { multiple: true }
end
f.actions
end
app/controllers/slideshow_controllers.rb
def options
# @options = proc {
# $SAFE = 1
# eval(Slideshow.take.options) if slideshow
# }.call
@options = JSON.parse(Slideshow.take.options) if slideshow
end
我需要获取变体 Active Storage(Ruby on Rails 的选项(例如,resize_to_limit: [300, 222],kuwahara: '3%') 6.1) 来自数据库。 我的决定:
app/admin/slideshow.rb
form do |f|
f.inputs 'Slideshow' do
f.input :name
f.input :options,
input_html: { value: f.object.options || "{ resize_to_limit: [300, 222], kuwahara: '3%' }" },
label: 'Options. For example: { resize_to_limit: [300, 222], monochrome: true }'
f.input :images, as: :file, input_html: { multiple: true }
end
f.actions
end
app/controllers/slideshow_controllers.rb
def options
@options = proc {
$SAFE = 1
eval(Slideshow.take.options) if slideshow
}.call
end
def slideshow
Slideshow.published.take
end
index.html.erb
<% if slideshow_present? %>
<% @slideshow.images.each do |x| %>
<a href="<%= path_to_file(x) %>" data-lightbox="photo" class="col-sm-4">
<%= image_tag x.variant(@options),
:class => "img-fluid" %>
</a>
<% end %>
<% end %>
app/helpers/slideshows_helper.rb
def path_to_file(x)
Rails.application.routes.url_helpers.rails_blob_path(x, only_path: true)
end
但我认为使用 eval 并不是最好的方法。也许你可以为我推荐最好的解决方案?
P.S。使用 Ruby 安全水平是否可取?
很简单...
app/admin/slideshow.rb
form do |f|
f.inputs 'Slideshow' do
f.input :name
f.input :options,
input_html: { value: f.object.options || '{ "resize_to_limit": [300, 222], "kuwahara": "3%", "quality": 15 }' },
label: 'Options. For example: { "resize_to_limit": [300, 222], "monochrome": true }'
f.input :images, as: :file, input_html: { multiple: true }
end
f.actions
end
app/controllers/slideshow_controllers.rb
def options
# @options = proc {
# $SAFE = 1
# eval(Slideshow.take.options) if slideshow
# }.call
@options = JSON.parse(Slideshow.take.options) if slideshow
end