set height and width of infowindow gmaps4rails in controller - gmaps4rails

class VenuesController < ApplicationController
def index
#search = Venue.search(params[:search])
#venues = #search.all(:order => "name ASC")
#json = #search.all.to_gmaps4rails do |venue, marker|
marker.infowindow render_to_string(:partial => "info", :locals => { :venue => venue})
end
end
end

u need to set these things on the css. THe fact is that the partial that u load have some HTML codes. So you implement some css class to it and then customize this class to appear as you want the box to appear.
Youll find gmaps css on your stylesheets folder.
You cant, or at least is not desirable that you set things from view in controller.

Related

Netzke with file uploads, preferably carrierwave

I have a model with a single field with carrierwave for file upload. I searched the google group but don't find a straightforward way to add file uploads to netzke component. I will be happy with doing it in a panel view with a regular browser file upload, i don't need anything fancy.
I just saw one of the demos has a file upload, the Paging form with custom layout. I think I still need guidance how to put it into the Basepack Grid
File upload field should first be added into Basepack::Form (which then can be tested independently):
class AttachmentForm < Netzke::Basepack::Form
def configure(c)
super
c.model = "Attachment"
c.items = [{xtype: :fileuploadfield, name: 'attachment'}]
end
end
Then your grid should be configured to use this form instead of the built-in one:
class AttachmentGrid < Netzke::Basepack::Grid
def configure(c)
super
c.model = 'Attachment'
c.force_fit = true
c.columns = [:link]
c.bbar = [:add_in_form, :del]
end
def preconfigure_record_window(c)
super
c.form_config.klass = AttachmentForm
c.width = 600
c.height = 150
end
end
For reference, here's also the Attachment model:
class Attachment < ActiveRecord::Base
mount_uploader :attachment
# this is a virtual column referred to from `AttachmentGrid`;
# can be moved over to grid itself using `getter` in case
# you object putting HTML into the model
def link
"<a href='#{attachment.url}' target='_blank'>#{attachment.file.try(:identifier)}</a>"
end
end

Override default scope in active_admin form.has_many

Given a model Post that has_many attachments, and an attachment has a hidden flag. Throughout the app I want to easily say post.attachments and only get the visible ones, so i setup a default scope in the Attachment model (using squeel):
default_scope -> { where { (hidden != true) | (hidden == nil) } }
But the admin page needs to be able to see all attachments for a post, not just the visible ones (so you can toggle the hidden checkbox). The default way of doing this (in admin/posts.rb) uses the default_scope and only lets me edit the visible ones:
f.has_many :attachments do |a|
...
end
I know I could just not use default_scope and instead name it :visible, and then everywhere (except the admin page) say post.attachments.visible but I prefer not having the do that.
How can I unscope the children attachments on the admin page?
Here's the solution I worked out:
In app/admin/posts.rb
f.has_many :attachments, for: [:attachments, f.object.attachments_including_hidden] do |a|
...
end
And in app/models/posts.rb
def attachments_including_hidden
Attachment.unscoped.where( attachable_id: id )
end
(where Attachment model belongs_to: :attachable, polymorphic: true )
What's going on? ActiveAdmin uses Formtastic, which uses Rails Form Builder.
The form.has_many method is an ActiveAdmin method, which calls Formtastic's form.inputs, which in turn calls Rails' fields_for. The :for option will get passed all the way down the fields_for, which can take a collection (as its 2nd arg) so I deliver this collection to it explicitly.

Displaying a thumbnail with RMagick in Rails

I just installed RMagick and am trying to display a thumbnail of any image I throw at it on my webpage. But the code from the tutorial opens up a new window in order to show my image. Here's what I have.
require 'RMagick'
class StaticPagesController < ApplicationController
include Magick
def home
#cat = ImageList.new("app/assets/images/me.jpg")
end
end
and the view:
<%= #cat.display %>
I want the image to be displayed on the page instead a new window pops up displaying the image and this is displayed on the page:
#<Magick::ImageList:0x007f564804bb18>
UPDATE
I need to resize the image before I display it.
From the documentation of display:
Displays the images in the imagelist to any X Window screen. By
default displays to the local screen. You can specify a different
screen by assigning the name to the server_name attribute in the
optional arguments block.
That's not the right method to use in a HTML page. If you already have the image saved, you don't need to load it back into an ImageList. You can just display it in your view:
<%= image_tag "me.jpg" %>
should do the trick.

Rails 3 Active Admin add preset value to new record

I have tried to do it from the controller and from the active admin override controller and I can't make it work.
A user creates a website.
current_user has an id attribute
website has an user_id attribute
So when I create a new website I want to add the current_user.id into website.user_id. I can't.
Anybody know how?
Right now I need it on the new/create actions but I'll probably need this on the edit/update actions too.
This seems to work for me:
ActiveAdmin.register Website do
controller do
# Do some custom stuff on GET /admin/websites/*/edit
def edit
super do |format|
# Do what you want here ...
#website.user = current_user
end
end
end
end
You should be able to override other controller actions in the same way.
ActiveAdmin.register Model do
# also look in to before_create if hidden on form
before_build do |record|
record.user = current_user
end
end
See https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource_dsl.rb#L156
You need to add a 'new' method to the controller. The 'new' method creates an empty website object that will be passed to the form. The default 'new' method just creates an empty #website object. Your 'new' method should create the empty object, and then initialize the value of user to current user:
ActiveAdmin.register Website do
controller do
# Custom new method
def new
#website = Website.new
#website.user = current_user
#set any other values you might want to initialize
end
end

rails 3 custom mime type - default view format

I need to render some views without layout.
To skip line :render :layout=>false and if else logic from controller actions,
i have custom mime type like phtml (plain html).
Mime::Type.register "text/phtml", :phtml
this format need to render the same html views, but only without layout. I complete this with this chunk of code in app. controller:
before_filter proc { |controller|
if params[:format] && params[:format]=='phtml'
controller.action_has_layout = false
controller.request.format = 'html'
end
}
First, this is ugly, second i can't any more control this format from controller in the way:
respond_to :phtml,:only=>:index
because it will always render view with requested format phtml.
is there a better solution? example? how can i alias view formats?
Thank a lot
You can use the layout method in your controller directly:
class ProductsController < ApplicationController
layout "product", :except => [:index, :rss]
end
Or to use no layout at all:
class ProductsController < ApplicationController
layout nil
end
Check out the guide for more info.
I haven't found better solution,only update to my previous example:
before_filter proc { |controller|
if params[:format] && params[:format]=='plain_html' && controller.collect_mimes_from_class_level.include?(:plain_html)
controller.action_has_layout = false
controller.request.format = 'html'
end
}
i added this line to check is a new format defined into our controller:
controller.collect_mimes_from_class_level.include?(:plain_html)
Now we can have full new format which will render our standard html vews rather the build new views for the new format.
This can be useful if we wont to share existing html code, but build different logic based on requested format.
For example, we can easily prepare our html content for print like:
class PagesController < ActionController::Base
layout 'print',:only=>:show
respond_to :plain_html,:only=>[:show]
def show
Page.find(1)
respond_with #page
end
end
And request will be like:
http://www.example.com/pages/1.plain_html
I hope that someone will find this useful.
If u have a better approach for doing this, please share with us.
Regards