如何在Rails中有条件地显示局部

时间:2019-07-11 12:37:00

标签: ruby-on-rails

如何在Rails中动态显示局部视图?

我正在尝试这样的事情:

def showdata(summary)
    blah = 'test'
    if summary
        partial('partial1', locals: { blah })
    else
        partial('partial2', locals: { blah })
    end

我对Rails非常陌生:-(

2 个答案:

答案 0 :(得分:0)

您可以使用渲染(this is the reference),下面是示例代码

def showdata(summary)
    blah = 'test'
    if summary
        render('partial1', locals: { blah })
    else
        render('partial2', locals: { blah })
    end
end

答案 1 :(得分:0)

也可以:

render partial: "partial1", locals: { blah } if summary
render partial: "partial2", locals: { blah } if !summary

或使用三元运算符

render partial: summary? "partial1" : "partial2", locals: { blah }