如何让一个 def 从同一个控制器的另一个 def 继承 Rails 4

How to make a def inherit from another def,from the same controller Rails 4

这就是我的演出控制器

def downloadpage
    ActiveRecord::Base.transaction do
      if current_user.points >= @gig.pointsneeded 
        current_user.points -= @gig.pointsneeded
        @gig.user.points += @gig.pointsneeded
        current_user.save
        @gig.user.save
        redirect_to @gig.boxlink
      else
        redirect_to :back, notice: "You don't have enough points"
      end
    end
  end

  def success_download
  end

def downloadpage ,在用户相互购买时进行积分交换。(我没有买家和卖家)而是 "user and current user"。现在如你所见,我def download page中有redirect_to @gig.boxlink,交易成功后直接跳转至演出的URL。

我计划创建一个名为“success_download”的页面,该视图将包含类似

的内容
yey you did it
<%= @gig.boxlink %>

并在 def download page 而不是 redirect_to @gig.boxlink 中说

redirect_to success_download_path

问题是@gig 在 def success_download 中不可用,但在 def download page

中可用

how can i make the inheritance?

@gig 在哪里初始化?它是数据库中对象的变量吗?

如果这些变量很重要(例如,用户可以修改它们并在没有资金的情况下使交易成功),那么通过 redirect_to 通过不同的视图传递变量是个坏主意。

最好只渲染结果的其他部分。

def downloadpage ActiveRecord::Base.transaction do if current_user.points >= @gig.pointsneeded current_user.points -= @gig.pointsneeded @gig.user.points += @gig.pointsneeded current_user.save if @gig.user.save render partial:'successful', locals:{link:@gig.boxlink} end else redirect_to :back, notice: "You don't have enough points" end end end

并且在视图中仅使用 link 变量。

另一种方法是使用保存事务状态的模型并在重定向中传递它的 id。但是部分会很好用。