更新方法不使用rails 4更新表

时间:2013-08-13 21:48:33

标签: ruby-on-rails

我很难将Headfirst翻译成Rails书,这本书基于Rails 2进入我的机器上设置的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)

0 个答案:

没有答案
相关问题