0

为什么不会输入此表单保存任何内容?Rails:提交后表单输入未保存

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
类申请<的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

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

+0

刚试过,它没有改变任何东西。我添加了一个链接到我基于我编码的控制器。 –

+0

请在发布表单后查看params散列表中的内容。您需要回答以下问题:信息是否从表单传入params散列?它是否从params散列获得新的Request对象?它是否从新的Request对象获取到数据库中? – Iain

+0

我添加了调试器输出。我可以让所有这些在控制台中工作,所以我倾向于表单不会将信息传递给params散列。 –