Rails创建操作不起作用

时间:2012-09-14 17:13:10

标签: ruby-on-rails routing

我的简单表单不是POST。我一直在看这个并且无法看到什么是错的(我确定它在我的路线中)。这就是我所拥有的:

视图:

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 

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

当我提交表单时,它会保留在同一页面上,永远不会进入创建操作。以下是日志

Started GET "/?..[params]..."
Processing by BuyersController#new as HTML

感谢您提供任何帮助

1 个答案:

答案 0 :(得分:0)

重启服务器可能是明智之举。您的问题可能在于您在持久性级别或buyer.rb文件中的验证。将其添加到_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 %>

尝试再次完成请求。查看是否有任何错误被抛出。解决这些问题。