我很难将 Headfirst 翻译成基于 Rails 2 的 Rails 书到在我的机器上设置的 Rails 4。该练习试图从数据库中检索记录,然后在编辑后更新表。我尝试了很多不同的方法,我当前的配置如下 - 这将显示记录以供编辑,但实际上不会更新记录!(真气!)注意——我没有使用“strong_parameters”或“protected_attributes”——我确实尝试过这个,但是堆栈太深和其他错误。我的广告模型也是空的 - 我已经删除了我尝试使用受保护属性 gem 的 att_accessible 条目。
这是我的 - 广告控制器条目:
def update
@ad = Ad.find(params[:id])
@ad.update_attributes(update_params[:id])
redirect_to "/ads/#{@ad.id}"
end
private
def update_params
params.permit(:name, :description, :price, :seller_id, :email, :ad, :img_url)
end
end
编辑视图:
<h1>Editing <%= @ad.name %></h1>
<%= form_for(@ad,:url=>{:action=>'update'}) do |f| %>
<p><b>Name</b><br /><%= f.text_field :name %></p>
<p><b>Description</b><br /><%= f.text_area :description %></p>
<p><b>Price</b><br /><%= f.text_field :price %></p>
<p><b>Seller</b><br /><%= f.text_field :seller_id %></p>
<p><b>Email</b><br /><%= f.text_field :email %></p>
<p><b>Img Url</b><br /><%= f.text_field :img_url %></p>
<p><%= f.submit "update" %></p>
<% end %>
更新日志条目:
Started PATCH "/ads/1/update" for 127.0.0.1 at 2013-08-13 22:35:51 +0100
Processing by AdsController#update as HTML
Parameters: {"utf8"=>"V", "authenticity_token"=>"O4cN72FXCCkfkL7M6bozodbB0Aqor
NctOOV5sfXdado=", "ad"=>{"name"=>"Typewriter", "description"=>"Old manual typewr
iter. Many years useful service. Works best with a bottle next to it.", "price"=
>"71.95", "seller_id"=>"54", "email"=>"dhammett@email.com1", "img_url"=>"http://
homepage.mac.com/david_griffiths/typewriter.png"}, "commit"=>"update", "id"=>"1"
}
←[1m←[35mAd Load (1.0ms)←[0m SELECT "ads".* FROM "ads" WHERE "ads"."id" = ? L
IMIT 1 [["id", "1"]]
Unpermitted parameters: utf8, _method, authenticity_token, ad, commit, id
←[1m←[36m (0.0ms)←[0m ←[1mbegin transaction←[0m
←[1m←[35m (0.0ms)←[0m commit transaction
Redirected to http://localhost:3000/ads/1
Completed 302 Found in 54ms (ActiveRecord: 1.0ms)