Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refinerycms - Adding an image field to the blog engine

I have a refinerycms app with the community blog engine installed on it. I would like to add an image field to the blog_post so that I can choose a main image for the post and have it show in my views.

I've tried adding an image field, no joy. Then I looked at one of my other custom engines with an image field and that uses image_id to link to the main image table, so I tried adding an image_id field instead and the editing the blog_post model to have the same 'belongs_to" line. THe edit page for the blog loads, and the image picker partial works, but when I hit save it looks like nothing is being sent to my table.

One thing that concerns me is when I created my custom engine with the image field, I specified it as the field type image. This appears to have created the image_id field on the back end, and set everything up so I can still reference an image class. Adding an image field to the blog didn't do that, just created a field type called image. When inspecting the tables for my custom engine, there is no field type called image, so somewhere there is some transformation magic that I am not able to recreate.

Currently I have the following code:

Created this migration:

class AddPictureToBlog < ActiveRecord::Migration
 def self.up
   add_column :blog_posts, :main_image_id, :integer
 end

 def self.down
   remove_column :blog_posts, :main_image_id
 end
end

Added this to the blog_post model:

  belongs_to :main_image_id, :class_name => 'Image'

and have this on the view:

    <%= f.label :main_image_id -%>
<%= render :partial => "/shared/admin/image_picker", :locals => {
      :f => f,
      :field => :main_image_id,
      :image => @blog_post.main_image_id,
      :toggle_image_display => false
    } %>

The custom engine doesn't even refer to the _id field, so I dont know which links im missing here. Any help would be greatly appreciated. It may not be a refinerycms specific problem at all - I am new at rails so there maybe some basics im missing here.

Thanks!

like image 575
Adam Avatar asked Aug 21 '11 15:08

Adam


2 Answers

For rails 3.2.3 and refinerycms 2.0.0 the bleow code works,

Create a new migration:

rails generate migration add_image_id_to_refinery_blog_posts image_id:integer
rake db:migrate

under "decorators/refinery/blog/" create a file post_decorator.rb

add the following lines,

Refinery::Blog::Post.class_eval do
  # Whitelist the :image_id parameter for form submission
  attr_accessible :image_id
  belongs_to :image 
end

generate the refinery form file:

rake refinery:override view=refinery/blog/admin/posts/_form

and add the below code in "views/refinery/blog/admin/posts/_form.html.erb"

<div class="field">
  <%= f.label :image_id %>
  <%= render :partial => "/refinery/admin/image_picker", :locals => {
    :f => f,
    :field => :image_id,
    :image => f.object.image,
    :toggle_image_display => false
  }
  %>
</div>

for more details, refer the link extending-models

like image 148
Arivarasan L Avatar answered Sep 23 '22 03:09

Arivarasan L


This is the way I did it in the end (but I have put a feature request in ;) ):

Create a new migration:

rails generate migration add_image_id_to_blog_posts image_id:integer
rake db:migrate

Add this to the blog_post.rb Model:

attr_accessible :image_id
belongs_to :image

Amend the blog admin form view to include the following:

<div class='field'>
  <%= f.label :image -%>
  <%= render :partial => "/shared/admin/image_picker", :locals => {
        :f => f,
        :field => :image_id,
        :image => f.object.image,
        :toggle_image_display => false
      } %>
</div>

You should be good to go then! :)

like image 43
Chris Edwards Avatar answered Sep 19 '22 03:09

Chris Edwards