【问题标题】:Render html.erb (not partial) in other html.erb在其他 html.erb 中渲染 html.erb(不是部分)
【发布时间】:2013-03-04 11:09:18
【问题描述】:

我对 Rails 很陌生,正在尝试使用 Foundation 框架制作应用程序。问题出在模态窗口上。

我在views/users 文件夹中有一个index.html.erb 文件,由脚手架生成。 在同一文件夹中还有一个new.html.erb 文件。

我想在索引页的模态窗口中打开../users/new 链接。

我用Reveal (Foundation Documentation)里的例子,把new.html.erb文件内容放到:

<div id="myModal" class="reveal-modal">
.
.
</div>

另外,添加新用户链接“href”属性。

<%= link_to 'New User', new_user_path, 'data-reveal-id' => 'myModal' %>

here 所述。

现在我需要在 index.html.erb 中渲染 new.html.erb,因为我需要在 /body 标记之前使用此代码,如 Foundation Docs 中所述。

我尝试将其渲染为部分,但它不起作用。

有没有办法做到这一点,而不是将new.html.erb 加倍为_new.html.erb 并将其作为部分插入index.html.erb

我不认为,RoR 的所有内容都是“少写代码!”东西,没有代码复制就没有办法。

谢谢!

【问题讨论】:

    标签: ruby-on-rails zurb-foundation


    【解决方案1】:

    您不能按原样使用新操作,尽管您可以将表单放在一个部分中,使用新的和您的操作以在模态中生成表单。

    在页面加载时,您可以使用 _form partial。

    如果需要,您可以使用 ajax 为模型生成表单,这可以用于编辑以及新建。

    Step1 :link_to "your action path", remote: true, id: "abc"

    第2步:你的动作路径应该使用渲染到字符串返回表单的html

    Step3:现在使用 $("#abc") 的 jquery ajax 成功将您的模型内部内容替换为表单。

    并对 ajax 和回发使用 create 操作。

    代码是干燥的。

    如果您需要代码,请告诉我。

    谢谢

    【讨论】:

    • 谢谢!你的答案的 Ajax 部分对我来说仍然不清楚。但我使用了您的_form 建议。因此,我的代码如下所示:&lt;%= link_to 'New User', new_user_path, 'data-reveal-id' =&gt; 'new-user-modal' %&gt; &lt;div id="new-user-modal" class="reveal-modal"&gt; &lt;%= render 'form' %&gt; &lt;/div&gt; 在索引部分中将@user = User.new 添加到users_controller.rb 之前它也不起作用。 ` def index @users = User.all @user = User.new respond_to do |format| format.html # index.html.erb format.json { 渲染 json: @users } end end`
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多