Want to form a "email id" with editable text field for "username" and a constant "domain name"
I am creating an application having domains and users. Admin can select any domain and create user inside that domain. In the new user creation view i have a email field. Currently i am creating new user by entering the full email id like "sam@exmple.com".I would like to divide the email id so that only the "username(sam)" part is need to be inserted using the text field. The "@domain" part is appended with the username while the form is being submitted.I wish to have a view in which there will be a email section with a text field f开发者_如何学编程or username followed by @domain name(populated from database,population is happening). Here is my current view.
<%= form_for @user do |f| %>
<%= f.hidden_field :domain_id %>
<%= f.label :email %>
<%= f.text_field(:email, :size => 20) %>
<%= f.submit "Create" %>
<% end %>
I am not being able to figure out how to do this. Can somebody help, please.
At present i am only having a text_field for my email section.So i need to write the full email id.But i wish to have something like this.
[.............]@example.com In the text_field(i mean the square braces) only username need to be inserted.The domain part is populated from domain table as per selection of domain.How can i prepare the email section of my view so that all the 3 items(username,@,domain name) are concatenated to form a complete email id at the time of submission.{update} Since i didn't want to add one more field to the table "users", i have not added "user_name" and trying to do with "email" only. Here is my view(question related portions only).
<%= form_for @user do |f| %>
<%= f.label :email %>
<%= f.text_field(:email, :size => 20) %>
<%= f.submit "Create" %>
<% end %>
Controller(question related portions only)
class UsersController < ApplicationController
def new
@domain = Domain.find(params[:id])
@user = @domain.users.build
@title = "Add User"
end
def create
@user = User.new(params[:user])
if @user.save
flash[:success] = "Welcome!"
redirect_to manageDomain2_path(:id => @user.domain_id)
else
@title = "Sign up"
redirect_to addNewUser_path(:id => @user.domain_id)
end
end
end
Model(question related portion only)
class User < ActiveRecord::Base
attr_accessor :email
before_create :compose_email
def compose_email
domain = @domain.domain_name
self.email = "#{email}@#{domain}"
end
end
When i am trying with something like (for checking only)
domain = "example.com"
Then the email id is getting saved properly.(like sam@example.com) As i have many domains, i want to build this domain part dynamically.so when i am using
domain = @domain.domain_name
Im getting error as "undefined method domain_name". I guess this is due to unavailability of the instance domain in my user model. Can you please suggest,where am i doing wrong. Is there any alternative way for this?
I don't understand this part of your post: "I wish to have a view in which there will be a email section with a text field for username followed by @domain name(populated from database,population is happening). Here is my current view".
Can you elaborate/reword this? :)
Aside from that here is an idea. Define:
attr_accessor :user_name
in your User model. Then you can write somethign like this in your form:
<%= f.text_field(:username, :size => 20) %>
And finally, define a callback in your model:
before_create :compose_email
def compose_email
domain = ... #prepare domain here
self.email = "#{user_name}#{domain}"
end
EDIT:
Ok, first, you do not have access to @domain in your model. It's defined in controller and available there and in the view.
Second, I see that you're using @domain.users.build
so I guess that Domain has_many :users
and User belongs_to :domain
, right?
If so, you can write your compose_email method like this:
def compose_email
domain = domain.domain_name #we are using AR association here and we are calling domain on self, which is instance of user here, by default
self.email = "#{email}@#{domain}"
end
精彩评论