【发布时间】: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">×</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