Rails ActiveAdmin - get current_admin_user in utility navigation? - ruby-on-rails-3

The ActiveAdmin docs say that I can override the utility navigation like this:
ActiveAdmin.setup do |config|
config.namespace :admin do |admin|
admin.build_menu :utility_navigation do |menu|
menu.add label: "ActiveAdmin.info", url: "http://www.activeadmin.info", html_options: { target: :blank }
admin.add_logout_button_to_menu menu # can also pass priority & html_options for link_to to use
end
end
end
It also says that the default display includes the current user's email address. How does it get the current user's email address? When I try replacing the menu.add label value with current_admin_user I get an error that says current_admin_user is undefined. How do I access info about the currently signed in user from the config file? How does ActiveAdmin do it behind the scenes?

config.namespace :admin do |admin|
admin.build_menu :utility_navigation do |menu|
menu.add :label => proc{ display_name current_active_admin_user },
:url => proc{ edit_admin_admin_user_path(current_active_admin_user) } ,#link_to current_active_admin_user,
:id => 'current_user',
:if => proc{ current_active_admin_user? }
admin.add_logout_button_to_menu menu
end
end

Just try the following:
ActiveAdmin.setup do |config|
config.namespace :admin do |admin|
admin.build_menu :utility_navigation do |menu|
menu.add label: "ActiveAdmin.info", url: "http://www.activeadmin.info",
html_options: { target: :blank }
admin.add_current_user_to_menu menu
admin.add_logout_button_to_menu menu
end
end
end

Related

Rails 3.2.13 Contact Form Only Sending Subject

So I followed this wonderfully flawed tutorial:
http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/
...on making contact forms. It works great. The only problem is that it ONLY sends the subject. I think maybe the problem is in the notifications mailer:
notifications_mailer.rb
class NotificationsMailer < ActionMailer::Base
default :from => "noreply#youdomain.dev"
default :to => "you#youremail.dev"
def new_message(message)
#message = message
mail(:subject => "[YourWebsite.tld] #{message.subject}")
end
end
I would, of course, like it to send ALL the info the user submitted... (name, email address, subject, and body.
Also I was wondering how I could do a simple version of this with just the body where the subject is set to a default. (I want to have a small comment box that would send an email to me with the comment.) Would I have to make a whole new controller and model for that, or could this handle both?
UPDATE
Notifications Mailer View / new.html.erb
Name: <%= #message.name %>
Email: <%= #message.email %>
Subject: <%= #message.subject %>
Body: <%= #message.body %>
contact controller
class ContactController < ApplicationController
def new
#message = Message.new
end
def create
#message = Message.new(params[:message])
if #message.valid?
NotificationsMailer.new_message(#message).deliver
flash[:success] = "Message was successfully sent."
redirect_to(root_path)
else
flash[:error] = "Please fill all fields."
render :new
end
end
end
message.rb
class Message
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :email, :subject, :body
validates :name, :email, :subject, :body, :presence => true
validates :email, :format => { :with => %r{.+#.+\..+} }, :allow_blank => true
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
Basically it works... but it only sends the subject. I also got it to send a complete mail once with everything BUT the subject... but I can't remember how I did it.
Should I just smash this computer into a million pieces and go on a rampage?
Sigh...
UPDATE AGAIN
This is what the emails say with the above settings:
Subject: [liquid.radio] Whatever The Subject is. Body: Completely
blank
This is what they said after whatever the hell I did two weeks ago.
Subject: Message from liquid.radio
Body:
A contact enquiry was made by Richard Pryor at 2013-06-17 23:36.
Reply-To: richard#pryor.com
Subject: Scared for no reason Body: Oh
no... Oh God no! What is that?!
All I did was mess around with the notifications controller. Although I don't remember... for the life of me... what I did. But, as you can see... it send the complete message as it should... but a completely different subject.
Really kinda need help here.
First, this project places production settings in config/application.rb. Move the GMail ActionMailer settings to config/environments/production.rb. Add the letter_opener gem to your Gemfile's development group.
# Gemfile
group :development do
gem 'letter_opener'
end
Add the letter_opener settings to your development environment.
# config/environments/development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :letter_opener
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
Add active_attr gem
# Gemfile
gem 'active_attr'
Make sure you run bundle install
Replace your messages model with robust ActiveAttr implementation.
# app/models/message.rb
class Message
include ActiveAttr::Model
attribute :name
attribute :email
attribute :subject
attribute :body
validates_presence_of :name, :email, :subject, :body
validates :email, :format => { :with => %r{.+#.+\..+} }, :allow_blank => true
end
Improve the routes.
# config/routes.rb
get 'contact' => 'contact#new', :as => 'contact'
post 'contact' => 'contact#create', :as => 'contact'
Make sure your email template is correct
# app/views/notifications_mailer/new_message.text.erb
Name: <%= #message.name %>
Email: <%= #message.email %>
Subject: <%= #message.subject %>
Body: <%= #message.body %>
Update: December 12, 2013
I've created a Rails 3.2 project on GitHub to assist anyone searching for a modern solution to creating contact forms in Rails. Merry Christmas!
https://github.com/scarver2/contact_form_app
Ultimately here's what I did... I realize this isn't a true solution. But I still don't have a different one.
notifications_mailer.rb
class NotificationsMailer < ActionMailer::Base
default :from => "example#gmail.com"
default :to => "example#gmail.com"
def new_message(message)
#message = message
mail(:subject => "[liquid.radio] #{message.subject}", :body => "
From: #{message.name}
Reply to: #{message.email}
Subject: #{message.subject}
Message: #{message.body}")
end
end
Not how the mailer is supposed to work at all... but at least it sends a complete message.
If you're in a time crunch... like I was... this will get you there. I will accept a real answer (probably scarver2's) once I stop getting blank emails any other way.

How to display only the value in edit page in Active Admin

I have a edit form in Active Admin. I need some field as read only.
My current edit page is like
I need the page look like this
How can this be done. My code for the edit form page is like
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.input :current_address, :label => 'Current Address', :as => :string
end
end
Please help.
As Alex said, set to disabled. You could then use css to get the visual you wanted, if you can live with the semantics of that.
The syntax was slightly different for me to get this to work.
in your admin form:
f.input :finish_position, input_html: { disabled: true }
in your CSS active_admin.css
input[disabled="disabled"],
input[disabled] {
background-color: #F4F4F4;
border: 0px solid #F4F4F4 !important;
}
For a cleaner form definition within your ActiveAdmin.register{} block you may as well want to define a "readonly" input type to be used within active admin using formtastic:
Form block syntax is for activeadmin version 1.0.0.pre at 0becbef0918a.
# app/admin/inputs/readonly_input.rb
class ReadonlyInput < Formtastic::Inputs::StringInput
def to_html
input_wrapping do
label_html <<
template.content_tag('div', #object.send(method))
end
end
end
# app/admin/your_model.rb
ActiveAdmin.register YourModel do
# ...
form do |f|
# ...
input :current_address, as: :readonly
# ...
end
end
I was facing the same issue and tried using :disabled but it did not solve my problem as I wanted field value to be included in params object while sending it to the server. When you mark a form input as :input_html => {:disabled => true} , it does not include this field value in params.
So, instead I used :input_html => {:readonly => true} which solved both of my problems:
Does not allow user to edit
Includes the value in params
I hope this will help.
How about this?
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.li do
f.label :current_address
f.span f.object.current_address
end
end
end
Try to add , :disabled => true for address input field.
The trick is to use "object". Here is how you should code it:
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.label :current_address, f.object.current_address
end
end

rails 3 absolute link to generated url

I am looking into generating urls which look like https://mywebsite.com/user/check_email_confirmation/46cee41bc2044104aff8cf7746687ea8
for the famous "please click this link to confirm your email".
So I have a route which I intend to use for this in my routes.rb:
resources :user do
collection do
get 'check_email_confirmation/:generated_url', :to => 'user#check_email_confirmation'
end
end
Now, my trouble is to generate the full address above. I cah generate the SHA (46cee41bc2044104aff8cf7746687ea8) part, but how do I write the "https://mywebsite.com/user/check_email_confirmation/" part in my view?
Added view's code:
<p>Please click <%= url_for(:action => 'check_email_confirmation', :controller => 'user', :only_path => false, :id => #confirmation_url) %> to confirm your e-mail address.</p>
Replace id: with generated_url:.
You could also simplify the url_foring with naming the route by as: in routes
get 'check_email_confirmation/:generated_url', to: 'user#check_email_confirmation', as: :confirmation
More on this at http://viget.com/extend/rails-named-routes-path-vs-url
get "check_email_confirmation/:id" => "users#check_email_confirmation", :as => "check_email_confirmation"

Unable to delete resource after overriding devise registrations controller

I have followed this devise how to: Redirect to a specific page on successful sign up.
I have created a new RegistrationsController
class RegistrationsController < Devise::RegistrationsController
def after_inactive_sign_up_path_for(resource)
...
end
def destroy
logger.debug 'destroy user'
...
end
end
I have changed routes.rb :
devise_for :users, :controllers => { :registrations => "registrations" } do
get 'users', :to => 'profile#index', :as => :user_root
end
and moved devise/registrations/ views under my new RegistrationsController.
With rake routes I have :
DELETE /users(.:format) {:action=>"destroy", :controller=>"registrations"}
after_inactive_sign_up_path_for is working.
But destroy action doesn't work : when I cancel my account
<%= button_to "Cancel my account", registration_path(resource_name), :confirm => "ok?", :method => :delete %>
I have the following error :
The action 'destroy' could not be found for RegistrationsController
I use Devise 1.4.5 & Rails 3.1
I just ran into the same issue. Moving the destroy method to the non private section of the controller fixed it.

Rails 3 Route: button_to HTML INPUT Action not working

I have a strange problem that I think has to do with routes.
In my "view" I have this:
<%= button_to "New Item", new_proposal_pitem_path(#proposal), :method => :get %>
I want to click the "New Item" button, and create a new pitem for a proposal. This generates the HTML I would expect:
<form method="get" action="/proposals/1234/pitems/new" ...><input ...></form>
However, what really happens is, when I click on the button it attempts a GET on /proposals/1234 rather than /proposals/1234/pitems/new. This gives me a "show" page rather than a "new" page. Interestingly, I can manually put the {site}/proposals/1234/pitems/new directly into the web browser HTTP address and get what I want (the "new" page). But rails is, on its own, translating it first to /proposals/1234 if I leave it to its own in response to the button click.
To make this more mysterious, I have a similar item on the same form which looks exactly the same way:
<%= button_to "New Payment", new_proposal_payment_path(#proposal), :method => :get %>
which generates the same HTML as the other case:
<form method="get" action="/proposals/1234/payments/new" ...><input ...></form>
But this one works! It takes me right to /proposals/1234/payments/new when I click the button, just like I'd expect. I just don't understand what makes these behave differently.
My full routes file looks like this:
TCoB::Application.routes.draw do
resources :proposals do
resources :pitems, :payments
get 'list', :on => :collection
end
resources :pitems do
get 'list', :on => :collection
end
resources :invoices do
resources :iitems, :payments
get 'list', :on => :collection
end
resources :iitems do
get 'list', :on => :collection
end
resources :payments do
get 'list', :on => :collection
end
resources :ids
resources :clients do
resources :proposals, :invoices
# Route GET /cients/list
get 'list', :on => :collection
get 'list_proposals', :on => :collection
get 'list_invoices', :on => :collection
end
get "home/index"
root :to => "home#index"
end
Can someone shed light on this issue?
Thanks!
Small helper method that uses JavaScript:
def button_link_to(name, url)
"<button type=\"button\" onclick=\"window.location.href='#{url}';\">#{h(name)}</button>".html_safe
end
button_link_to "New Item", new_proposal_pitem_path(#proposal)