English 中文(简体)
我的科技创新概况模型中可以找到我的类型特征
原标题:Can t get my type attribute set in my Profile model for STI

有一个<代码>User模型,has_one :profile,而profile/code>模型则有一个e 单项表因先令栏。 我想要一个用户在签署时设定<代码> 类型,而我会遇到麻烦。

i m 在我的简历控制员中试图这样做:

def create
   @profile = Profile.find(params[:id])
   type = params[:user][:profile_attributes][:type]
   if type && ["Artist","Listener"].include?(type)
    @profile.update_attribute(:type,type)    
   end
end

页: 1 用户新观点:

<%= form_for(setup_user(@user)) do |f| %>
...
  <%= f.fields_for :profile do |t| %>
 <div class ="field">
    <%= t.label :type, "Are you an artist or listener?" %><br />
    <p> Artist: <%= t.radio_button :type, "Artist" %></p>
    <p> Listener: <%= t.radio_button :type, "Listener" %></p>
  </div>
  <% end %>
...
<% end %>   

以及我的申请助手:

def setup_user(user)
  user.tap do |u|
    u.build_profile if u.profile.nil?
  end
end

在创建用户时,我看不到<条码>类型。 仍然有<代码>nil的违约情况。 为什么如此,我如何做到这一点? 我赞赏一个守则实例。

最新资料:

这是我的<编码>中的相关守则。 用户模式:

has_one :profile 
accepts_nested_attributes_for :profile
before_create :build_profile

附录 2. 我有这个错误:WARNING: Can t Mass-assign Protect属性:

最佳回答

用以下方法取代你的创造方法:

def create
  profile_type = params[:user][:profile][:type].constantize
  if ["Artist", "Listener"].include? profile_type
    @profile = current_user.profile = profile_type.new
    current_user.save!
  else
    flash[:alert] = "Profile type not supported"
  end
end

最新资料:这实际上并不必要。 或许是有用的法典,但不是解决上述问题所必需的。

问题回答

它看起来像该标的吨数被拯救到数据库。 类似情况:

def create
  @profile = Profile.find(params[:id])
  type = params[:user][:profile_attributes][:type]
  if type && ["Artist","Listener"].include?(type)
    @profile.update_attribute(:type,type)    
  end
end

你的最后一个问题可以通过增加内容加以解决。

attr_accessible :type

“类型”是一种受保护的属性,你可以发出大规模转让受保护的属性。

该栏的类型保留在继承情况下储存该类别。 将表格一栏重新命名为“模仿_型号”。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签