Button_to 未从参数哈希传递正确的 ID
Button_to 未将正确的 id 传递给 line_item。在下面的日志中,您会看到 bike_id 从正确的“86”更改为错误的“1”(巧合的是我的 user_id)。任何帮助将不胜感激。下面是我的development.log 中的错误,然后是我的视图和控制器中的代码。谢谢。
development.log
于 2011-08-01 18:09:52 -0400 开始发布 127.0.0.1 的“/line_items?bike_id=86”
弃用警告:在 ActionController 中设置 filter_parameter_logging 已被弃用并且不再有效,请在中设置 'config.filter_parameters' config/application.rb 代替。 (从 /Users/willdennis/rails_projects/spinlister/app/controllers/application_controller.rb:8 处的
由 LineItemsController#create as HTML 处理
参数: {"authenticity_token"=>"5GYQqvf7U5awhLrZ9Aw910ETf2kqOk3PI315jkjEfMU=", "bike_id"=>"86"}
[1m[35mCart 加载 (0.6ms)[0m 选择“车”。* FROM“车”在哪里("购物车"."id" = 8) 限制 1 [1m[36mBike 负载 (1.2ms)[0m [1mSELECT "bikes".* FROM "bikes" WHERE ("bikes"."id" = 86) ORDER BY bikes.created_at DESC LIMIT 1[0m [1m[35mSQL (0.5ms)[0m INSERT INTO "line_items" ("bike_id", "cart_id", "created_at", "updated_at") VALUES (1, 8, '2011-08-01 22:09:53.208978' , '2011-08-01 22:09:53.208978') [1m[36mCart 负载 (1.5ms)[0m [1mSELECT "carts".* FROM "carts" WHERE ("carts"."id" = 8) LIMIT 1[0m 重定向到 http://localhost:3000/carts/8 已完成 302 发现于 251 毫秒
line_items_controller.rb
def create
@cart = current_cart
@bike = Bike.find(params[:bike_id])
@line_item = @cart.line_items.build(:bike_id => @bike)
respond_to do |format|
if @line_item.save
format.html { redirect_to(@line_item.cart,
:notice => 'Line item was successfully created.') }
format.xml { render :xml => @line_item,
:status => :created, :location => @line_item }
else
format.html { render :action => "new" }
format.xml { render :xml => @line_item.errors,
:status => :unprocessable_entity }
end
end
end
views/bikes/show
<%= button_to "Rent this Bicycle!", line_items_path(:bike_id => @bike), {:id => "rentthisbike"} %>
bike.rb
class Bike < ActiveRecord::Base
belongs_to :user
has_many :line_items
attr_accessible :name, :description, :size, :biketype, :price, :photo, :id, :address, :city, :state, :zip, :latitude, :longitude, :neighborhood, :bike_id
end
line_item.rb
class LineItem < ActiveRecord::Base
belongs_to :bike
belongs_to :cart
accepts_nested_attributes_for :bike, :cart
attr_accessible :bike_id, :bike, :cart, :name, :description, :size, :biketype, :price, :photo, :id, :address, :city, :state, :zip, :latitude, :longitude, :neighborhood
end
购物车.rb
class Cart < ActiveRecord::Base
has_many :line_items, :dependent => :destroy
belongs_to :user
accepts_nested_attributes_for :line_items
attr_accessible :bike_id, :line_items, :name, :description, :size, :biketype, :price, :photo, :id, :address, :city, :state, :zip, :latitude, :longitude, :neighborhood
end
The button_to is not passing the correct id to the line_item. In the log below you see the bike_id change from the correct '86' to the incorrect '1' (which coincidentally is my user_id). Any help would be appreciated. Below is the error from my development.log, then the code from my view and controllers. Thanks.
development.log
Started POST "/line_items?bike_id=86" for 127.0.0.1 at 2011-08-01 18:09:52 -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 LineItemsController#create as HTML
Parameters: {"authenticity_token"=>"5GYQqvf7U5awhLrZ9Aw910ETf2kqOk3PI315jkjEfMU=", "bike_id"=>"86"}
[1m[35mCart Load (0.6ms)[0m SELECT "carts".* FROM "carts" WHERE ("carts"."id" = 8) LIMIT 1
[1m[36mBike Load (1.2ms)[0m [1mSELECT "bikes".* FROM "bikes" WHERE ("bikes"."id" = 86) ORDER BY bikes.created_at DESC LIMIT 1[0m
[1m[35mSQL (0.5ms)[0m INSERT INTO "line_items" ("bike_id", "cart_id", "created_at", "updated_at") VALUES (1, 8, '2011-08-01 22:09:53.208978', '2011-08-01 22:09:53.208978')
[1m[36mCart Load (1.5ms)[0m [1mSELECT "carts".* FROM "carts" WHERE ("carts"."id" = 8) LIMIT 1[0m
Redirected to http://localhost:3000/carts/8
Completed 302 Found in 251ms
line_items_controller.rb
def create
@cart = current_cart
@bike = Bike.find(params[:bike_id])
@line_item = @cart.line_items.build(:bike_id => @bike)
respond_to do |format|
if @line_item.save
format.html { redirect_to(@line_item.cart,
:notice => 'Line item was successfully created.') }
format.xml { render :xml => @line_item,
:status => :created, :location => @line_item }
else
format.html { render :action => "new" }
format.xml { render :xml => @line_item.errors,
:status => :unprocessable_entity }
end
end
end
views/bikes/show
<%= button_to "Rent this Bicycle!", line_items_path(:bike_id => @bike), {:id => "rentthisbike"} %>
bike.rb
class Bike < ActiveRecord::Base
belongs_to :user
has_many :line_items
attr_accessible :name, :description, :size, :biketype, :price, :photo, :id, :address, :city, :state, :zip, :latitude, :longitude, :neighborhood, :bike_id
end
line_item.rb
class LineItem < ActiveRecord::Base
belongs_to :bike
belongs_to :cart
accepts_nested_attributes_for :bike, :cart
attr_accessible :bike_id, :bike, :cart, :name, :description, :size, :biketype, :price, :photo, :id, :address, :city, :state, :zip, :latitude, :longitude, :neighborhood
end
cart.rb
class Cart < ActiveRecord::Base
has_many :line_items, :dependent => :destroy
belongs_to :user
accepts_nested_attributes_for :line_items
attr_accessible :bike_id, :line_items, :name, :description, :size, :biketype, :price, :photo, :id, :address, :city, :state, :zip, :latitude, :longitude, :neighborhood
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试此代码并从日志文件中发布@@@@行项目属性条目以及参数哈希吗
我认为这可能与您的 current_cart 方法有关,但我不确定
line_items_controller.rb
更新。
除了这一行之外,您之前的代码都很好
@line_item = @cart.line_items.build(:bike_id => @bike)
您提供整个类作为自行车 ID 的值,而不是自行车的 ID。我知道这与传递表单参数不一致,但事实就是如此。
Can you try this code and post the @@@@ Line item attributes entry from your log file along with the params hash
I think this may be related to your current_cart method but I'm not sure
line_items_controller.rb
Update.
Your previous code was fine except for this line
@line_item = @cart.line_items.build(:bike_id => @bike)
You were supplying the whole class as the value for the bike ID instead of the id of the bike. I know this is inconsistent with passing form parameters but that's just the way it is.