2

我有实现模型:

# 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)

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

4

1 回答 1

2

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

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

即在新操作中,您应该有一行显示@realization = Realization.new

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

于 2013-04-12T03:50:33.037 回答