Creating a new view in a Controller

Asked

Viewed 552 times

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 %> &#187; <%= link_to l(:lbl_knowledge), knowledges_path %> &#187; </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

  • In the view, instead of using for, use each. Ex.: <% @knowledges.each do |Knowledge| %>

1 answer

1

Create a new action for your controller this way:

def acao
end

And then create app/views/seu_controlador/acao.html.erb and paste your code into it.

Then edit your routes.rb doing the following:

resources :seu_controlador do
  member do
    get 'acao'
  end
end

This should create the route /seu_controlador/acao GET type, pointing to your page.

Always remember to search for your questions on Official Guide.


Updating

Try with the changes below.

def knowledges
  params[:responsability][:knowledge_ids] ||= []

  # coloque isso antes do :render
  # note que em mudei o nome das variáveis para o plural, para seguir as convensões do Rails
  @responsabilities = Responsability.all
  @knowledges = Knowledge.all

  render :nested_knowledges
end

HTML:

<% @knowledges.each do |knowledge| %>
  <%= check_box_tag "responsability[knowledge_ids][]", knowledge.id,  @responsabilities.knowledges.include?(knowledge) %>
  <%= knowledge.nome %><br/>
<% end %>
  • I did the whole process described above @Andrey. Even the part where I reviewed the Views worked. When I put my code on the created page, it returns an error '< Undefined method `Knowledge' for nil:Nilclass >'

  • @Brunofonseca Try once to use Knowledge.all.each do |k| (...) end instead of for knowledge in (...).

  • I used the search the way you told me. It works as long as I take the part @responsability.knowledges.include?(knowledge). If I leave , it presents the same error ... I tried Create a @reponsability object and a @Knowledge in Action that I created in Controller no more success. I made a few other attempts using knowledges etc... and he makes the same mistakes. I don’t think it’s problems with relationships, any idea of what might be?

  • 1

    @Brunofonseca The error you quoted apparently indicates that the object @responsability is as nil. Please edit your question including the code of your controller action and also the model classes, especially the relationship part (has_and_belong_to_many, etc.).

  • Editing was done as requested @Andrey. I hope there is some practical way to make this process.

  • @Brunofonseca I edited my question.

  • ActionView::Template::Error (undefined method 'each' for nil:NilClass):&#xA; <%= render :partial => 'tabs' -%>&#xA; <div class="box">&#xA; <% @knowledges.each do |knowledge| %>&#xA; <%= check_box_tag "responsability[knowledge_ids][]", knowledge.id, @responsabilities.knowledges.include?(knowle&#xA; <%= knowledge.nome %> It is returning this result on the console...

  • @Brunofonseca This indicates that the variable @knowledges is as nil, however it is difficult to know why, since in the controller we are assigning @knowledges = Knowledge.all.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.