我的表单出现了问题,我在帮助下解决了第一个问题,但现在我遇到了新的错误,说我有一个未定义的方法。还在学习RoR所以如果这听起来很蠢,请耐心等待,感谢先进的帮助。
以下是代码:
<h1>Create New Contact</h1>
<% form_for :contact, :url => { :action => 'create' } do |f| %>
<p><label for="contact_first_name">First Name:</label>
<%= f.text_field 'contact', 'first_name' %></p>
<p><label for="contact_last_name">Last Name:</label>
<%= f.text_field 'contact', 'last_name' %></p>
<p><label for="contact_address">Address:</label>
<%= f.text_field 'contact', 'address' %></p>
<p><label for="contact_city">City:</label>
<%= f.text_field 'contact', 'City' %></p>
<p><label for="contact_state">State:</label>
<%= f.text_field 'contact', 'state' %></p>
<p><label for="contact_country">Country:</label>
<%= f.text_field 'contact', 'country' %></p>
<p><label for="contact_phone">Phone:</label>
<%= f.text_field 'contact', 'phone' %></p>
<p><label for="contact_email">Email:</label>
<%= f.text_field 'contact', 'email' %></p>
<%= f.submit "Create" %>
<% end %>
<p><%= link_to 'Back', { :action => 'index' } %></p>
这是我得到的错误,
NoMethodError in Contact#new
Showing C:/rails_projects/contactlist/app/views/contact/new.html.erb where line #4 raised:
undefined method `merge' for "first_name":String
Extracted source (around line #4):
1: <h1>Create New Contact</h1>
2: <% form_for :contact, :url => { :action => 'create' } do |f| %>
3: <p><label for="first_name">First Name:</label>
4: <%= f.text_field 'contact', 'first_name' %></p>
5:
6: <p><label for="contact_last_name">Last Name:</label>
7: <%= f.text_field 'contact', 'last_name' %></p>
Rails.root: C:/rails_projects/contactlist
Application Trace | Framework Trace | Full Trace
app/views/contact/new.html.erb:4:in `block in _app_views_contact_new_html_erb___976580590_43916136'
app/views/contact/new.html.erb:2:in `_app_views_contact_new_html_erb___976580590_43916136'
Request
Parameters:
None
Show session dump
Show env dump
Response
Headers:
None
答案 0 :(得分:0)
你text_field应该只是
<%= f.text_field 'first_name' %></p>
而不是
<%= f.text_field 'contact', 'first_name' %></p>
第二个参数应该是一个哈希。