Rails:表单输入在提交后不保存

时间:2012-06-30 06:00:55

标签: ruby-on-rails forms polymorphic-associations actionview actioncontroller

为什么没有任何东西进入这个表单保存?

new.html.erb

<%= form_for [@requestable, @request] do |f| %>
   <%= f.label :status %>
   <%= f.text_field :status, rows: 8 %>
   <%= f.submit "Request", :class => 'btn'%>
<% end %>

requests_controller.rb

class RequestsController < ApplicationController
   before_filter :load_requestable

   def index
     @requests = @requestable.requests
   end

   def new
     @request = @requestable.requests.new
   end

   def create
     @request = @requestable.requests.new(params[:status])
     if @request.save
        redirect_to [@requestable, :requests], notice: "Request sent."
     else
      render :new
   end
 end

private

  def load_requestable
    klass = [Company, Profile].detect { |c| params["#{c.name.underscore}_id"]}
    @requestable = klass.find(params["#{klass.name.underscore}_id"])
  end

end

我的控制器基于此 https://github.com/railscasts/154-polymorphic-association-revised/blob/master/blog-after/app/controllers/comments_controller.rb

request.rb
    class Request&lt;的ActiveRecord ::基

  attr_accessible :status

  belongs_to :requestable , polymorphic: true
  belongs_to :profile

  validates :status,  presence: true

end

这是由我的debuger

制作的
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ✓
authenticity_token: /0H2k89HN4JVXBPsoFWen5rUfx2xr4p5hr1uDSQVlcA=
request: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  status: pending
commit: Request
action: create
controller: requests
company_id: '1'

1 个答案:

答案 0 :(得分:1)

看看你的params散列中的内容。状态字段可能类似于params[:request][:status]。假设标准的activerecord-y东西,你想要将整个请求对象的哈希值传递给.new。