Ruby on rails 4 在rails控制器中访问数据库信息
我在项目控制器中定义支付操作的金额时遇到问题。我正在尝试提取数据库中存储的物品价格。我什么都试过了。我重读了这篇文章,但仍然没有运气 在项目控制器中:Ruby on rails 4 在rails控制器中访问数据库信息,ruby-on-rails-4,model-view-controller,Ruby On Rails 4,Model View Controller,我在项目控制器中定义支付操作的金额时遇到问题。我正在尝试提取数据库中存储的物品价格。我什么都试过了。我重读了这篇文章,但仍然没有运气 在项目控制器中: def pay # Find the user to pay. user = User.find( params[:id] ) amount = #I have no idea what to put here fee = 100 如果我输入一个数字,就可以了。然而,当我试图访问存储在数据库中的物品价格时,事情就
def pay
# Find the user to pay.
user = User.find( params[:id] )
amount = #I have no idea what to put here
fee = 100
如果我输入一个数字,就可以了。然而,当我试图访问存储在数据库中的物品价格时,事情就爆发了
我尝试过各种定义:
@item.price #fail
item_price #fail
Item.find (params: [:price]) #fail
Item.price #fail
#EVERYTHING FAILS
模式:
ActiveRecord::Schema.define(version: 20150127171203) do
create_table "items", force: true do |t|
t.string "name"
t.decimal "price"
t.text "description"
t.integer "booth_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "category_id"
t.integer "user_id"
t.string "image"
t.string "image_two"
end
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.string "activation_digest"
t.boolean "activated", default: false
t.datetime "activated_at"
t.string "reset_digest"
t.datetime "reset_sent_at"
t.boolean "admin", default: false
t.string "avatar"
t.text "about"
t.string "location"
t.string "publishable_key"
t.string "secret_key"
t.string "stripe_user_id"
t.string "currency"
end
(编辑)我还尝试在支付行动中定义项目,但这也没有帮助。”item=item.find(参数[:id]'
有人知道这样做的正确方法吗?我觉得这应该很容易,但我有困难
感谢您的考虑和帮助!用户与项目的关系如何?我这样问是因为如果您使用的是active record,您在数据库中建立的关联将决定为您创建的方法。用户
在您的模型中是否有许多项目?项目是否属于用户?在这种情况下会是什么是
@user = User.find(1)
你可以找到这样的价格
@item = @user.items.find(1)
@price = @item.price
编辑
好的,我看了一下你提供的代码。这个例子显示了一个在用户控制器中定义路由的付费方法,它似乎只被设置为接受一次性费用。在上面的评论中,它甚至说进行一次性付款,所以我们知道这是真的
即使在这个例子中,他们也明确地说这是一个固定的金额。现在我们如何使它成为动态的?好吧,看看代码。工资控制器中有一些逻辑,不是吗
看到正在创建的Stripe::Charge对象了吗
charge = Stripe::Charge.create(
{
amount: amount,
currency: user.currency,
card: params[:token],
description: "Test Charge via Stripe Connect",
application_fee: fee
},
# Use the user-to-be-paid's access token
# to make the charge.
user.secret_key
)
您可以将此逻辑移动到您的项目模型中。只要它接受用户作为参数,就可以将其称为pay或其他名称
def pay(user)
amount = self.price
begin
charge = Stripe::Charge.create(
{
amount: amount,
currency: user.currency,
card: user.token,
description: "Test Charge via Stripe Connect",
application_fee: fee
},
# Use the user-to-be-paid's access token
# to make the charge.
user.secret_key
)
flash[:notice] = "Charged successfully! <a target='_blank' rel='connected-account' href='https://dashboard.stripe.com/test/payments/#{charge.id}'>View in dashboard »</a>"
rescue Stripe::CardError => e
error = e.json_body[:error][:message]
flash[:error] = "Charge failed! #{error}"
end
end
您得到的错误代码是什么?您的数据库中的items表中是否有price列?该项目属于该用户,并且该用户有许多项目(付款操作在items controller中)。我执行了与您建议的类似的操作,item=item.find(params[:id])amount=item.price,我得到了错误“参数数目错误(1代表0)你可以提供你的用户和项目迁移的代码以及它们的模型吗?或者这段代码在GitHub上的某个地方我可以看一下吗?是的,我现在就添加它。谢谢!在你的项目控制器中,你有创建项目记录的创建方法吗?在数据库中是否有为我们实际查找的项目种子?哦,顺便说一句,这是参数s[:id]可能只是语法错误。请确保params和[]之间没有空格
def pay
user = User.find(session[:user_id])
item = Item.find(params[:id])
item.pay(user)
redirect "somewhere"
end