如何将部分当前文件夹添加到rails视图路径?

时间:2010-09-17 09:34:52

标签: ruby-on-rails partial-views render view-path

我有一组局部用于根据用户在下拉菜单中的选择更新表单的一部分。有很多不同的选择,所以不要像这样的视图文件夹:

app/views/myview/
  _choice001.html.erb
  _choice002.html.erb
  ...
  _choice998.html.erb
  _choice999.html.erb
  _form.html.erb
  _sharedchoicestuff1.html.erb
  _sharedchoicestuff2.html.erb
  edit.html.erb
  new.html.erb

我想这样说出来:

app/views/myview/
  choices/
    _choice001.html.erb
    _choice002.html.erb
    ...
    _choice998.html.erb
    _choice999.html.erb
    _sharedchoicestuff1.html.erb
    _sharedchoicestuff2.html.erb
  _form.html.erb
  edit.html.erb
  new.html.erb

如果我这样做,那么我知道我需要将render :partial => whatever更改为render :partial => "myview/choices/#{whatever}",这在表单中是正常的,但我不想在所有选择模板中更改它。有没有办法添加'。'到视图路径,所以我仍然可以在选择模板中使用render :partial => 'sharedchoicestuff1'

1 个答案:

答案 0 :(得分:2)

为此创建一个帮助:

def render_choice(name)
  render "myview/choices/#{name}"
end