In Active Admin Gem,how to modify the view of resource? - ruby-on-rails

I have implemented ActiveAdmin in my Rails project. I have a resource called "Customer". In the navigation bar, there is an option called "New Customer" to create a customer. How can I change the view of this new customer? When I click "New Customer" button, it has to accept email from me, and it should send an invitation to that email-id.

All you need to do is follow this
https://gist.github.com/mikeatlas/5628317
it has enough details to setup devise_invitable with active_admin.

Actually we can do it by, integrating ActiveAdmin and DeviseInvitable Gems. The following link illustrates that.
Here's a link!, In which they have explained neatly.

If your Customer model has a email field, then you can just allow only the email to be shown in the form, and override the 'create' method. Something like this:
form do |f|
f.input :email
f.actions
end
def create
#send the email here
end

Related

Contact Form for User Profile

I been struggling of thinking of a way to complete this function.
Currently I have a user with a profile. And a general Contact form model that is table-less and doesn't save anything to the database.
My goal is to have a general contact form, In which I can link a contact button on a individual user profile. That contact form when submitted will be sent to the user email specified in the profile attribute. So for example the profile has a field t.string contact_email.
Currently I have the contact model set up where it can send to one individual email. Mainly the app owner.
class ContactMailer < ApplicationMailer
default :to => "stephen#example.com"
def contact_me(msg)
#msg = msg
mail from: #msg.email, subject: #msg.subject, body: #msg.content
end
end
My goal is to simply link the
default :to => "stephen#example.com"
to something like
default :to => "#profile.contact_email"
I have no idea how to specify the user for the form or if its possible. The process would have to include once the visitor hits contact us the form takes the profile email and uses it as the recipient.
Sorry if this seems like I brung nothing to table to answering I'm just looking for a tip on maybe where to start or how it could be done.
Note: Don't get into this thing that you would have to specify to in default. You can simply send to in mail method like you are sending from, subject and other variables.
There are several ways to do it:
1) You can use hidden_field_tag to pass along the email of that user like following
<%= hidden_field_tag :contact_email, #user.contact_email %>
And then on server side, you can have its value through parmas[:contact_email].
2) You can send the email of the user when you can call contact_me. Right now, you are only sending one parameter msg. So,
def contact_me(msg, to)
#msg = msg
mail to: to, from: #msg.email, subject: #msg.subject, body: #msg.content
end
And when you call contact_me, you can do: ContactMailer.contact_me(msg, #user.contact_email) though it may be different depending upon the actual implementation in your code, but the concept would always be same.

How can I have the user only add their #name instead of entire Twitter link in a url_field?

In my Rails app I have a user model (Devise) with url_field for Twitter.
Right now I have this in my show page:
<%= link_to #deal.user.company_twitter, #deal.user.company_twitter, :class => "comp_twitter" %>
It shows the entire link in the show page (even http://), that's because my url_field makes the user add "http://" or it won't validate the link.
I want the user to only add their "#name" instead of the whole Twitter link when creating their profile. I would also want to show only the #name instead of the Twitter link in the "show" page. How can I do this?
Simply don't use an url_field, but a regular string database field, like twitter_username. When signing up, let the user enter their username, e.g. foo.
For getting the real URL to the Twitter account, create a new method in your user.rb model:
def twitter_url
"http://twitter.com/#{self.twitter_username}"
end
The advantage is that at this point, in your model, you can include a custom validation that would check if this URL really exists after the user has submitted their twitter_username.
Finally, use that in your view instead of just the URL:
<%= link_to "##{#user.twitter_username}", #user.twitter_url %>
This would render:
#foo

Rails: Devise: User

Environment: Rails 3.2.3 with Devise
I would like to add a registration code to the registration form. The registration code is NOT in the users table. There's a separate registration_codes for that. The reason it's in the registration form, is that I would like to compare the code entered by the user filling out the registration form to what's in the registration codes table, and produce a value that will go into the users table.
I am getting an error message:
undefined method `registration_code' for #<User:0x007f91b57b7890>
and it's pointing to the code that displays the registration_code text box in views/registrations/new.html.erb
How do I get around the fact that registration_code is not part of the users table? The form was working fine, until I added the registration_code field to the form.
I would create a method in your User model that handles your registration code logic:
def registration_code=(code)
# Check registation_codes table and produce a value that will go into the users table
end
Submitting a form with this new field should now call this method with the submitted value
I assume you're using a form builder to create the form (a form_for block). In this case, you're probably using something like <%= f.text_field :registration_code %> to display the text field, but Rails can't find registration_code on the User model, because, like you said, it's not there.
Instead, try using <%= text_field_tag :registration_code %>. You can access this from your controller through params[:registration_code]

Registration with Devise with only email field on page with other form

I want to give users posibility to add content not being registered but show 'registration form' above submit button on my 'content form'. How to do that using Devise gem? Or maybe I should use some other gems for this functionality?
Thanks.
Generated in standard view user login is default by email. If you wont show registeration form directly in place you want put render file with registeration form:
<%= render :file => 'users/registrations/new' %>
I propose to review the wiki: How To: Add sign_in, sign_out, and sign_up links to your layout template
and RailsCasts: Episode 210
You can register the user in the back-end (for default configuration):
user = User.new(params[:user])
if user.save!
#some logic, where user adds content
else
#some logic
If you want to register only with the email, you will need to generate a password, and for example, send it to the user email. See more there: https://github.com/plataformatec/devise/wiki/How-To:-Automatically-generate-password-for-users-(simpler-registration)/

Ruby on Rails: Confirmation Page for ActiveRecord Object Creation

Using Ruby on Rails I want a confirmation page before creating an ActiveRecord object. The user will see a preview of the item they are creating before submitting and the object being saved in the database
A common pattern;
User visits /entry/new
User enters details and clicks submit
User is redirected to /entry/confirm which displays the entry and clicks submit or edit to correct mistakes
Object is saved
How would you implement it?
Another option to solve this issue adding by a virtual confirmation attribute to your model. This way, there is no need to create a separate action for this:
class MyRecord < ActiveRecord::Base
attr_accessor :confirmation
validates_acceptance_of :confirmation, :on => :create
end
Now, your new object will not save correctly because the validation will fail on the confirmation field. You can detect this situation and present something like this:
<% form_for(#my_record) do |form| %>
...
<%= form.check_box :confirmation %> Really create this record.
<%= submit_tag('Confirm') %>
<% end %>
I would probably add a "preview" action to the routes.rb file for that model:
map.resource :objects, :new => { :preview => :post }
You would get to this preview action by POSTing the preview_object_url named route. You would need to essentially create the Object in the same way you would in your create action, like this:
def preview
#object = Object.new(params[:object])
end
This page would then POST to the create action, which would then create the Object. It's pretty straight forward.
http://api.rubyonrails.org/classes/ActionController/Resources.html
A few options
1- store the object you want to create in the session until you hit the confirm page, then just save it
2- pass around the object w/ each Post/submit from new -> details -> confirm
I would probably go with 2, since I am not prone to saving state with the session.
I'm not sure how to do this (RoR is new to me) but you could just specify the action for /new as /confirm, and then it calls create.
Right?

Resources