0

这是rails表单帮助代码,

    <%= form_tag({ :action => "create"}, :method => "POST", :id=>"login") do  -%>

    <h1>Log In</h1>
    <fieldset id="inputs">
        <%= text_field :username, :placeholder => 'Username', :autofocus=>true%>

        <%= password_field_tag :userpass, params[:userpass], placeholder: 'Password', id:'password'%>


    </fieldset>
    <fieldset id="actions">
        <input type="submit" id="submit" value="Log in">
        <a href="/registration/index">Register</a>
    </fieldset>

    <% end %>

这是我想要的html代码,

<form action="/login/create" method="post" id="login" >
        <h1>LogIn</h1>
        <fieldset id="inputs">

        <input id="username" type="text" name ="username" placeholder="Username" autofocus required>

        <input id="password" type="password" name="userpass" placeholder="Password" required>
        </fieldset>
        <fieldset id="actions">
        <input type="submit" id="submit" value="Log in" name="apply">
        <a href="/registration/index">Register</a>
        </fieldset>

        </form>

但是,如果我运行 rails 代码,它会生成这样的代码,

<input id="username_{:placeholder=&gt;&quot;Username&quot;, :autofocus=&gt;true}" name="username[{:placeholder=&gt;&quot;Username&quot;, :autofocus=&gt;true}]" size="30" type="text">

有人知道问题是什么吗?

4

1 回答 1

0

您将希望text_field_tag在您的表单中使用,而不仅仅是text_field

<%= form_tag({ :action => "create"}, :method => "POST", :id=>"login") do  -%>

  <h1>Log In</h1>
  <fieldset id="inputs">
    <%= text_field_tag :username, params[:username], { :placeholder => 'Username', :autofocus => true } %>

    <%= password_field_tag :userpass, params[:userpass], placeholder: 'Password', id:'password'%>
  </fieldset>

  <fieldset id="actions">
    <input type="submit" id="submit" value="Log in">
      <a href="/registration/index">Register</a>
  </fieldset>

<% end %>

您还应该查看field_set_tag - 如果您希望使用 HTMLfieldset标记,这可能会为您省去一些麻烦。

于 2013-11-11T01:09:30.683 回答