Ruby on rails 如何从单独的控制器访问实例变量(签出中的金额变量)
我正在使用Stripe checkout进行工具租赁应用程序项目的签出过程。我遇到了一个问题,当我想根据发布工具的人想租什么来动态改变租金时 我的费用嵌套在我的工具中,如下所示:Ruby on rails 如何从单独的控制器访问实例变量(签出中的金额变量),ruby-on-rails,ruby,ruby-on-rails-4,model-view-controller,stripe-payments,Ruby On Rails,Ruby,Ruby On Rails 4,Model View Controller,Stripe Payments,我正在使用Stripe checkout进行工具租赁应用程序项目的签出过程。我遇到了一个问题,当我想根据发布工具的人想租什么来动态改变租金时 我的费用嵌套在我的工具中,如下所示: Rails.application.routes.draw do devise_for :users root 'pages#home' resources :tools do resources :charges get :manage, :on => :collection
Rails.application.routes.draw do
devise_for :users
root 'pages#home'
resources :tools do
resources :charges
get :manage, :on => :collection
end
当我导航到/tools/1/charges/new时,出现以下错误:
Couldn't find Tool with 'id'=
这是我的收费管理员:
class ChargesController < ApplicationController
def new
@tool = Tool.find(params[:id])
@amount = @tool.rent_price * 100
end
def create
@tool = Tool.find(params[:id])
@amount = @tool.rent_price * 100
customer = Stripe::Customer.create(
:email => 'example@stripe.com',
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
end
private
def tool_params
params.require(:tool).permit(:name, :description, :user_id, :tool_image, :rent_price)
end
end
class ToolsController < ApplicationController
before_action :set_tool, only:[:show, :edit, :update, :destroy]
before_action :authenticate_user!, only:[:new, :destroy, :edit, :manage], notice: 'you must be logged in to proceed'
def index
@tools = Tool.all
end
def manage
@user = current_user
@tools = @user.tools
end
def show
end
def new
@tool = Tool.new
end
def create
@tool = Tool.new(tool_params)
if @tool.save
redirect_to @tool
else
redirect_to :action => "new"
flash[:notice] = "You did not fill out all the fields"
end
end
def edit
end
def update
@tool.update(tool_params)
redirect_to @tool
end
def destroy
@tool.destroy
redirect_to tools_path
end
private
def set_tool
@tool = Tool.find(params[:id])
end
def tool_params
params.require(:tool).permit(:name, :description, :user_id, :tool_image, :rent_price)
end
end
class ChargesController'example@stripe.com',
:card=>params[:stripeToken]
)
charge=Stripe::charge.create(
:customer=>customer.id,
:amount=>@amount,
:description=>“Rails条带客户”,
:货币=>“美元”
)
救援条带::CardError=>e
闪光[:错误]=e.message
将\u重定向到\u路径
结束
私有的
def工具参数
参数require(:tool).permit(:name,:description,:user\u id,:tool\u image,:rent\u price)
结束
结束
这是我的工具控制器:
class ChargesController < ApplicationController
def new
@tool = Tool.find(params[:id])
@amount = @tool.rent_price * 100
end
def create
@tool = Tool.find(params[:id])
@amount = @tool.rent_price * 100
customer = Stripe::Customer.create(
:email => 'example@stripe.com',
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
end
private
def tool_params
params.require(:tool).permit(:name, :description, :user_id, :tool_image, :rent_price)
end
end
class ToolsController < ApplicationController
before_action :set_tool, only:[:show, :edit, :update, :destroy]
before_action :authenticate_user!, only:[:new, :destroy, :edit, :manage], notice: 'you must be logged in to proceed'
def index
@tools = Tool.all
end
def manage
@user = current_user
@tools = @user.tools
end
def show
end
def new
@tool = Tool.new
end
def create
@tool = Tool.new(tool_params)
if @tool.save
redirect_to @tool
else
redirect_to :action => "new"
flash[:notice] = "You did not fill out all the fields"
end
end
def edit
end
def update
@tool.update(tool_params)
redirect_to @tool
end
def destroy
@tool.destroy
redirect_to tools_path
end
private
def set_tool
@tool = Tool.find(params[:id])
end
def tool_params
params.require(:tool).permit(:name, :description, :user_id, :tool_image, :rent_price)
end
end
类工具控制器“新建”
flash[:注意]=“您没有填写所有字段”
结束
结束
定义编辑
结束
def更新
@工具更新(工具参数)
将_重定向到@tool
结束
def销毁
@毁灭
重定向到工具路径
结束
私有的
def设置工具
@tool=tool.find(参数[:id])
结束
def工具参数
参数require(:tool).permit(:name,:description,:user\u id,:tool\u image,:rent\u price)
结束
结束
看看我的收费控制器。条带签出文档通常将amount值硬编码到@amount实例变量中。但是我希望它由工具创建者设置。我的工具表上有一个rent_price列,希望将此值传递给amount实例变量
我试图通过找到产生电荷的工具来实现这一点。然而,没有电荷模型,因此工具和电荷之间没有关联。条带签出在没有模型的情况下工作。在这种情况下,我不确定如何访问费用控制器中工具创建者(tool.rent_price)设置的金额。似乎没有向参数传递Tool.id。有没有办法解决这个问题
我想我会创建一个收费模型,即使stripe没有说,并使用一个关联来链调用金额。但不确定是否有更好的方法而不必创建费用模型 对我来说,这看起来像是使用错误的参数或路由问题 您是否尝试使用
Tool.find(params[:Tool\u id])
而不是Tool.find(params[:id])
根据关于嵌套资源的文档,在您的示例中,
params[:id]
应该与费用资源相关,params[:tool_id]
应该与工具资源相关。您可以使用rake routes
检查存在哪些路由以及它们采用哪些参数。这是正确的!我还是习惯了很多这些东西,谢谢!