ZebThan
ZebThan

Reputation: 275

Pre filled, partially not editable input field with simple form?

Is it possible to create an input field, that is partially filled, but the filled part is not editable, and also, is not being submitted? It may seem confusing, so a simple example.
I have a field, that looks like url. I want the user to specify, let's say, his name, so the field should look like this:

www.something.com/users/ input

User should not be able to edit the bold part, only the "input" part. Also, I want to submit only the input, not the whole url. So basically I need something like this

<%= f.input :name, :not_editable_decoration: "www.something.com/users/" %>

Upvotes: 1

Views: 780

Answers (2)

Richard Peck
Richard Peck

Reputation: 76784

You wouldn't be able to use the show action for users but you'd be able to use a wildcard route:

#config/routes.rb
resources :users, except: :show do
   get "*input", to: :action, on: :collection #-> url.com/users/*input
end

This will pass a params[:input] variable to your action/view, which you'll be able to use in the form itself:

<%= f.input :name, :not_editable_decoration: "www.something.com/users/", value: params[:input] %>

Upvotes: 1

Pardeep Dhingra
Pardeep Dhingra

Reputation: 3946

You can try something like this:

<div class="form-group">
    <div class="input-group">
      <span class="input-group-addon">www.something.com/users/</span>
      <%= f.input :url, input_html: { class: 'form-control' } %>
    </div>
</div>

The above example use bootstrap css.

You can put static url into some instance variable too in your controller.

@static_url = "www.something.com/users/"

<span class="input-group-addon"><%= @static_url %></span>

link to Fiddle

Upvotes: 1

Related Questions