【问题标题】:Updating Model Attributes Through Show Page通过显示页面更新模型属性
【发布时间】:2011-10-24 08:44:05
【问题描述】:

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

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

carts/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) 在 431 毫秒内完成 200 次 OK(查看次数:399.6 毫秒 | ActiveRecord:6.7 毫秒)

   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)

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 这里不用:method
    • 非常感谢。这很好地更新了页面。你知道我如何保存日历 jQuery 下拉菜单(我相信它是由text_field_tag 触发的)吗?
    • 元素看起来还是一样的,只是ID不同。只需调整 jQuery 选择器,它应该可以工作。
    猜你喜欢
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 2021-07-14
    • 2017-08-08
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多