【问题标题】:Modal updating data but throwing error模态更新数据但抛出错误
【发布时间】:2015-07-23 20:25:37
【问题描述】:

我希望能够更新我的模态中的数据,并在模态关闭时将更新后的数据显示在表格中。

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

NoMethodError in Products#update

并突出显示这行代码:并说@products 是 nil

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

这些都在 index.html.erb 中

<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>

然后这是我的 HomeController

class HomeController < AuthenticatedController

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

end

还有我的 ProductsController:

class ProductsController < ApplicationController

def new
    @product = Product.new
end

def edit
    @product = Product.find(params[:id])
end

def update
    @product = Product.find(params[:id])

    if @product.update(product_params)
        flash[:success] = "Updated Succesfully!"
        render 'home/index'

    else
      render 'shared/modal' #this is the same modal as a partial..
    end

end

    private
    def product_params
       params.require(:product).permit(:title, :asin)
    end

end

【问题讨论】:

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


    【解决方案1】:

    发生这种情况是因为当它呈现模式时,@productsnil。你需要定义它,否则@products.each 不能工作。

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

    【讨论】:

    • 这就是我不明白的......因为“@products”填充了启动模式的按钮所在的表格。不知道@products 怎么可能是零?知道如何解决吗?
    • 当你第一次渲染时,@products 是在你的家庭控制器的 index 动作中定义的。但是,当您在更新操作中渲染内容时,@products 永远不会被定义。当你做render 'home/index' 时,params[:page] 有什么东西吗?当您执行render 'shared/modal' 时,除非您指定,否则@products 不会有任何内容。
    • 感谢您让我朝着正确的方向前进。实际上已将显示操作更新为 redirect_to root_path.. 现在它工作正常..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2020-04-02
    • 2014-12-31
    • 2015-03-27
    • 2014-01-27
    • 1970-01-01
    • 2017-08-29
    相关资源
    最近更新 更多