Refinerycms - Adding an image field to the blog engine
Asked Answered
E

4

8

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!

Elodea answered 21/8, 2011 at 15:54 Comment(1)
Did you come up with a solution to this? I'm looking to do exactly the same thing :Manche
P
14

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

Phyl answered 23/11, 2012 at 4:35 Comment(1)
This was excellent thanks. Only change was the migration needed to be refinery_blog_posts. Also to get the form file you can run rake refinery:override view=refinery/blog/admin/posts/_formYuen
M
7

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! :)

Manche answered 8/9, 2011 at 16:32 Comment(0)
F
3

Have you thought about using page-images for this?

https://github.com/resolve/refinerycms-page-images

Fairing answered 5/9, 2012 at 12:7 Comment(0)
L
1

I will update the answer for rails 4.x and Refinery 3.x

Create a new migration adding the new image_id field to the refinery_blog_posts model:

rails generate migration add_image_id_to_refinery_blog_posts image_id:integer

Then run the migration:

rake db:migrate

Now you will need to create a file under the decorators/refinery/blog/ directory with the following name post_decorator.rb and then write this code inside of it:

decorators/refinery/blog/post_decorator.rb

Refinery::Blog::Post.class_eval do
  belongs_to :image, :class_name => '::Refinery::Image'
end

When the decorator is done, you will need to add the new image_id field to the controllers/refinery/blog/admin/posts_controller.rb to the permitted params, like this:

controllers/refinery/blog/admin/posts_controller.rb

def post_params
  params.require(:post).permit(:title, :body, :custom_teaser, :image_id, :tag_list, :draft, :published_at, :custom_url, :user_id, :browser_title, :meta_description, :source_url, :source_url_title, :category_ids => [])
end

After the above, the only thing that will remain it will be to add the new image_id field to the form in order to be able to add an image in this case to the post:

views/refinery/blog/admin/posts/_form.html.erb

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

Now you are all done, following this steps you will be able to add any kind of field to a refinery model, like a post in this case.

Hope it helps :)

Livingstone answered 24/7, 2015 at 4:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.