Rails form_for(form_tag)更改id问题

时间:2013-11-05 02:28:09

标签: ruby-on-rails ruby

这是原始的HTML代码:

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

我正在尝试将其更改为Rails表单代码。我用Google搜索,发现我必须改变它:

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

如果我运行这个,我收到一个错误:

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...'%>

另外,我不明白Rails中这个:model@user类似的东西。

1 个答案:

答案 0 :(得分:4)

这一行的括号错了:

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

:html也是一个参数,以及:model,所以它们都应该包含在相同的括号中,或者根本不使用括号。

以下任何一种都可以使用:

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

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