In my project i have a gem that uses paperclip for file attachments. an example model in the gem:
class Example do
...
has_attached_file :image,
styles: { mini: '32x32>', normal: '128x128>' },
default_style: :mini,
url: '/example/url/:id/:style/:basename.:extension',
path: ':rails_root/public/example/url/:id/:style/:basename.:extension',
...
end
I want to modify the image so instead of having mini and normal sizes, i can add another size. I also want to change the path. How do i go about this? i tried creating a decorator like:
Example.class_eval do
has_attached_file :image,
styles: { mini: '32x32>', normal: '128x128>', large: '1024x1024' },
default_style: :mini,
url: '/example/url/:id/:style/:basename.:extension',
path: 'updated/example/url/:id/:style/:basename.:extension',
end
this didn't do anything.
Not sure if you've solved this by now, but for any future viewers of this question...
You can do this by modifying the attachment_definitions property of the class. For instance you could add an initializer with:
Example.attachment_definitions[:image][:styles][:normal] = "1000x500"
Example.attachment_definitions[:image][:path] = '...'
Related
I want to be able to to have a view file called my.css.erb.scss in which the ERB is handled and then the SCSS is compiled.
So for example this file:
h1 { color: <%= 'red' %>; }
Would result in:
h1 { color: red; }
Currently the only solution I've found to work is a bit of a fudge and I don't like it (and it's proving difficult to render via this method in specs):
lib/renders.rb:
ActionController.add_renderer :css do |template,options|
string = render_to_string template, options
css = Sass::Engine.new(string, :syntax => :scss).render
self.content_type ||= Mime::CSS
self.response_body = css
end
Then in the controller I need to do this:
respond_to do |format|
format.css { render :css => params[:action] }
end
I've tried adding an registering a renderer via the ActionView::Template::Handler module method with a .call method but I couldn't get the ERB to render then.
I am adding mercury to a simple blogging application and would like to use the newest version, but I have not been able to get it to save updates. Here is my code:
Gemfile:
gem 'rails', '3.2.11'
gem "thin", "~> 1.3.1"
gem 'mercury-rails'
gem 'paperclip'
group :development do
gem 'taps'
gem "nifty-generators"
gem 'sqlite3'
end
gem 'jquery-rails'
blogs_controller.rb:
def update
#blog = Blog.find(params[:id])
if #blog.update_attributes(params[:blog])
redirect_to #blog
else
render 'edit'
end
end
def mercury_update
blog = Blog.find(params[:id])
blog.content = params[:content][:blogContent][:value]
blog.save!
render text: ""
end
blog.rb:
class Blog < ActiveRecord::Base
attr_accessible :title, :content, :author
end
routes.rb:
Mercury::Engine.routes
mount Mercury::Engine => '/'
root :to => "pages#home"
namespace :mercury do
resources :images
end
resources :blogs do
member { post :mercury_update }
end
mercury.html.erb
<body>
<script type="text/javascript">
// Set to the url that you want to save any given page to, leave null for default handling.
var saveUrl = null;
// Instantiate the PageEditor
new Mercury.PageEditor(saveUrl, {
saveStyle: 'form', // 'form', or 'json' (default json)
saveMethod: null, // 'PUT', or 'POST', (create, vs. update -- default PUT)
visible: true // boolean - if the interface should start visible or not
});
</script>
</body>
blogs/show.html.erb:
<div id="blogFull" class="rounded-corners-mild">
<div id="blogTitle">
<h1 style="font-size:150%; text-align:center; " class="mercury-region" ><%= #blog.title%></h1>
</div>
<div data-mercury="full" id="blogContent" class="mercury-region">
<%= raw #blog.content %>
</div><br />
<%= link_to "mercury", "/editor" + "/blogs" + "/#{#blog.id}", :class => 'bestButton',
id: "edit_link", data: {save_url: mercury_update_blog_path(#blog)} %>
added to: mercury.js:
jQuery(window).on('mercury:ready', function() {
var saveUrl = $("#edit_link").data("save-url");
Mercury.saveUrl = saveUrl;
});
Mercury.on('saved', function() {
window.location.href = window.location.href.replace(/\/editor\//i, '/');
});
I also tried with this: to mercury.js:
onload: function() {
//Mercury.PageEditor.prototype.iframeSrc = function(url) { return '/testing'; }
Mercury.on('ready', function() {
var link = $('#mercury_iframe').contents().find('#edit_link');
Mercury.saveUrl = link.data('save-url');
link.hide();
});
Mercury.on('saved', function() {
window.location.href = window.location.href.replace(/\/editor\//i, '/');
});
}
Neither worked. Both got me to the editor and allowed me to edit. I have not been able to save any changes yet. Thank You.
Do you get an error message when trying to save? What does it say? If it says it's unable to save but the URL is correct you want to check your log file.
I had problems setting the correct saveUrl and adding the following to mercury.js fixed it for me;
onload: function() {
Mercury.on('ready', function() {
var link = $('#mercury_iframe').contents().find('#edit_link');
console.log("mercury ready ready", link);
mercuryInstance.saveUrl = link.data('save-url');
link.hide();
});
Mercury.on('saved', function() {
window.location.href = window.location.href.replace(/\/editor\//i, '/');
});
}
Found it in this thread on RailsCasts.
Edit:
I notice something else in your code. You created a route for a POST action. In mercury.html.erb the saveMethod gets set to 'PUT' by default. Change the saveMethod to 'POST'.
For me I had trouble with "mercuryInstance" not being around with the solution above. I did get it to work with this javascript added to the end of mercury.js:
jQuery(window).on('mercury:ready', function() {
var link = $('#edit_link');
Mercury.saveUrl =link.attr('data-save-url');
link.hide();
});
I wanna override html code when working with active_admin gem in Rails; because the nav-bar and many elements in these gem's views are different with my views (other pages). I hope that has a way to change html code without changing css manually! Thanks
It is not very easy , activeadmin use DSL for building html (called "Arbre")
You have to monkey patch every page class, also , it may prevent customizing of css too.
For example to move sidebar to left, create initializer with next patch.
class ActiveAdmin::Views::Pages::Base < Arbre::HTML::Document
def build_page_content
build_flash_messages
div :id => "active_admin_content", :class => (skip_sidebar? ? "without_sidebar" : "with_sidebar") do
build_sidebar unless skip_sidebar?
build_main_content_wrapper
end
end
end
default method was
def build_page_content
build_flash_messages
div :id => "active_admin_content", :class => (skip_sidebar? ? "without_sidebar" : "with_sidebar") do
build_main_content_wrapper
build_sidebar unless skip_sidebar?
end
end
The full list of classes used for rendering can be found here , so some of them you need to patch.
https://github.com/gregbell/active_admin/tree/master/lib/active_admin/views
Be ready for a big piece of work.
UPD. Gem for changing activeadmin sidebar position
https://github.com/Fivell/active_admin_sidebar
How can I add a favicon in Active Admin. I am using rails 3.2 and active admin 0.4.3
Though this question is about an older version of ActiveAdmin, as of this commit ActiveAdmin supports favicons in the initializer. Around line 145 of active_admin.rb for me:
# == Setting a Favicon
#
# config.favicon = '/assets/favicon.ico'
HTH the next person who comes along. I almost did the top-voted answer...
Until they support favicon you can do the trick:
$(document).ready(function(){
$('head').append("<link href='favicon.ico' rel='shortcut icon'>");
});
You'll have to add it to your ActiveAdmin.rb initializer like so :
module ActiveAdmin
module Views
module Pages
class Base < Arbre::HTML::Document
def build_active_admin_head
within #head do
insert_tag Arbre::HTML::Title, [title, render_or_call_method_or_proc_on(self, active_admin_application.site_title)].join(" | ")
insert_tag Arbre::HTML::Link, rel: 'shortcut icon', href: '/favicon.gif'
# Change the href used in this line ^
active_admin_application.stylesheets.each do |style|
text_node(stylesheet_link_tag(style.path, style.options).html_safe)
end
active_admin_application.javascripts.each do |path|
script :src => javascript_path(path), :type => "text/javascript"
end
text_node csrf_meta_tag
end
end
Is there a way I can switch between to views at random for the root path?
root :to => 'pages#blue' or root :to => 'pages#red'
Thanks for any kind of help with this.
You can pass a lambda as the value of :to, so theoretically you could return two different responses randomly. It might be a better idea to swap the layout/view that's rendered though.
Edit
root to: lambda {|env| [ 302, {'Location'=> your_randomizing_code_here }, [] ]}
You could do that, or something very similar in your controller:
class YourRootController < ActionController::Base
def index
render some_method_that_returns_your_view_paths_randomly
end
end