【发布时间】:2021-10-30 00:18:14
【问题描述】:
我需要从数据库中获取变体 Active Storage (Ruby on Rails 6.1) 的选项(例如 resize_to_limit: [300, 222], kuwahara: '3%' )。 我的决定:
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 并不是最好的方法。或许您可以为我推荐最好的解决方案?
附:是否建议使用 Ruby 安全级别?
【问题讨论】:
标签: ruby-on-rails eval rails-activestorage