我正在制作物品,它们属于特定用户。我正在做不同的操作,一切正常(项目使用正确的用户 ID、图像上传等制作)。
现在对于编辑操作,一切正常,页面被渲染,当前项目信息被填写。如果我然后点击“更新”按钮,我会收到一个路由错误。(没有路线匹配 [POST] "/items/6")
我的控制器 items_controller.rb
# items_controller.rb
#-------------------
class ItemsController < ApplicationController
before_filter :signed_in_user
respond_to :html, :js
def edit
@item = current_user.items.find(params[:id])
@categorys = current_user.items.group(:category)
end
def update
if @item.update_attributes(params[:item])
redirect_to items_path
else
render 'edit'
end
end
end
@item 变量从 ID 中获取当前项目,然后 @categorys 将显示用户可以从中选择的已使用类别。
现在我的edit.html.erb 文件(它很长我使用引导程序)
<%= provide(:title, 'Edit Item') %>
<h1>Edit Item</h1>
<div class="row">
<div class="span7">
<%= form_for(@item, html: {class: "form-vertical"}) do |f| %>
<fieldset>
<span class="legend">Please pick a logo that represents the item.</span>
<%= render 'error_messages' %>
<div class="control-group">
<label class="control-label" for="name">1. Give your item a name. Please don't include the volume of the
item.</label>
<div class="controls">
<%= f.text_field :name, size: nil, class: 'input-xlarge', id: 'name' %>
</div>
</div>
<div class="control-group">
<label class="control-label" for="logo">2. Select a Logo for the item (this will help identifying the
item)</label>
<div class="controls">
<%= image_tag @item.logo.url(:original), alt: @item.name %>
<%= f.file_field :logo, size: nil, id: 'logo', 'accept' => 'image/*' %>
</div>
</div>
<div class="control-group">
<label class="control-label" for="category">3. Type the name of a new category or click on an
existing</label>
<ul class="breadcrumb category-select">
<% @categorys.each do |c| %>
<li><a href="#"><%= c.category %></a></li>
<% end %>
</ul>
<div class="controls">
<%= f.text_field :category, size: nil, class: 'input-xlarge', id: 'category' %>
</div>
</div>
<div class="control-group">
<label class="control-label">4.Specify how much 1 quantity is and how many items you have in stock at this
moment.<strong>Don't use this for updating the stock, please go to stock!</strong><br>
For example: a bottle of coke 0.25l and I have 24 of them.</label>
<div class="form-horizontal category-stock">
<div class="control-group">
<label class="control-label" for="quantity">Quantity (l)</label>
<div class="controls">
<div class="input-append">
<input class="span1" id="quantity" name="quantity" type="text">
<span class="add-on">l</span>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label" for="amount">Amount (#)</label>
<div class="controls">
<div class="input-append">
<input class="span1" id="amount" name="amount" type="text">
<span class="add-on">#</span>
</div>
</div>
</div>
</div>
<%= f.text_field :stock, size: nil, type: 'hidden' %>
<div class="form-actions category-stock">
<%= f.submit "Edit Item", class: "btn btn-large btn-primary" %>
</div>
</div>
</fieldset>
<% end %>
</div>
</div>
我的 routes.rb 文件
root to: 'sessions#new'
resources :items do
member do
get :category
end
end
resources :events
resources :ei_relationships, only: [:create, :destroy]
resources :users
resources :sessions, only: [:new, :create, :destroy]
match '/signup', to: 'users#new'
match '/signout', to: 'sessions#destroy', via: :delete
我在服务器中获得的日志:
Started GET "/items/6/edit" for 127.0.0.1 at 2012-04-20 00:34:34 +0200
Processing by ItemsController#edit as HTML
Parameters: {"id"=>"6"}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = '5le_BlKtwTQWeS15pzlCGQ' LIMIT 1
Item Load (0.0ms) SELECT "items".* FROM "items" WHERE "items"."user_id" = 1 AND "items"."id" = ? LIMIT 1 [["id", "6"]]
Rendered shared/_error_messages.html.erb (0.0ms)
Item Load (1.0ms) SELECT "items".* FROM "items" WHERE "items"."user_id" = 1 GROUP BY category
Rendered items/edit.html.erb within layouts/application (8.0ms)
Rendered layouts/_shim.html.erb (0.0ms)
Rendered layouts/_header.html.erb (1.0ms)
Rendered layouts/_footer.html.erb (1.0ms)
Completed 200 OK in 77ms (Views: 73.0ms | ActiveRecord: 1.0ms)
Started POST "/items/6" for 127.0.0.1 at 2012-04-20 00:34:47 +0200
ActionController::RoutingError (No route matches [POST] "/items/6"):
actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.3) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.3) lib/rails/engine.rb:479:in `call'
railties (3.2.3) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.0ms)
我认为这就是所有文件。就像我说的一切正常,但按下编辑按钮时似乎只有编辑功能被破坏。
谢谢!
编辑
好吧,经过一番挖掘,我发现了问题。我使用 jquery 来更新隐藏输入,该输入是 2 个输入字段的乘积。代码如下。真的不明白为什么这段代码会弄乱rails路由..
// Calculate the stock from the 2 fields and insert it in the hidden field
$('.category-stock').find('input').on("click select change", function () {
$.calculateStock();
});
$.calculateStock = function () {
var quantity = parseFloat($('input[name="quantity"]').val());
var amount = parseFloat($('input[name="amount"]').val());
var result = quantity * amount;
if (isNaN(result)) {
result = 0;
}
$('input[type="hidden"]').val(result);
};