通过显示页面更新模型属性

时间:2011-08-10 18:38:10

标签: ruby-on-rails forms model update-attributes

我希望允许用户通过模型的显示页面更新模型的属性,然后提交,以显示更新的属性。 :startdate:enddate位于attr_accesible模型的cart.rb列表中。基于日志,它看起来像是在结束日期和开始日期,但没有设置它。有什么想法吗?非常感谢。

carts_controller.rb

def show
  @cart = Cart.find(params[:id])
end

def update
@cart = Cart.find(params[:id])
 respond_to do |format|
    if @cart.update_attributes(params[:cart])
      format.html { redirect_to(@cart, :notice => 'Dates Set.') }
      format.xml  { head :ok }
    end
  end
end

推车/ show.html.erb

    <%= form_for @cart do %>
       From <%= text_field_tag :startdate %> </br>
       To <%= text_field_tag :enddate %>
       <%= submit_tag "Set Dates", :method => :put %>
    <% end %> 

    Rental Dates: <%= @cart.startdate %> <%= @cart.enddate %>

development.log

Started POST "/carts/42" for 127.0.0.1 at 2011-08-10 14:27:10 -0400
DEPRECATION WARNING: Setting filter_parameter_logging in ActionController 
is deprecated and has no longer effect, please set 'config.filter_parameters' 
in config/application.rb instead. (called from <class:ApplicationController> at
/Users/willdennis/rails_projects/spinlister/app/controllers/application_controller.rb:8)
Processing by CartsController#update as HTML
Parameters: {"utf8"=>"✓",   
"authenticity_token"=>"2RC9jfvbdUWtlT3OWnVd1OhW7WigUPYVoS5Quuwv2hQ=", 
"startdate"=>"08/10/2011", "enddate"=>"08/18/2011", "commit"=>"Set Dates", "id"=>"42"}
 [1m[35mCart Load (1.1ms)[0m  SELECT "carts".* FROM "carts" WHERE ("carts"."id" = 42)   
 LIMIT 1 Redirected to http://localhost:3000/carts/42 Completed 302 Found in 82ms


  Started GET "/carts/42" for 127.0.0.1 at 2011-08-10 14:27:10 -0400
  DEPRECATION WARNING: Setting filter_parameter_logging in ActionController is       
  deprecated and has no longer effect, please set 'config.filter_parameters' in  
  config/application.rb instead. (called from <class:ApplicationController> at 
/Users/willdennis/rails_projects/spinlister/app/controllers/application_controller.rb:8)
   Processing by CartsController#show as HTML
   Parameters: {"id"=>"42"}
   [1m[36mCart Load (1.3ms)[0m  [1mSELECT "carts".* FROM "carts" WHERE ("carts"."id" = 42) LIMIT 1[0m
   [1m[35mLineItem Load (0.4ms)[0m  SELECT "line_items".* FROM "line_items" WHERE ("line_items".cart_id = 42)
   [1m[36mBike Load (1.6ms)[0m  [1mSELECT "bikes".* FROM "bikes" WHERE ("bikes"."id" = 86) ORDER BY bikes.created_at DESC LIMIT 1[0m

渲染布局/ _stylesheets.html.erb(1.5ms)        [1m [35mUser Load(2.4ms)[0m SELECT“users”。* FROM“users”WHERE(“users”。“id”= 1)LIMIT 1        渲染布局/ _header.html.erb(116.3ms)        渲染布局/ _footer.html.erb(1.3ms)        在布局/应用程序中渲染了carts / show.html.erb(398.2ms)        在431ms完成200 OK(浏览次数:399.6ms | ActiveRecord:6.7ms)

   Started GET "/stylesheets/application.css" for 127.0.0.1 at 2011-08-10 14:27:11 -0400

   ActionController::RoutingError (No route matches "/stylesheets/application.css"):


   Rendered /Users/willdennis/.rvm/gems/ruby-1.9.2-p180@rails3tut/gems/actionpack-  3.0.1/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.3ms)

1 个答案:

答案 0 :(得分:3)

问题是您没有正确使用form_for。这就是表单的生成方式:

<%= form_for @cart do |f| %>
  From <%= f.text_field :startdate %> <br>
  To <%= f.text_field :enddate %>
  <%= f.submit "Set Dates" %>
<% end %>