边栏和渲染部分,尝试在侧边栏上运行todo应用程序

时间:2016-07-06 23:45:09

标签: ruby-on-rails

我是ruby on rails的新手,我建议使用侧边栏,以便在大多数页面上显示我想要的内容。我目前在自己的页面上有一个名为Todo List的应用程序。它只是让您创建一个任务并显示任务。它在自己的页面上运行。现在我想移动该应用程序并让它在我的侧边栏中运行。我已经实现了我的侧边栏如下。

/views/layouts/application (in body)
<%= yield :sidebar %>

/views/myapp1/index
<% content_for(:sidebar) do %>
 <% render :partial => "layouts/sidebar" %>
<% end %>


/views/layouts/_sidebar
 <% if logged_in? %>
  <div id="mySidenav" class="sidenav">

  <div class="row", id="container_todo_lists">
   <%= render @todo_lists %>
  </div>

  <div class="links" data-toggle="modal" data-target="#mynewtodo_list">
   <%= link_to('New Todo List', 'javascript:;', :id => :random) %>
  </div>

  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
 </div>

<span style="font-size:30px;cursor:pointer" onclick="openNav()">☰ Todo List</span>
<script>
 function openNav() {document.getElementById("mySidenav").style.width = "250px";}

 function closeNav() {document.getElementById("mySidenav").style.width = "0";}
</script>

<% end %> 

<!-- Modal  create action -->
  <%= form_for(@todo_list, remote: true) do |f| %>
  <div class="modal fade" id="mynewtodo_list" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">

        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="myModalLabel">Todo</h4>
        </div>

        <div class="modal-body">
          <div class="field">
            <%= f.label :title %><br>
            <%= f.text_area :title, class: "form-control todo_list_title" %>
          </div>
          <div class="field">
            <%= f.label :description %><br>
            <%= f.text_area :description, class: "form-control todo_list_content" %>
          </div>
        </div>

        <div class="modal-footer">
          <%= submit_tag "Create", class: "btn btn-primary" %>
          <button type="button" class="btn btn-default" data-dismiss="modal" id="mynewtodo_listclose">Close</button>
        </div>

      </div>
    </div>
    </div>
  <% end %>
<!-- Modal -->

我收到此错误

 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.

我想我知道问题的一部分是我的

@todo_lists

因为我试图在我的todo_list页面上调用侧边栏并且它可以工作,但侧边栏搞砸了我的原始应用程序,所以当我按下编辑并显示它会将屏幕变为灰色。任何建议都非常感谢。提前谢谢。

/views/todo_lists/_todo_list.html.erb
<div class="text-center" id="todo_list_<%= todo_list.id %>">

                <h3 class="panel-title"><%= todo_list.title %>
                  <sup style="color: #3BB2D6; position: absolute; top: 5px; right: 15px; text-align:right;">
                      (<%= time_ago_in_words(todo_list.created_at) %>)
                  </sup>
                </h3>

                <p><%= todo_list.description %></p>

                <div class="row" role="group">
                    <div class="links1" data-toggle="modal" data-target="#myupdatetodo_list_<%= todo_list.id %>">
                      <%= link_to('Edit', 'javascript:;', :id => :random) %>
                    </div>

                    <div class="links1" data-toggle="modal" data-target="#myitemtodo_list_<%= todo_list.id %>">
                      <%= link_to('Show', 'javascript:;', :id => :random) %>
                    </div>

                    <div class="links1">
                      <%= link_to 'Destroy', todo_list, method: :delete, remote: true %>
                    </div>

                </div>

        <!-- Modal - update posts -->
              <%= form_for(todo_list, :method => :put, remote: true) do |f| %>
              <div class="modal fade" id="myupdatetodo_list_<%= todo_list.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Edit</h4>
                  </div>
                  <div class="modal-body">

                  <div class="field">
                    <%= f.label :title %><br>
                    <%= f.text_area :title, class: "form-control" %>
                  </div>
                  <div class="field">
                    <%= f.label :description %><br>
                    <%= f.text_area :description, class: "form-control" %>
                  </div>

                  </div>
                  <div class="modal-footer">
                    <button type="button" id="myupdatebutton_<%= todo_list.id %>" class="btn btn-default" data-dismiss="modal">Close</button>
                    <%= submit_tag "Update", class: "btn btn-primary" %>
                  </div>
                </div>
              </div>
              </div>
              <% end %>
        <!-- Modal -->

        <!-- Modal - item posts -->
              <%= form_for(todo_list, :method => :put, remote: true) do |f| %>
              <div class="modal fade" id="myitemtodo_list_<%= todo_list.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Items</h4>
                  </div>
                  <div class="modal-body">



                  <h3><%= todo_list.title %></h3>
                  <p><%= todo_list.description %></p>



                  </div>
                  <div class="modal-footer">
                    <button type="button" id="myitembutton_<%= todo_list.id %>" class="btn btn-default" data-dismiss="modal">Close</button>
                    <%= submit_tag "Add Item", class: "btn btn-primary" %>
                  </div>
                </div>
              </div>
              </div>
              <% end %>
        <!-- Modal -->

1 个答案:

答案 0 :(得分:0)

在您的情况下,这是因为@todo_lists未设置,我确定您是否返回到呈现视图的上一个网址,侧边栏会显示。< / p>

解决方法是确保在每个页面上设置:

class ApplicationController
  before_action :set_to_do_lists

  def set_to_do_lists
    @todo_lists ||= TodoList.all #assign this to what's in your todo_lists_path
  end
end

我想你的应用应该是固定的