问题
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:
view:
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 %>
controller:
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
routes:
resources :buyers
rake routes:
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
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
Thanks for any help you can give
回答1:
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.
来源:https://stackoverflow.com/questions/12429097/rails-create-action-not-working