好的,所以这个问题与html.erb代码的正确语法有关。我正在尝试使用弹出按钮显示表单。我可以通过button_tag函数创建按钮,也可以通过form_tag函数创建表单,但是无法将一个嵌入另一个。我什至不能完全确定这是我应该做的事情。我的理解是,最佳实践是避免在可以使用erb进行页面生成时使用html。
无论如何,这是我到目前为止的代码:
<%= button_tag "Remove Friend", :title=>"Remove from Friend List",
:id=>"removeFriend", :class=>"btn btn-default", :rel=>"popover",
:data => {:html=>"true", :placement=>"top", :content=>
"form_tag(\"/friend\", method: \"post\") do
label_tag(:symbolInput, \"Enter Username\")
text_field_tag(:symbolInput)
submit_tag(\"Remove\")
end"}%>
所以这是生成以下HTML
<button class="btn btn-default" data-content="form_tag("/friend",
method: "post") do
label_tag(:symbolInput, "Enter Username")
text_field_tag(:symbolInput)
submit_tag("Remove")
end" data-html="true" data-placement="top" id="removeFriend"
name="button" rel= "popover" title="Remove from Friend List"
type="submit">Remove Friend</button>
因此,从本质上讲,它只是将erb代码作为文本复制到弹出窗口中。
我也尝试过用<%=%>来为每一行定帧,但是我不清楚它的语法是什么,或者什么时候应该这样做。
基本上需要发生的是,表单的erb必须转换为html,然后将其传递到button_tag的:content部分。
我应该以这种方式执行此操作,还是有其他方法可以完成我要执行的操作?作为新手,我不确定最佳实践是什么。
顺便说一句,如果我使用html对另一种表单或按钮和erb进行编码,则它可以完美地工作,因此可以解决。
当您进入嵌入式Ruby标签(例如<%%>)的那一刻,那么您在其中所做的一切都只是红宝石,或者说另一种方式必须是有效的红宝石,可以是任何红宝石。
我要做的第一个重构是将所有内容代码移出自己的位置。这个中间点比较整洁,应该可以给您带来更好的想法。
<% remove_friend_form = "form_tag(\"/friend\", method: \"post\") do
label_tag(:symbolInput, \"Enter Username\")
text_field_tag(:symbolInput)
submit_tag(\"Remove\") end" %>
<%= button_tag "Remove Friend", :title=>"Remove from Friend List",
:id=>"removeFriend", :class=>"btn btn-default", :rel=>"popover",
:data => {:html=>"true", :placement=>"top", :content=> remove_friend_form }%>
隔离内容后,很明显可以进一步隔离“ remove_friend_form”。为此,请将此内容移至其自己的部分内容中。
# create new file in the same folder as the current view.
# _remove_friend_form.html.erb
<%= form_tag("friend", method: "post") do |f| %>
<%= f.label_tag(:symbolInput, "Enter Username") %>
<%= f.text_field_tag(:symbolInput) %>
<%= f.submit_tag("Remove") %>
<% end %>
主页现在看起来像这样
<%= button_tag "Remove Friend", :title=>"Remove from Friend List",
:id=>"removeFriend", :class=>"btn btn-default", :rel=>"popover",
:data => {:html=>"true", :placement=>"top", :content=> (render partial: 'remove_friend_form') }%>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句