我尝试了这个form_for标记的每个配置,告诉它发布表单,但它会继续发布一个GET请求,其中包含我在post请求中需要的参数。以下是视图中的表单:
dash.html.erb:
<ul class = "new-offer-form" id = "new-offer-form">
<form class="form-inline">
<%= form_for(@offer, url: offers_create_path(@offer), method: :post, :html => {:class => "form-inline"}) do |f|%>
<%= render 'shared/error_messages', object: f.object %>
<div class="form-group">
<li class = "offer-item"> <%= f.text_field :category %> </li>
</div>
<div class="form-group">
<li class = "offer-item"> <%= f.text_field :plan_name %> </li>
</div>
<div class="form-group">
<li class = "offer-item"> <%= f.text_field :price %> </li>
</div>
<div class="form-group">
<li class = "offer-item"> <%= f.text_field :rate %> </li>
</div>
<div class="form-group">
<li class = "offer-item"> <%= f.submit "Add new service plan", id: "new_link" %> </li>
</div>
<% end %>
</form>
这是控制器代码:
def dash
@offer = current_user.offers.build()
@offers = current_user.offers
respond_to do |format|
format.html
format.json {render :json => @offer}
end
端
def create
@user = current_user
@offer = @user.offers.build(offer_params)
if @offer.save
flash[:success] = "Service plan added"
else
render 'offers/dash'
end
end
这是在提交表单后出现在服务器日志中的内容:
Started GET "/offers/dash?utf8=%E2%9C %93&authenticity_token=42V3KL6EB9BBE4h7LQbMIR44MZLq2KisKtgA6gh9dMBer5tewe%2 BUEWzZ0t0MG49IdisczNPz0PMtnUHSAxdf3A%3D%3D&offer%5Bcategory%5D=test& offer%5Bplan_name%5D=hello&offer%5Bprice%5D=there&offer%5Brate%5D=world&commit=Add+new+service+plan" for 127.0.0.1 at 2017-04-12 22:16:25 -0400
Processing by OffersController#dash as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"42V3KL6EB9BBE4h7LQbMIR44MZLq2KisKtgA6gh9dMBer5tewe+U EWzZ0t0MG49IdisczNPz0PMtnUHSAxdf3A==", "offer"=>{"category"=>"test", "plan_name"=>"hello", "price"=>"there", "rate"=>"world"}, "commit"=>"Add new service plan"}
我尝试将网址更改为&#39; offer / create&#39;哪些路线提供#cudate&#39;无济于事。有什么想法吗?
答案 0 :(得分:0)
正如Dario在评论中所说,你实际上是在放置两个<form>
标签,这些标签不是有效的HTML。
form_for
默认为帖子(因此您不需要:method =&gt;:post)。默认情况下,基本的html <form>
代码是get,这就是您看到Get请求的原因。
并且,如果您要进行标准create
操作并且已在路线中定义。
resources :offers
然后这将是form_for行
<%= form_for(@offer, :html => {:class => "form-inline"}) do |f|%>