WQ.Kevin
WQ.Kevin

Reputation: 319

Ruby Titleize Simple Form input

My user gives his university information through simple form as a parameter and sends it to the server.

<div class="col-xs-12 col-s-4 col-sm-4">
  <%= simple_fields_for :users do |r| %>
    <%= r.input :university, required: false, placeholder: "University", label: false %>
  <% end %>
</div>

How can I titleize the param input the user gives me?

I tried

<%= r.input :university.titleize, required: false, placeholder: "University", label: false %>

but it's not working.

Upvotes: 1

Views: 263

Answers (3)

army_coding
army_coding

Reputation: 154

You can also try this way in your controller:

user_params[:university] = user_params[:university].titleize
@user = User.create!(user_params)

Upvotes: -1

Sebasti&#225;n Palma
Sebasti&#225;n Palma

Reputation: 33420

The first parameter on the input receives the user attribute for which to generate the input, so, it's not the place to chain the titleize method.

What you can do is to modify the value sent, before saving the record, or any action you might perform. As an example, if you're sending the data to a create method, then you can modify it after creating the user, and saving it into the database, like:

def create
  @user = User.new(user_params)
  @user.university = @user.university.titleize
  ...

Upvotes: 2

hoffm
hoffm

Reputation: 2436

If you are trying to titleize the text on the client after the user types it, you're going to need to write a javascript handler to manipulate the DOM on some event trigger (maybe on blur?).

On the other hand, if you are trying to titleize the text before you store it in your database, you'll want to do that on the server. Convert the user-inputted text to the titelized version before assigning it the record you're updating.

Upvotes: 1

Related Questions