模态更新数据但抛出错误

时间:2015-07-23 20:25:38

标签: ruby-on-rails twitter-bootstrap bootstrap-modal

我希望能够更新模态中的数据,并在模式结束时将更新的数据显示在表格中。

它正在更新数据库,但在此之后会抛出此错误:

C:\git\junit [cleanup +0 ~0 -1]> git rm '*.md'
rm 'README.md'
C:\git\junit [cleanup +0 ~0 -2]> git checkout master
D       README.md
D       target/surefire-reports/com.tester.webdriver.MyFristTest.txt
Switched to branch 'master'
**Your branch is up-to-date with 'origin/master'.**
C:\git\junit [master +0 ~0 -2]> git merge cleanup
Already up-to-date.

并强调这一行代码:并说@products是零

NoMethodError in Products#update

这完全在index.html.erb

<% @products.each do |product| %>

然后这是我的HomeController

<div class="container-fluid col-md-8">
<h1>Products</h1>
<table class="table table-striped table-hover table-bordered table-condensed
sortable">
<thead>
  <tr>
    <th><a>TITLE</a></th>
    <th><a>ASIN</a></th>
    <th><a>PRICE</a></th>
    <th> </th>
    <th><a>Toggle</a></th>
  </tr>
</thead>
<tbody>
  <% @products.each do |product| %>
    <tr>
      <td><%= product.title %></td>
      <td><%= link_to product.asin, "" %></td>
      <td><%= number_to_currency(product.price) %></td>
      <td>
        <%= link_to 'Edit', "" , :class => 'btn btn-primary btn-sm' %>
        <%= link_to 'Delete', "" , :method => :delete, :confirm => 'Are you 
        sure?', :class => 'orange btn btn-primary btn-sm' %>
      </td>
      <td>
        <a href="#" class="btn btn-primary btn-sm" data-toggle="modal" 
        data-remote="<%= edit_product_path(product) %>" #largeModal<%= 
        product.asin %> data-target="#largeModal<%= product.asin%>"
        >Click</a>
      </td>  
        <div class="modal fade" id="largeModal<%= product.asin %>" 
        tabindex="-1" role="dialog" aria-labelledby="largeModal" 
        aria-hidden="true">
          <div class="modal-dialog modal-lg">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Edit Product: <%= product.asin %></h4>
              </div>
              <div class="modal-body">
                <h3>Modal Body</h3>
                  <div class="well col-md-8 col-md-offset-2">
                    <%= form_for product do |f| %>

                    <label>Title</label>
                    <%= f.text_field :title %>

                    <label>ASIN</label>
                    <%= f.text_field :asin %>

                    <%= f.submit "submit", class: "btn btn-primary btn-sm" %>

                    <% end %>
                  </div>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default"
                data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Update</button>
              </div>
            </div>
          </div>
        </div>
    </tr>
  <% end %>
</tbody>

我的ProductsController:

class HomeController < AuthenticatedController

def index
  @products = Product.paginate(page: params[:page], per_page: 4)
  @home = @products
end

end

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为当它呈现模态时,@productsnil。您需要定义它,否则@products.each无法正常工作。

也可能是因为您在呈现params[:page]时没有为home/index定义任何内容。