0

newbie in rails,

i'm trying to use refinerycms in my rails app, refinery already available to upload an images to the database, i'm trying to combine it to an extension that i use, "events" extension, so when i create a new events that should create 2 object, events to refinery_event table which have image_id as a foreign key for images id, and the event image attribute that will be saved to refinery_images,

i've add a migrations image_id column to refinery_event and put this relation to event.rb models

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

add this in 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

and this is the form looks like,

<%= 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 %>

and i'm stuck what else ? how to make this form work? anyone can teach me?

thanks..

Auliya Fadli
  • 87
  • 12

1 Answers1

0

As answered here: https://groups.google.com/forum/#!msg/refinery-cms/5RbAD079IPc/nVFGKdnfAQAJ

You can use the built in image picker, as we can see if we use the generator:

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

This creates the following admin controller:

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

And the following template:

<%= 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 -%>
parndt
  • 1,873
  • 11
  • 13
  • thanks for the respon mr.parndt, but i'm not sure that i need to generate any engine more.. i already generate event engine.. and now i have two tables, refinery_images and refinery_events, i just want to saving into two tables from one form, events form, so when i create an event object, there is an images upload button, so when i click save button, the form saving into refinery_events table and refinery_images_ table.. – Auliya Fadli Jun 09 '16 at 09:54
  • 2
    The reason why I generated an extension was to demonstrate the code which _would_ work for your situation. If you used the code pasted above, you'd be able to attach an image to an event happily. – parndt Jun 13 '16 at 03:15