我在提交简单表单时遇到问题,我不完全确定交易是什么。我认为,问题在于我正在尝试将表单从用户的个人资料页面提交到另一个模型(ItemShare)。它不是通过 post 提交表单,而是尝试发出 GET 请求。当我更改 ItemSharesController 以允许这样做时,它会将 ItemShare#index 放入我的模态表单中,即使我已经指定了十亿个我希望它发布的位置。邮政!
在我的 routes.rb 中:
match '/item_shares' => 'item_shares#create', :via => :post
resources :item_shares, :except => :create
表格:
#share-list-button-dialog.modal.hide.fade{:role => "dialog"}
.modal-dialog
%h3 Share List
.modal-body
=form_for(ItemShare.new, :method => :post, :url => {:action => "create"}) do |f|
=f.hidden_field(:item_id, :value => item.token)
=f.hidden_field(:owner_id, :value =>user.id)
=f.label :shared_user_email, "Your collaborator's email:"
=f.text_field :shared_user_email, :value => "collaborator@example.com"
=f.submit "Share"
项目共享控制器:
class ItemSharesController < ApplicationController
def new
@item_share = ItemShare.new
end
def create
@item_share = ItemShare.new(params[:item_share])
respond_to do |format|
if @item_share.save
format.html {redirect_to user_path(current_user.id), :notice => "List shared successfully"}
else
flash.now[:alert] = "Could not share list."
end
end
end
end
这就是堆栈跟踪显示的内容:
Started GET "/item_shares/" for 127.0.0.1 at 2013-10-15 21:40:44 -0400
ActionController::RoutingError (No route matches [GET] "/item_shares"):
actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
...
这里发生了什么??
更新...终于知道发生了什么。表单本身不是问题,而是我用来触发模态表单的按钮。我有:
%button{'data-toggle' => 'modal', 'href' => '../item_shares/', 'data-target' => '#share-list-button-dialog'}
.
将其更改为:
=button_to "Share", item_shares_path, "data-toggle" => "modal", "data-target" => "#share-list-button-dialog"
现在一切正常。非常感谢 Helios de Guerra 的帮助和耐心。:)