Custom template for padrino admin page - ruby

How can I make padrino-admin page generator produce beautiful custom pages?
By default padrino-admin generates pretty ugly admin pages, totally unmaintainable:
.group
=f.label :title
=f.error_message_on :title
=f.text_field :title, :class => :text_field
%span.description Ex: a simple text
.group
=f.label :name
=f.error_message_on :name
=f.text_field :name, :class => :text_field
%span.description Ex: a simple text
--- more annoyingly redundant frak
.group.navform.wat-cf
=f.submit pat(:save), :class => :button
=f.submit pat(:cancel), :onclick => "window.location='#{url(:pages, :index)}';return false", :class => :button
I wrote a nice AdminFormBuilder < AbstractFormBuilder, connected it with set :default_builder, 'AdminFormBuilder', it generates same admin pages from very short code:
= f.inputs :name, :surname, :email
= f.inputs :password, :password_confirmation, :as => :password
= f.input :role, :as => :select, :options => access_control.roles, :descr => 'a simple text'
= f.submits
Now I want padrino g admin_page to generate more of such pages. What should I do?

There are two ways:
1) Make your custom admin gem copying as base the actual padrino-admin
2) Fork the project (where now we support a new admin based on bootstrap) apply your changes and submit a pull request.
Btw the most interesting file for this job is this: https://github.com/padrino/padrino-framework/blob/master/padrino-admin/lib/padrino-admin/generators/admin_page.rb

Here is one-line patch for padrino-admin gem: https://github.com/ujifgc/padrino-framework/commit/b07399bdfbc15d05682237c64580e77558ac9fce
Now I can place copy of original templates folder from padrino-admin-0.10.5/lib/padrino-admin/generators to vendor/padrino-admin/generators and enjoy my own admin page templates.

Related

How to implement "change your profile picture" feature (server side)

Im trying to implement a feature on my project, so users can change their profile picture. I'm currently at the point where everything on the front end works (when you hover the mouse over the image it shows camera and when you click it, you can browse through your computer to select an image file) Yet, it doesnt work after that point.
I'm not really sure how to handle server request.
FYI
im using routes.rb and controller .rb files to handle backend.
Also,
should i use iframe, instead of form tag in html?
Thanks!
You don't need an iFrame. Simple form-for ruby helper will help in this case. I will suggest you to use a file-uploading gem like paperclip.
Create a model for your records:
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
(I am supposing the model name to be user)
Create a new migration with generator:
rails generate paperclip user avatar
Update your html form as follow:
<%= form_for #user, :url => users_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %>
<% end %>
Then do following changes in the controller:
def create
#user = User.create( user_params )
end
private
def user_params
params.require(:user).permit(:avatar)
end
To display image anyware on the page use the following helper:
<%= image_tag #user.avatar.url %>
This is the easiest way to handle image uploading at server side in rails. You can read more at the paperclip github page.If you don't want to use a gem then then there is a ruby class FileUtils which can help you in achieving same goal.

ruby, sinatra, padrino,slim

I need to develop a simple File Upload
I'm using padrino and slim-templates. Also generated my views using the padrino command generator such as padrino g admin_pages modelName. Now I want to add a fileUpload field to the generated code... I'm getting this error:
undefined method `name' for nil:NilClass
My question is any way to automatically generate the admin page with this functionality or simply add the field manualy?
This is the code:
= f.text_field :newName, :class => 'form-control input-large input-with-feedback',
= f.label :content, :class => 'control-label'
= f.text_area :content, :class => 'form-control input-large input-with-feedback'
= f.file_field :fileimg
= f.submit pat(:save), :multipart => true ,:class => 'btn btn-primary'
Thanks in advance!
It seems that fileimg field is nil for the record f. Make sure that it isn't `nil, and in that case, it shell be validated by a model validation rule.

Paperclip attachment column in database is empty

I have installed paperclip and imagemagick,and implemented the code to my model and view file.
I have a database column named 'picture' and it is empty no matter if i uploaded a picture or not. the picture acctually exists in the/public/system/decks/pictures/000/000/019/medium folder. i can see all of the uploaded pictures there, but i can't show them cause the database is empty.
My model:
class Deck < ActiveRecord::Base
attr_accessible :picture
has_attached_file :picture, :styles => { :medium => "300x300>", :thumb => "100x100>" }
attr_accessor :picture_file_name
attr_accessor :picture_content_type
attr_accessor :picture_file_size
attr_accessor :picture_updated_at
My view:
<%= form_for #deck,:url => decks_path, :html => { :multipart => true } do |f| %>
<%= f.file_field :slika %>
My migration:
class AddAttachmentPictureToDecks < ActiveRecord::Migration
def change
add_column :decks, :picture, :attachment
end
end
So i get the picture in that folder that i have mentioned before but the picture column in my decks table is empty. I can't get the picture with <%= image_tag #deck.picture.url(:medium) %>, cause my #deck.picture.url holds a /pictures/original/missing.png, my #deck.picture.path and my #deck.picture.picture_file_name also shows nothing.
Thank you.
in the end it looks like i mixed the old syntax and the new one and i couldn't get the fields right in the database. and it looks like the attr_accessors are unneccessary

Ruby rails - drop down size properties

I am trying to give size properties to a drop down and select a default value. Doesn't seem to work, on the html I see length="35" but it has no effect on the actual width. How do I do this?
#roles = ['admin','user']
collection_select(:user, :title, roles.all, :id, :name, :value => "user", {:length => 35})
collection_select(:user, :title, roles.all, :id, :name, :value => "user", {:width => 35})
You should use :style => "width:200px" instead of :length.
Or much better, to use a css class for that selection box.

RanSack complex object relationships not building out... (method not found error)

First come caveats. I'm a total RoR n00b but i have experience with programming so i get the basic's. I've got an application i'm building which i need to build a complex search engine for. The basic layout is Guides >> Profiles >> Mentorings >> MentorAreas. Below is the code for each of the models and then the code i'm trying to build. My issue is i can't seem to figure out the proper object name to get the search engine to search mentor_areas.
System Setup:
rails -v :: Rails 3.1.1
ruby -v :: ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]
RanSack :: ransack (0.5.8) from git://github.com/ernie/ransack.git (at master)
Guide:
class Guide < User
end
User: (what's relevant)
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Virtual attribute for authenticating by either username or email
# This is in addition to a real persisted field like 'username'
attr_accessor :login
# Setup accessible (or protected) attributes for your model
attr_accessible :login, :username, :email, :password, :password_confirmation, :remember_me, :sex,
:location, :role, :first_name, :last_name, :home_town, :profile_attributes
has_one :profile, :dependent => :destroy
accepts_nested_attributes_for :profile, :allow_destroy => true
has_and_belongs_to_many :roles
end
Profile
class Profile < ActiveRecord::Base
belongs_to :user
has_many :mentor_areas, :through => :mentorings
has_many :mentorings
accepts_nested_attributes_for :mentor_areas,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }, :allow_destroy => true
validates_uniqueness_of :user_id
end
Mentoring
class Mentoring < ActiveRecord::Base
belongs_to :mentor_area
belongs_to :profile
validates_uniqueness_of :profile_id, :scope => :mentor_area_id
end
MentorArea
class MentorArea < ActiveRecord::Base
has_many :profiles, :through => :mentorings
has_many :mentorings
validates_uniqueness_of :mentor_area
end
In my Guides Controller i have:
#search_guides = Guide.joins(:roles).where("sex = :sex AND roles.name = :role",{:sex => current_user.sex, :role => 'guide'}).search(params[:search])
#guides_found = #search_guides.all
and in my view (index.html.erb) i have the following:
<%= form_for #search_guides do |f| %>
<%= f.label :username_cont %>
<%= f.text_field :username_cont %><br />
<%= f.label :guides_profiles_mentor_areas_mentor_area_cont %>
<%= f.text_field :guides_profiles_mentor_areas_mentor_area_cont %><br />
<%= f.submit %>
<% end %>
I can't seem to figure out what the correct name should be for the second field so that it will search against the mentor_areas that a person has associated with there profile.
thanks in advance!
Updated for RanSack Code
If I'm reading your code correctly, you want:
:profile_mentor_areas_mentor_area_cont
I think it's:
:profiles_mentorings_mentor_area_mentor_area_contains
Basically, it's you have to think about which tables you have to reach through one by one in sequence to get to your search field. I think in your case, your view for guides needs to go through : profiles -> mentoring -> mentor_area in order to search for the mentor area? Your mentor_area only have profiles :through the mentoring model. You can't go directly to the mentor_are without going through the mentoring model first.
Also,
<%= f.label :guides_profiles_mentor_areas_mentor_area_contains %>
looks really cumbersome in the view. You can just say this instead:
<%= f.label "Mentor Area" %>
Hope that works.

Resources