0

我有这个模型:

class User < ActiveRecord::Base
  # abstract
end

class Company < User

end

class Person < User

end

如何通过选择类型创建表单?

<%= simple_form_for(@user) do |f| %>
    <%# I would like to have a selector of classes ["Company","Person"] %>
    <%= f.input :type %>
    <%= f.input :name %>
<% end %>

在controleur中,我应该写什么代码?

@user = User.new(params[:user]) # Pb params[:user][:type] have no effect
4

2 回答 2

0

This is about as concise as you could make it:

@@classmap = { :person => Person, :company => Company }
@@Classmap[params[:user].delete :type].new(params[:user])

DO NOT convert params[:type] directly to a class name. That's a security hole waiting to be exploited.

于 2012-04-06T15:04:58.270 回答
0

您不能批量分配类型属性。你可以这样做:

type = params[:user].delete :type
@user = User.new(params[:user])
@user.type = type
@user.save
于 2012-04-06T13:45:26.483 回答