Search code examples
ruby-on-railsformspolymorphic-associationsactionviewactioncontroller

Rails: Form inputs not saving after submit


why wont anything entered into this form save?

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

my controller is based on this
https://github.com/railscasts/154-polymorphic-association-revised/blob/master/blog-after/app/controllers/comments_controller.rb

request.rb
class Request < ActiveRecord::Base

  attr_accessible :status

  belongs_to :requestable , polymorphic: true
  belongs_to :profile

  validates :status,  presence: true

end

This is being produced by my 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'

Solution

  • Take a look at what's in your params hash. The status field is probably in something like params[:request][:status]. Assuming standard activerecord-y stuff, you want to pass the hash for the whole request object to .new.