打开模态表单而不导航到rails中的新页面

时间:2015-02-20 18:47:46

标签: ruby-on-rails zurb-foundation

我有一个用户拥有“群组”页面的网站,为了创建新群组,该用户当前必须点击New Group按钮,将其定向到{{1} }页面。我想摆脱那个页面,并用模态弹出窗体替换它。

在群组视图中,我有一个部分localhost/groups/new,如下所示:

_form.html.erb

然后我的<%=form_for @group do |f|%> <div class = "title"> <%=f.label :title%> <%=f.text_field :title%> </div> <div class = "description"> <%=f.label :description%> <%=f.text_field :description%> </div> <div class = "memberships"> <%for user in @users%> <%= label_tag user.name%> <%= check_box('members', "[#{user.id}]") %> <% end %> </div> <div class="actions"> <%= f.submit 'Submit', class: "button radius"%> </div> <%end%> 文件看起来像这样:

new.html.erb

如何将此表单设为弹出模式而不是全新页面?我想我可以像这样在一个揭示模式中渲染表单:

<h1>New Group</h1>

<%= render 'form' %>

但这不起作用,因为我收到错误:

<!DOCTYPE html>
<html>
<head>
    <title>Title</title>
    <script src="js/vendor/jquery.js"></script>
    <script src="js/foundation/foundation.js"></script>
    <script src="js/foundation/foundation.reveal.js"></script>
    <link rel="stylesheet" type="text/css" href="css/normalize.css">
    <link rel="stylesheet" type="text/css" href="css/foundation.min.css">
</head>
<body>
    <a href="#" class="button" data-reveal-id="newGroup">New Group</a>
    <div class="reveal-modal" id="newGroup" data-reveal>
        <%= render 'form' %>
        <a class="close-reveal-modal">x</a>
    </div>
    <script>
        $(document).foundation();
    </script>
</body>

我是否需要更改控制器中的某些内容才能使其正常工作?或者我可以在一个模态中以某种方式呈现我需要的页面吗?

编辑:这是我控制器中的First argument in form cannot contain nil or be empty --> <%=form_for @group do |f|%> <div class = "title"> <%=f.label :title%> <%=f.text_field :title%> </div> 方法。我需要在这里改变什么?

index

2 个答案:

答案 0 :(得分:1)

您对改变控制器中某些内容的猜测可能是正确的。

您需要确保@group实例变量由呈现表单的控制器中的操作定义。这可能类似于:

class GroupsController < ApplicationController
  # ...

  def index
    @group = Group.new
    # The rest of the implementation of this action.
  end

  # ...
end

这假设您希望模式显示的任何页面都由index的{​​{1}}操作呈现。

答案 1 :(得分:1)

试试这个:

在您的视图文件中

<div class="reveal-modal" id="newGroup" data-reveal>
  <%= render template: 'groups/new' %>
  <a class="close-reveal-modal">x</a>
</div>