Rails - 控制器:注意 - 添加变量?

发布于 2024-10-03 10:50:19 字数 752 浏览 3 评论 0原文

我很陌生,但有一个关于修改脚手架的基本问题:通知添加变量。例如,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹沉浮 2024-10-10 10:50:19

Beestings 是将动态值插入到 ruby​​ 字符串中的首选方法。因此,如果您希望在 :notice 中添加 @order.id,您可以这样做:

 def create
    @order = Order.new(params[:order])

    respond_to do |format|
      if @order.save
        format.html { redirect_to(@order, :notice => "Order id # #{@order.id} 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

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:

 def create
    @order = Order.new(params[:order])

    respond_to do |format|
      if @order.save
        format.html { redirect_to(@order, :notice => "Order id # #{@order.id} 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文