Rails如何写“创建”在HABTM模型控制器中的行动 [英] Rails How To Write "Create" Action in HABTM Model Controller

查看:123
本文介绍了Rails如何写“创建”在HABTM模型控制器中的行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Rails比较陌生,我在尝试使用在它们之间有HABTM关联的模型时遇到了巨大的困难。



这些是我的模型:



challenge.rb:


$ b b

  class Challenge< ActiveRecord :: Base 
has_and_belongs_to_many:skills
attr_accessible:description,:name,:relevant_content,:solutions,:skills
end

skill.rb:

  class Skill< ActiveRecord :: Base 
has_and_belongs_to_many:challenges

attr_accessible:name
end


b $ b

我还使用以下迁移创建了一个名为chall_skills的连接表:

  class CreateChallengesSkills< ActiveRecord :: Migration 
def up
create_table:challenges_skills,:id => false do | t |
t.integer:challenge_id
t.integer:skill_id
end
end

def down
drop_table:challenges_skills
end
end

我使用下面的助手选择我的挑战所需的技能:

 <%= collection_select(:challenge,:skills,Skill.all,:id,:name,{},{ :multiple =>true})%> 

HTML:

 < select id =challenge_skillsmultiple =multiplename =challenge [skills] []> 
< option value =2> Pesquisa< / option>
< option value =3> Senso Critico< / option>
< option value =4> Criatividade< / option>
< option value =5> Colaboracao< / option>
< option value =6> Comunicacao< / option>
< option value =7> Proatividade< / option>
< / select>

这是我的params哈希:

  {utf8=>✓,
authenticity_token=>n0ggK8eE7vjh + qY33lYbNLJtZW6Sz7LyM2IRVbAPwhM =,
challenge=& =>我的说明,
skills=> [,3,4],
name=>测试挑战 brelevant_content=>我的相关内容,
solutions=>我的解决方案},
commit=>创建挑战!
}

错误:
Skill(#70228688111620)expected,got String #14953820)



问题是,我不知道如何处理技能哈希,将其转换为我的挑战控制器创建操作中的对象数组。
我知道这应该是简单的,但我找不到一个pratical解决方案。
我想知道如何编写代码以将技能集合与挑战控制器中的挑战实例相关联。



ChallengesController#create

  def create 

@challenge = Challenge.new(params [:challenge])

感谢

解决方案 div>

我的建议是远离HABTM,切换到 has_many:通过它将更容易使用,更灵活地更改(添加属性)



请参阅



http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association





http://stackoverflow.com/ a / 11601236/631619



这是一个很好的比较:



http://railscasts.com/episodes/47-two-many-to-many a>


I'm relatively new to Rails and I am having an enormous difficulty trying to work with models that have HABTM associations between them.

These are my models:

challenge.rb:

class Challenge < ActiveRecord::Base
    has_and_belongs_to_many :skills
    attr_accessible :description, :name, :relevant_content, :solutions, :skills
end

skill.rb:

class Skill < ActiveRecord::Base
    has_and_belongs_to_many :challenges

    attr_accessible :name
end

I've also created a join table called challenges_skills using the following migration:

class CreateChallengesSkills < ActiveRecord::Migration
    def up
        create_table :challenges_skills, :id => false do |t|
        t.integer :challenge_id
        t.integer :skill_id
        end
    end

    def down
    drop_table :challenges_skills
    end
end

And I'm using the helper below to to select my challenge required skills:

<%= collection_select(:challenge, :skills, Skill.all, :id, :name, {}, {:multiple => "true"}) %>

HTML:

<select id="challenge_skills" multiple="multiple" name="challenge[skills][]">
    <option value="2">Pesquisa</option>
    <option value="3">Senso Critico</option>
    <option value="4">Criatividade</option>
    <option value="5">Colaboracao</option>
    <option value="6">Comunicacao</option>
    <option value="7">Proatividade</option>
</select>

Here's my params hash:

{"utf8"=>"✓",
 "authenticity_token"=>"n0ggK8eE7vjh+qY33lYbNLJtZW6Sz7LyM2IRVbAPwhM=",
 "challenge"=>{"description"=>"My description.",
 "skills"=>["", "3", "4"],
 "name"=>"Test Challenge",
 "relevant_content"=>"My relevant_content",
 "solutions"=>"My solutions"},
 "commit"=>"Create Challenge!"
}

Error: Skill(#70228688111620) expected, got String(#14953820)

The problem is that I don't know how to handle the skills hash, turn it into an object array inside my challenges controller create action. I know it should be something simple but I cannot find a pratical solution. I want to know how to code to associate the skills collection to my challenge instance inside challenges controller.

ChallengesController#create

def create

    @challenge = Challenge.new(params[:challenge])

    ???

Thanks

解决方案

My advice is to stay away from HABTM and switch to has_many :through it'll be easier to work with and more flexible to change (add attributes) over time.

See

http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association

and

http://stackoverflow.com/a/11601236/631619

and here's a nice comparison of the two:

http://railscasts.com/episodes/47-two-many-to-many

这篇关于Rails如何写“创建”在HABTM模型控制器中的行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆