我是Rails的新手,学习使用Rails 4的Agile Web Development一书,并使用Rails版本4.2.4和Ruby版本2.1.5。我现在正处于迭代D3,我遇到了下面所述的错误:
ActiveRecord::RecordNotFound in LineItemsController#create
Couldn't find Product with 'id'=
Rails.root: c:/Ruby on Rails/Agile Rail Development/depot
Application Trace | Framework Trace | Full Trace<br>
app/controllers/line_items_controller.rb:29:in `create'
以下是图片的链接:Link to the error。
在这里,我将代码推送到GitHub:Link to the full code。
这是我的line_items_controller.rb文件。
class LineItemsController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:create]
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
def create
product = Product.find(params[:product_id])
@line_item = @cart.line_items.build(product: product)
respond_to do |format|
if @line_item.save
format.html { redirect_to @line_item.cart, notice: 'Line item was successfully created.' }
format.json { render :show, status: :created, location: @line_item }
else
format.html { render :new }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
private
def set_line_item
@line_item = LineItem.find(params[:id])
end
def line_item_params
params.require(:line_item).permit(:product_id, :cart_id)
end
end
非常感谢您的帮助,谢谢!!
答案 0 :(得分:0)
没有params[:product_id]
使用此
product = Product.find(params[:line_item][:product_id])
答案 1 :(得分:0)
错误可以在line_items <%= button_to 'Add to Cart', line_items_path(prodcut_id: product) %>
视图中找到
第34行 app / views / index.html.erb 。
你有一点错字:
product_id:
应该是prodcut_id:
而不是<%= button_to 'Add to Cart', line_items_path(product_id: product) %>
。
该行应如下所示:
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Add a trip</h3>
</div>
<div class="panel-body">
<div class="form-group">
<label for="from">From</label>
<input type="text" class="form-control" id="from" name="from" placeholder="From" data-bind="value: from">
</div>
<div class="form-group">
<label for="to">To</label>
<input type="text" class="form-control" id="to" name="to" placeholder="To" data-bind="value: to">
</div>
<a class="btn btn-primary btn-lg" role="button" data-bind="click: add()" >Add</a>
</div>
</div>
<div class="panel panel-default">
<div class=panel-heading>Your trips</div>
<table class=table>
<thead>
<tr>
<th>From</th>
<th>To</th>
</tr>
</thead>
<tbody data-bind="foreach: records">
<tr>
<td data-bind="text: from"></td>
<td data-bind="text: to"></td>
</tr>
</tbody>
</table>
</div>
<script type="text/javascript" src="js/knockout-3.4.0.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
var AppViewModel = function() {
this.from = ko.observable();
this.to = ko.observable();
this.records = ko.observableArray();
};
var model = new AppViewModel();
model.add = function() {
model.records.push({
from: model.from(),
to: model.to()
});
//sending data to server
var data =
{
from : this.from(), to : this.to(), date : this.date(), price : this.price(), freeSeats : this.freeSeats()
}
alert(data);
$.post("/data", data, function(response)
{
})
}
ko.applyBindings(model);
</script>
<script>
function tripModel() {
this.records = ko.observableArray([]);
$.getJSON("/usersTrips", function(data) {
self.records(data);
})
}
ko.applyBindings(new tripModel());
</script>