简体   繁体   中英

Rails form_for(form_tag) change id issue

This is the original HTML code:

<form id="login" action="/barbie/main">
    <h1>Log In</h1>
    <fieldset id="inputs">

        <input id="username" type="text" placeholder="Username" autofocus required>
        <input id="password" type="password" placeholder="Password" required>
    </fieldset>
    <fieldset id="actions">
        <input type="submit" id="submit" value="Log in">
        <a href="/barbie/register">Register</a>
    </fieldset>


</form>

I'm trying to change this to Rails form code. I googled and found that I have to change it like this:

<%= form_for(:model), :html=> {:id => 'login'} do |form| %>
<p>
  <%=form.label :input%>
  <%=form.text_field :input, :placeholder => 'Enter text here...'%>
</p>
<p>
  <%=form.label 'condiment'%>:
  <%=form.check_box :ketchup%>
</p>
<%end%>

If I run this, I get an error:

C:/Users/Hyunjae.Park/rubyworkspace/Barbie_WebUI/app/views/barbie/login.html.erb:20: syntax error, unexpected tASSOC, expecting keyword_end
...end=  form_for(:model), :html=> {:id => 'custom_form_id'} do...
...                               ^
C:/Users/Hyunjae.Park/rubyworkspace/Barbie_WebUI/app/views/barbie/login.html.erb:20: syntax error, unexpected keyword_do_block, expecting keyword_end
...=> {:id => 'custom_form_id'} do |form| @output_buffer.safe_c...
...                               ^
C:/Users/Hyunjae.Park/rubyworkspace/Barbie_WebUI/app/views/barbie/login.html.erb:30: syntax error, unexpected keyword_ensure, expecting $end
Extracted source (around line #20):

17: </form>
18: -->
19:
20: <%= form_for(:model), :html=> {:id => 'custom_form_id'} do |form| %>
21: <p>
22:   <%=form.label :input%>
23:   <%=form.text_field :input, :placeholder => 'Enter text here...'%>

Also I don't understand what this :model or @user kind of thing is in Rails.

The parentheses are wrong in this line:

<%= form_for(:model), :html=> {:id => 'login'} do |form| %>

:html is also an argument, as well as :model , so they should both be included in the same parentheses, or use no parentheses at all.

Either of the following will work:

<%= form_for(:model, :html=> {:id => 'login'}) do |form| %>

<%= form_for :model, :html=> {:id => 'login'} do |form| %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM