【问题标题】:How to make a def inherit from another def,from the same controller Rails 4如何使def从另一个def继承,来自同一个控制器Rails 4
【发布时间】:2015-07-28 23:27:02
【问题描述】:

这就是我的演出控制器中的内容

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,在用户相互购买时进行积分交换。(我没有买家和卖家)而不是“用户和当前用户”。现在你看到我有 @987654323 def download page中的@,交易成功后直接跳转到演出的网址。

我打算创建一个名为“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中,

如何继承?

【问题讨论】:

  • 你在哪里设置@gig?是否在 before_action 中?
  • 你也可以通过redirect_to success_download_path, gig: @gig.boxlink from
  • @Nithin 并在 views/gig/success_download 中使用 对吗?

标签: ruby-on-rails ruby ruby-on-rails-4 model-view-controller model


【解决方案1】:

@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。但局部效果会很好。

【讨论】:

  • 你的方法有效,我没想到的一件事是导航栏消失了,页脚,当我重定向到那个部分时,所有的东西,直接通过控制器,都是一个白页,最后链接在我想要的页面中间。我将如何显示添加导航栏、正文、页脚也在那里?我猜他们消失了,因为它实际上不是一个页面,而只是一个部分。
  • 对不起,如果我让你感到困惑,基本上支付消失了,我希望它出现,通过@gig.boxlink,你的方法有效,但布局消失了。
  • 是的,因为部分不会渲染布局。使用 then normal render without partial 来渲染带有布局的整个页面,并传递变量以在下载视图中渲染特定的部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-29
  • 2013-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多