Rails - 控制器:注意 - 添加变量?
我很陌生,但有一个关于修改脚手架的基本问题:通知添加变量。例如,rails 为我创建了以下 create 方法:
def create
@order = Order.new(params[:order])
respond_to do |format|
if @order.save
format.html { redirect_to(@order, :notice => 'Order was successfully created.') }
format.xml { render :xml => @order, :status => :created, :location => @order }
else
format.html { render :action => "new" }
format.xml { render :xml => @order.errors, :status => :unprocessable_entity }
end
end
end
我要做的就是向 :notice 添加一个变量,以便它可以具体打印创建的订单(或使用 update 方法编辑的订单)。我尝试了一些基本的东西,例如传递 <%= order.id %>,尽管我觉得这在控制器中看起来不自然?
在此脚手架的这种格式中是否可以添加动态值?或者说这违反了惯例。
我很感谢您的帮助,如果这很新鲜,我很抱歉。
I'm green behind the ears, but had a basic question about modifying the scaffolding's :notice to add a variable. For example, rails created the following create method for me:
def create
@order = Order.new(params[:order])
respond_to do |format|
if @order.save
format.html { redirect_to(@order, :notice => 'Order was successfully created.') }
format.xml { render :xml => @order, :status => :created, :location => @order }
else
format.html { render :action => "new" }
format.xml { render :xml => @order.errors, :status => :unprocessable_entity }
end
end
end
What I'm looking to do is add a variable to :notice so that it would print specifically what order was created (or edited with the update method). I tried some basic things using such as passing <%= order.id %>,though I felt this seemed unnatural within the controller?
Is adding a dynamic value possible within this format of this scaffolding? Or is it against the convention.
I appreciate the help, sorry if this is very newbish.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Beestings 是将动态值插入到 ruby 字符串中的首选方法。因此,如果您希望在
:notice
中添加@order.id
,您可以这样做:Beestings are the preferred way to insert dynamic values into strings in ruby. So if you wanted
@order.id
in your:notice
, you could do this: