我正在处理this Epicodus lesson 并且我刚刚为新视图和编辑视图重构了我的表单,但是新的&编辑操作无效。提交更新时,我收到“名称不能为空”错误。
这是我的新作,创作,编辑和播放更新我的contacts_controller中的操作:
def new
@contact = Contact.new
end
def create
@contact = Contact.new name: params[:name],
email: params[:email],
phone: params[:phone]
if @contact.save
flash[:notice] = "#{@contact.name} added."
redirect_to contacts_path
else
render 'new'
end
end
def edit
@contact = Contact.find(params[:id])
end
def update
@contact = Contact.find(params[:id])
if @contact.update name: params[:name],
email: params[:email],
phone: params[:phone]
flash[:notice] = 'Contact updated.'
redirect_to contact_path(@contact)
else
render 'edit'
end
end
我的编辑视图:
<% content_for(:title, "Edit #{@contact.name} | Wikipages") %>
<h1>Edit <%= @contact.name %></h1>
<%= render 'errors'%>
<%= render 'form'%>
<p><%= link_to 'Return to contacts', '/contacts' %></p>
我的新观点:
<% content_for(:title, "New contact | Wikipages") %>
<h1>New contact</h1>
<%= render 'errors'%>
<%= render 'form'%>
<p><%= link_to 'Return to contacts', '/contacts', class: 'btn btn-default' %></p>
我的表格部分:
<%= form_for(@contact) do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="form-group">
<%= f.label :phone %>
<%= f.text_field :phone %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<%= f.submit(class: "btn btn-primary")%>
<% end %>
我的路线:
Rails.application.routes.draw do
root 'contacts#index'
resources :contacts do
resources :phones
end
end
以下是我的日志输出:
Started POST "/contacts" for 127.0.0.1 at 2014-09-23 07:00:34 -0400
Processing by ContactsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"q59xzpBWXEs58qStmPcnpEqm89finUQKiUvYsjRYm8Q=", "contact"=>{"name"=>"Lance", "phone"=>"", "email"=>""}, "commit"=>"Create Contact"}
(2.1ms) BEGIN
(0.2ms) ROLLBACK
Rendered contacts/_errors.html.erb (0.6ms)
Rendered contacts/_form.html.erb (3.2ms)
Rendered contacts/new.html.erb within layouts/application (6.0ms)
Completed 200 OK in 214ms (Views: 207.5ms | ActiveRecord: 2.3ms)
以下是编辑日志的输出:
Started PATCH "/contacts/12" for 127.0.0.1 at 2014-09-23 06:48:02 -0400
Processing by ContactsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"q59xzpBWXEs58qStmPcnpEqm89finUQKiUvYsjRYm8Q=", "contact"=>{"name"=>"Chuck Wight", "phone"=>"435345345e", "email"=>"desfwrf@er.com"}, "commit"=>"Update Contact", "id"=>"12"}
Contact Load (0.3ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" = $1 LIMIT 1 [["id", 12]]
(0.1ms) BEGIN
(0.4ms) ROLLBACK
Rendered contacts/_errors.html.erb (0.5ms)
Rendered contacts/_form.html.erb (4.4ms)
Rendered contacts/edit.html.erb within layouts/application (7.2ms)
Completed 200 OK in 208ms (Views: 201.0ms | ActiveRecord: 0.8ms)
有什么想法吗?
答案 0 :(得分:1)
您的params嵌套在联系中,您正在将params直接传递给您的创建和更新操作。因此,请尝试paarams[:contact][:email]
而不是params[:email]
像这样:
def create
@contact = Contact.new name: params[:contact][:name],
email: params[:contact][:email],
phone: params[:contact][:phone]
if @contact.save
flash[:notice] = "#{@contact.name} added."
redirect_to contacts_path
else
render 'new'
end
end
def update
@contact = Contact.find(params[:id])
if @contact.update name: params[:contact][:name],
email: params[:contact][:email],
phone: params[:contact][:phone]
flash[:notice] = 'Contact updated.'
redirect_to contact_path(@contact)
else
render 'edit'
end
end
显然,在您的更新操作中,您的@contact
为零。因此,请尝试在控制器操作中进行更改。希望这能帮助你。