Refinerycms 如何上传图片到事件,但图片保存到 refinery_images table

Refinerycms how to upload image to events, but the image saved to refinery_images table

rails、

的新手

我正在尝试在我的 rails 应用程序中使用 refinerycms,refinery 已经可以将图像上传到数据库,我正在尝试将它与我使用的扩展程序结合起来,"events" 扩展,所以当我创建一个应该创建 2 个对象的新事件时,refinery_event table 的事件具有 image_id 作为图像 id 的外键,事件图像属性将保存到 refinery_images,

我已将迁移 image_id 列添加到 refinery_event 并将此关系与 event.rb 模型

belongs_to :image_id, :class_name => '::Refinery::Image'

将此添加到 event_controller.rb

def image_params
  params.require(:event).permit(:image_name, :image_size, :image_width, :image_height, :created_at, :updated_at)
end

def new
  @event = Event.new
  @image_id = Refinery::Image.new
end

def create
  @event = Event.new(event_params)
  @image_id = Refinery::Image.new(image_params)

  if @event.save
      begin
        Mailer.notification(@event, request).deliver_now
      rescue => e
        logger.warn "There was an error delivering the event notification.\n#{e.message}\n"
      end

      if Event.column_names.map(&:to_s).include?('email')
        begin
          Mailer.confirmation(@event, request).deliver_now
        rescue => e
          logger.warn "There was an error delivering the event confirmation:\n#{e.message}\n"
        end
      else
        logger.warn "Please add an 'email' field to Event if you wish to send confirmation emails when forms are submitted."
      end

      redirect_to refinery.thank_you_events_events_path
    else
      render :action => 'new'
  end
end

def event_params
 params.require(:event).permit(:nama, :deskripsi, :periode_start, :periode_end, :lokasi, :jumlah, :gender_id, :age_id, :event_types_id, :des_acara, :key_kata, :tipe_sponsor, :dana, :exposure, :enggagement, :image_id)
end

这是表格,

<%= form_for [refinery, :events, @event], :html => { :multipart => true } do |f| %>
  <%= render '/refinery/admin/error_messages',
             :object => @event,
             :include_object_name => true  %>

  <div class='field nama_field string_field'>
    <%= f.label :nama %>
    <%= f.text_field :nama %>
  </div>

  <div class='field deskripsi_field text_field'>
    <%= f.label :deskripsi %>
    <%= f.text_area :deskripsi, :rows => 8 %>
  </div>

  <div class="field">
    <p>
      <%= f.file_field :image_id %>
    </p>
  </div>

  <div class='actions'>
    <%= f.submit t('.send') %>
  </div>
<% end %>

我还有什么问题?如何使这个表格工作? 谁能教教我?

谢谢..

正如这里的回答:https://groups.google.com/forum/#!msg/refinery-cms/5RbAD079IPc/nVFGKdnfAQAJ

您可以使用内置的图像选择器,如果我们使用生成器就可以看到:

$ rails generate refinery:engine event nama:string deskripsi:text image:image

这将创建以下管理控制器:

module Refinery
  module Events
    module Admin
      class EventsController < ::Refinery::AdminController


        crudify :'refinery/events/event',
                :title_attribute => 'nama'


        private


        # Only allow a trusted parameter "white list" through.
        def event_params
          params.require(:event).permit(:nama, :deskripsi, :image_id)
        end
      end
    end
  end
end

以及以下模板:

<%= form_for [refinery, :events_admin, @event] do |f| -%>
  <%= render '/refinery/admin/error_messages',

              :object => @event,
              :include_object_name => true
%>


  <div class='field'>
    <%= f.label :nama -%>
    <%= f.text_field :nama, :class => 'larger widest' -%>
  </div>


  <div class='field'>
    <%= render '/refinery/admin/wysiwyg',
                :f => f,
                :fields => [:deskripsi],
                :object => "events/event" -%>
  </div>


  <div class='field'>
    <%= f.label :image -%>
    <%= render '/refinery/admin/image_picker',
               :f => f,
               :field => :image_id,
               :image => @event.image,
               :toggle_image_display => false -%>
  </div>


  <%= render '/refinery/admin/form_actions', :f => f,
             :continue_editing => false,
             :delete_title => t('delete', :scope => 'refinery.events.admin.events.event'),
             :delete_confirmation => t('message', :scope => 'refinery.admin.delete', :title => @event.nama) -%>
<% end -%>


<% content_for :javascripts do -%>
  <script>
    $(document).ready(function(){
      page_options.init(false, '', '');
    });
  </script>
<% end -%>