English 中文(简体)
Ruby On Rails 多选择器, 多个和多个关系
原标题:Ruby On Rails multi selector with many to many relation

我有两个模特

项目 已经拥有_ and_ belios_to_ many: 用户

拥有_和属于_Iongs_to_many: 项目的 用户

in project view I have a form that has a selector where I want to try 和assign multiple users to a project.

<%= select_tag("project[users]",  options_for_select(@users, @project_users), {:multiple=>true, :size=>6} )%>

在我的工程控制器中, 选择中使用的变量是

@project_users = @project.users.collect { |user| user.id}
@users = User.all.collect { |user| ["#{user.first_name} #{user.last_name}", user.id] }

以显示

<select id="project_users" multiple="multiple" name="project[users][]" size="6">
   <option value="#<User:0x007f567cb7f078>">User1</option>
   <option value="#<User:0x007f567cb7e9c0>">User2</option>
</select>

问题是,这不等于

@some_project << [User(#), User(#)]

(注用户(#)代表类实例))

代之以其相当于

@some_project << ["1", "2"]

问题是用户实例被转换为字符串, 而不是再次返回到实例 。

Which does not work 和will throw an error as

ActiveRecord::AssociationTypeMismatch in ProjectsController#update
User(#70004716784160) expected, got String(#4266680)

我怎样才能使这个工作正确?

最佳回答

在您的用户模式中 :

def full_name
  [first_name, last_name].join(" ")
end

在您的控制器中

@users = User.all

你们认为:

<%= select_tag( project[user_ids] , options_from_collection_f或_select(@users,  id ,  full_name ), { :multiple => true, :size => 6 }) %>

那样你就可以使用

@project.user_ids = params[:project][:user_ids]

@project.update_attributes(params[:project])

f或 assignment.

问题回答

无法发送实例。 它可以在窗体和提交器的转换之间改变。 转换为字符串是一种操作方式, 因为它指对象为独有的 id( 如内存地址), 而不是属性或数据库标识符 。 由于它不是活动 Record 的一部分, 而是 红宝石 对象基础 。 当返回窗体的数据时, 示例不在内存中, 因此您无法将它转换为反向, 即使有可能的话 。

坚持古老的方式:

@project_users = @project.users.collect { |user| user.id}
@users = User.all.collect { |user| ["#{user.first_name} #{user.last_name}", user.id] }

当提交数据时:

@users = params[:project][:users].map{|a| User.find(a) }




相关问题
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: ...

热门标签