如何使用file_field_tag与carrierwave
How to use file_field_tag with carrierwave
我试图通过助手优化我的项目,但遇到了问题。
优化前我的 .erb 看起来像:
# app/views/object/_form.html.erb
<%= form_for(@object) do |f| %>
<%= f.label :image %>
<%= f.file_field :image %>
<%= f.submit %>
<% end %>
优化后我想看:
# app/views/object/_form.html.erb
<%= form_for(@object) do |f| %>
<%= f.labeled_image :image %>
<%= f.submit %>
<% end %>
# app/helpers/application_helper.rb
module ApplicationHelper
class ActionView::Helpers::FormBuilder
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::FormOptionsHelper
include ActionView::Helpers::AssetTagHelper
include ActionView::Helpers::FormHelper
def labeled_image (name, options = {})
label = label_tag @object_name + "[#{name}]", I18n.t(name)
file_field = file_field_tag @object_name + "[#{name}]"
content_tag 'div', label+file_field
end
end
end
这段代码没有错误。此外,它生成正确的代码,但 carrierwave 不接收我发送的文件。
我只找到了这个解决方法:
# app/views/object/_form.html.erb
<%= form_for(@object) do |f| %>
<%= f.labeled_image f, :image %>
<%= f.submit %>
<% end %>
# app/helpers/application_helper.rb
module ApplicationHelper
class ActionView::Helpers::FormBuilder
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::FormOptionsHelper
include ActionView::Helpers::AssetTagHelper
include ActionView::Helpers::FormHelper
def labeled_image (f, name, options = {})
label = label_tag @object_name + "[#{name}]", I18n.t(name)
file_field = f.file_field name
content_tag 'div', label+file_field
end
end
end
但看起来像菜鸟:)
简单
只是self.file_field name
。
我试图通过助手优化我的项目,但遇到了问题。 优化前我的 .erb 看起来像:
# app/views/object/_form.html.erb
<%= form_for(@object) do |f| %>
<%= f.label :image %>
<%= f.file_field :image %>
<%= f.submit %>
<% end %>
优化后我想看:
# app/views/object/_form.html.erb
<%= form_for(@object) do |f| %>
<%= f.labeled_image :image %>
<%= f.submit %>
<% end %>
# app/helpers/application_helper.rb
module ApplicationHelper
class ActionView::Helpers::FormBuilder
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::FormOptionsHelper
include ActionView::Helpers::AssetTagHelper
include ActionView::Helpers::FormHelper
def labeled_image (name, options = {})
label = label_tag @object_name + "[#{name}]", I18n.t(name)
file_field = file_field_tag @object_name + "[#{name}]"
content_tag 'div', label+file_field
end
end
end
这段代码没有错误。此外,它生成正确的代码,但 carrierwave 不接收我发送的文件。
我只找到了这个解决方法:
# app/views/object/_form.html.erb
<%= form_for(@object) do |f| %>
<%= f.labeled_image f, :image %>
<%= f.submit %>
<% end %>
# app/helpers/application_helper.rb
module ApplicationHelper
class ActionView::Helpers::FormBuilder
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::FormOptionsHelper
include ActionView::Helpers::AssetTagHelper
include ActionView::Helpers::FormHelper
def labeled_image (f, name, options = {})
label = label_tag @object_name + "[#{name}]", I18n.t(name)
file_field = f.file_field name
content_tag 'div', label+file_field
end
end
end
但看起来像菜鸟:)
简单
只是self.file_field name
。