Ruby on rails:创建动作不起作用,而新的,编辑和更新(与创建中的形式相同!)动作有效 - 为什么?

时间:2013-04-12 03:13:41

标签: ruby-on-rails ruby forms methods

我有实现模型:

# encoding : utf-8
class Realization < ActiveRecord::Base
  attr_accessible :city, :street, :title, :work, :photo, :date

  has_attached_file :photo
end

控制器:

# encoding : utf-8
class RealizationsController < ApplicationController
  before_filter :admin_required, :except => [:index,:show]

  # GET /realization/new
  def new
    @realization = Realization.new
    @realization.date = Time.now.__send__(:to_date).to_s
  end

  # POST /realization
  def create
    @realization = Realization.new(params[:realization])

    if @realization.save
      redirect_to @realization, notice: 'realization was successfully created.'
    else
      render action: "new"
    end
  end

(...) others

表格视图:

<%= form_for @realization, :html => { :multipart => true } do |f| %>
    <% if @realization.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@realization.errors.count, "error") %> prohibited this realization from being saved:</h2>

          <ul>
            <% @realization.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>
          </ul>
        </div>
    <% end %>

    <div class="field">
      <%= f.label :title %><br />
      <%= f.text_field :title %>
    </div>
    (...)
        <div class="field">
            <%= f.file_field :photo %>
        </div>

    <div class="actions">
      <%= f.submit "Submit" %>
    </div>
<% end %>

路线:

resources :realizations

WEBrick服务器信息就是:

Started POST "/realizacje" for 127.0.0.1 at 2013-04-12 12:26:35 +0200
Processing by RealizationsController#index as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"zK5jP4ChBBY+R21TjrZkp4xGvCHViTFJ+8Fw6Og28YY=", "realization"=>{"title"=>"wwwwww", "street"=>"", "city"=>"", "work"=>"", "date"=>"2013-04-12"}, "commit"=>"Submit"}
   (1.0ms)  SELECT COUNT(*) FROM "realizations" 
  Realization Load (2.0ms)  SELECT "realizations".* FROM "realizations" ORDER BY created_at DESC LIMIT 7 OFFSET 0
  User Load (1.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Rendered realizations/index.html.erb within layouts/application (156.0ms)
Completed 200 OK in 340ms (Views: 333.0ms | ActiveRecord: 4.0ms)

当我使用表单并推送提交时,它会重定向/运行程序实现/索引,甚至没有通知或错误! 我完全不知道为什么?特别是它之前有效...... 也许以后添加javascript可能是原因? Paperclip在更新中运行良好,所以它不是......

1 个答案:

答案 0 :(得分:2)

您可以检查新动作以查看您传递给form_for的内容。

您希望传入Realization模型的全新实例。

即。在新操作中,您应该有一行读取@realization = Realization.new

我建议这是因为form_for在你给它的对象上调用一个方法(#new_record?),并根据该方法调用是返回true还是false来提交post或put请求。