0

I'm using this tutorial to set bootstrap modal content in forms, but I want to set the title...

I think I don't understand how to set the title content_for method...I understand it for templating, but if I want to send a title to the modal, is it best to make a helper for that? Is there a better Rails 4 way to do this?

Please advise, and remember, I'm not a CE--I've looked at the rails doc and google searched, so please don't demean my question by posting a google link. Thanks!

4

1 回答 1

2

如果您要动态呈现模态框的标题,通常最好使用辅助方法。这将使您的视图逻辑保持干燥和可读。此方法将接受标题的参数,如果不存在,它将呈现默认标题。

在 application_helper.rb 中:

  def modal_title(title)
    if title.empty?
      "My Default Title"
    else
      title
    end
  end

在标题应该出现的模态部分中:

<%= modal_title(yield(:title)) %>

在视图中,您将有权使用 content_for 动态设置标题。例如,您可以传递一个字符串作为您的标题或从您的控制器传递一个实例变量。

<% content_for :title, @your_variable %>
于 2015-02-09T16:29:19.347 回答