0
In a Rails project, I created a Scaffold Responsability and consequently Rails created the whole basic structure of this Scaffold.
I created a has_and_belongs_to_many relationship between a model responsability
and a model Knowledge
, and there will be displayed several check_box with the knowledges available for a certain Reponsability. If I put this code in the form
, works, but when creating a view nested_knowledges.html.erb the content with check_box’s is not being displayed. Any idea what might be going on?
responsabilities_controller.Rb
def knowledges
params[:responsability][:knowledge_ids] ||= []
render :nested_knowledges
@responsability = Responsability.all
@knowledge = Knowledge.all
end
model Reponsabilitiy.Rb
class Responsability < ActiveRecord::Base
belongs_to :setor
has_and_belongs_to_many :knowledges
validates_presence_of :nome, :setor_id
validates_uniqueness_of :nome
accepts_nested_attributes_for :knowledges
attr_accessible :atribuicoes, :experiencia, :formacao, :id, :missao, :nome, :setor_id, :knowledge_ids
default_scope order('nome ASC')
end
model Knowledge.Rb
class Knowledge < ActiveRecord::Base
has_and_belongs_to_many :responsabilities
validates_presence_of :nome
validates_uniqueness_of :nome
accepts_nested_attributes_for :responsabilities
attr_accessible :id, :nome, :responsability_ids
default_scope order('nome ASC')
end
View nested_knowledges.html.erb
<h1> <%= link_to l(:lbl_responsability), responsabilities_path %> » <%= link_to l(:lbl_knowledge), knowledges_path %> » </h1>
<%= render :partial => 'tabs' -%>
<div class="box">
<% for knowledge in Knowledge.find(:all) %>
<%= check_box_tag "responsability[knowledge_ids][]", knowledge.id, @responsability.knowledges.include?(knowledge) %>
<%= knowledge.nome %><br/>
<% end %>
</div>
2 days. Knowledge.find(:all) should be in your controller, not in the view. Something like @Knowledge = Knowledge.all
– Alex Takitani
In the view, instead of using for, use each. Ex.: <% @knowledges.each do |Knowledge| %>
– Alex Takitani