Note that there are some explanatory texts on larger screens.

plurals
  1. POWant to form a "email id" with editable text field for "username" and a constant "domain name"
    text
    copied!<p>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 for username followed by @domain name(populated from database,population is happening). Here is my current view.</p> <pre><code>&lt;%= form_for @user do |f| %&gt; &lt;%= f.hidden_field :domain_id %&gt; &lt;%= f.label :email %&gt; &lt;%= f.text_field(:email, :size =&gt; 20) %&gt; &lt;%= f.submit "Create" %&gt; &lt;% end %&gt; </code></pre> <p>I am not being able to figure out how to do this. Can somebody help, please.</p> <p>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.<br/> [.............]@example.com<br/> 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.</p> <p>{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).</p> <pre><code>&lt;%= form_for @user do |f| %&gt; &lt;%= f.label :email %&gt; &lt;%= f.text_field(:email, :size =&gt; 20) %&gt; &lt;%= f.submit "Create" %&gt; &lt;% end %&gt; </code></pre> <p>Controller(question related portions only)</p> <pre><code>class UsersController &lt; 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 =&gt; @user.domain_id) else @title = "Sign up" redirect_to addNewUser_path(:id =&gt; @user.domain_id) end end end </code></pre> <p>Model(question related portion only)</p> <pre><code>class User &lt; ActiveRecord::Base attr_accessor :email before_create :compose_email def compose_email domain = @domain.domain_name self.email = "#{email}@#{domain}" end end </code></pre> <p>When i am trying with something like (for checking only)</p> <pre><code>domain = "example.com" </code></pre> <p>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 </p> <pre><code>domain = @domain.domain_name </code></pre> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload