预填充,部分不可编辑的简单表单输入字段?

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

是否可以创建一个部分填写的输入字段,但填写的部分不可编辑,也不会提交? 可能看起来很混乱,所以一个简单的例子。
我有一个字段,看起来像 url。我希望用户指定,比方说,他的名字,所以这个字段应该是这样的:

www.something.com/users/ 输入

用户不能编辑粗体部分,只能编辑 "input" 部分。另外,我只想提交输入,而不是整个 url。所以基本上我需要这样的东西

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

您可以尝试这样的操作:

<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>

上面的例子使用bootstrap css.

您也可以在控制器中将静态 url 放入某个实例变量中。

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

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

link to Fiddle

您将无法对 users 使用 show 操作,但您可以使用 wildcard route:

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

这会将一个 params[:input] 变量传递给您的 action/view,您将能够在表单本身中使用它:

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