me_kalli
me_kalli

Reputation: 23

RoR form: adding text behind input-field

I am currently working on a registration form. Users should only register with an email-adress belonging to a specific domain (e.g. "@example.com"). I want to add this domain as a text behind the email-input-field to make it clear for the users what to enter. Unfortunately it seems impossible to write something behind an input-field as rails automatically does a line break.
This is the relevant part of the form:

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
    <%= devise_error_messages! %>
    <div class="field">
        <%= f.label :email %>
        <%= f.text_field :email, autofocus: true %>[@example.com]
    </div>
   
    <div class="actions">
        <%= f.submit "register", class: "button" %>
    </div>
<% end %>

The result should look like [ ] @example.com
Also I need the email-adress to be an actual email-adress. Therefore I also need to manipulate the input e.g. "john" to "[email protected]" before saving the user to the database.

How can I achieve these two things?

Upvotes: 0

Views: 56

Answers (1)

rmlockerd
rmlockerd

Reputation: 4136

The strict answer to your question is that in your controller action you are free to manipulate or set attributes on an instance before you save it.

def create
 @foo = Foo.new(foo_params)
 @foo.email = mangle_email(@foo.email)
 if @foo.save
   ... # normal stuff
 end
end

In your particular case, you should consider the various input scenarios (e.g., user adds the @domain on their own, etc.), since there are lots of cases where just appending something to the end of the user input is probably not what you're after.

Upvotes: 0

Related Questions