Rails创建动作不起作用 [英] Rails Create Action Not Working
问题描述
我的简单表单不执行POST。我一直在查看,却无法发现问题所在(我确定是在我的路线上)。这是我所拥有的:
My simple form is not doing a POST. I've been looking at this and haven't been able to see what is wrong (I'm sure it's in my routes). Here's what I have:
视图:
views/buyers/new.html.erb
<%= form_for(@buyer) do |f| %>
<%= f.text_field :phone %><br />
<%= f.text_field :make %><br />
<%= f.text_field :model %><br />
<%= f.submit %>
<% end %>
控制器:
BuyersController
def new
@title = "Welcome to Car Finder"
@buyer = Buyer.new
end
def create
@buyer = Buyer.new(params[:buyer])
if @buyer.save!
redirect_to success
else
redirect_to :back
end
end
路线:
resources :buyers
耙道:
buyers GET /buyers(.:format) buyers#index
POST /buyers(.:format) buyers#create
new_buyer GET /buyers/new(.:format) buyers#new
edit_buyer GET /buyers/:id/edit(.:format) buyers#edit
buyer GET /buyers/:id(.:format) buyers#show
PUT /buyers/:id(.:format) buyers#update
DELETE /buyers/:id(.:format) buyers#destroy
当我提交表单时,它将保留在同一页面上,永远不要执行create动作。下面是来自日志的
When I submit the form, it stays on the same page, never going to the create action. Below is from the log
Started GET "/?..[params]..."
Processing by BuyersController#new as HTML
感谢您提供的任何帮助
推荐答案
重新启动服务器可能是明智的。您的问题可能出在持久性级别或 buyer.rb
文件中的验证中。将此添加到_form.html.erb:
It is probably wise to restart your server. You're issue may lie in validations you have at your persistence level or in your buyer.rb
file. Add this to the _form.html.erb:
<% if @buyer.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@buyer.errors.count, "error") %> prohibited this from being saved: </h2>
<ul>
<% @buyer.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
尝试再次完成请求。查看是否抛出任何错误。解决这些问题。
Try to complete the request again. See if any errors are being thrown. Fix those.
这篇关于Rails创建动作不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!