button_to没有通过正确的ID从PARAMS哈希 [英] button_to not passing correct id from params hash
问题描述
该button_to没有传递正确的ID给LINE_ITEM。在日志下面你看到正确的'86'到不正确的bike_id改变'1'(巧合的是我的USER_ID)。任何帮助将是AP preciated。下面是从我的development.log错误,然后从我的观点和控制器的code。谢谢你。
development.log
在启动POST/ line_items?bike_id = 86为127.0.0.1 2011-08-01 18点09分52秒-0400
DE preCATION警告:ActionController的设置filter_parameter_logging是德precated并且不再生效,请设置config.filter_parameters中的config / application.rb中代替。 (称为来自以下;等级:ApplicationController中>对于/Users/willdennis/rails_projects/spinlister/app/controllers/application_controller.rb:8)
处理由LineItemsController#创建为HTML
参数:{authenticity_token=>中5GYQqvf7U5awhLrZ9Aw910ETf2kqOk3PI315jkjEfMU =,bike_id=>86}
[1分[35mCart负荷(0.6ms)0米选择大车。* FROM大车WHERE(大车。ID= 8)LIMIT 1
[1分[36mBike负荷(1.2ms的)[0分[1mSELECT自行车。* FROM自行车WHERE(自行车。ID= 86)ORDER BY bikes.created_at DESC LIMIT 1 [0米
[1分[35mSQL(0.5毫秒)[0米INSERT INTOline_items(bike_id,cart_id,created_at,的updated_at)值(1,8,'2011-08-01 22:09:53.208978' '2011-08-01 22:09:53.208978')
[1分[36mCart负荷(1.5毫秒)[0分[1mSELECT大车。* FROM大车WHERE(大车。ID= 8)LIMIT 1 [0米
重定向到的http://本地主机:3000 /车/ 8
完成302 251ms找到
line_items_controller.rb
DEF创建
@cart = current_cart
@bike = Bike.find(PARAMS [:bike_id])
@line_item = @ cart.line_items.build(:bike_id => @bike)
做的respond_to |格式|
如果@ line_item.save
format.html {redirect_to时(@ line_item.cart,
:通知=> 订单项已成功创建。')}
format.xml {渲染:XML => @line_item,
:状态=> :创建:位置=> @line_item}
其他
format.html {渲染:行动=> 新}
format.xml {渲染:XML => @ line_item.errors,
:状态=> :unprocessable_entity}
结束
结束
结束
的意见/自行车/表演
<%= button_to租自行车这样!,line_items_path(:bike_id => @bike){:ID => rentthisbike}%GT;
bike.rb
类车和LT; ActiveRecord的::基地
belongs_to的:用户
的has_many:line_items
attr_accessible:姓名,:描述:尺寸:biketype,:价格:照片,:ID,:地址:城市:状态:ZIP,:纬度,:经度,:居委会,:bike_id
结束
line_item.rb
类的LineItem< ActiveRecord的::基地
belongs_to的:自行车
belongs_to的:车 accepts_nested_attributes_for:自行车:车 attr_accessible:bike_id,:自行车:车,:姓名,:描述:尺寸:biketype,:价格:照片,:ID,:地址:城市:状态:ZIP,:纬度,:经度,:邻里结束
cart.rb
类车< ActiveRecord的::基地
的has_many:line_items,:依赖=> :破坏
belongs_to的:用户 accepts_nested_attributes_for:line_items attr_accessible:bike_id,:line_items,:姓名,:描述:尺寸:biketype,:价格:照片,:ID,:地址:城市:状态:ZIP,:纬度,:经度,:居委会
结束
你可以试试这个code和发布@@@@线项目与params哈希表一起进入属性从您的日志文件
我想这可能与你的current_cart方法,但我不知道
line_items_controller.rb
DEF创建
@bike = Bike.find(PARAMS [:bike_id])
@line_item = current_cart.line_items.build
@ line_item.bike = @bike
logger.debug(@@@@行项属性= #{@line_item.inspect})
做的respond_to |格式|
如果@ line_item.save
format.html {redirect_to时(@ line_item.cart,
:通知=> 订单项已成功创建。')}
format.xml {渲染:XML => @line_item,
:状态=> :创建:位置=> @line_item}
其他
format.html {渲染:行动=> 新}
format.xml {渲染:XML => @ line_item.errors,
:状态=> :unprocessable_entity}
结束
结束
结束
更新。
您previous code,不同的是这条线细
@line_item = @ cart.line_items.build(:bike_id => @bike)
您作为自行车ID而不是自行车的ID值提供了全班同学。我知道这是不符合形式传递参数,但是这只是事情是这样的。
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
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
def create
@bike = Bike.find(params[:bike_id])
@line_item = current_cart.line_items.build
@line_item.bike = @bike
logger.debug("@@@@ Line item attributes = #{@line_item.inspect}")
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
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.
这篇关于button_to没有通过正确的ID从PARAMS哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!