Reputation: 13
I have a form with two collection_select fields, the first one is an easy one, it just gets a model named courses, which shows the course name, and of course returns the id of the selected course, the second is the one Im having problems with, its a collection_select of the similar courses a course may have.
The courses model:
class Course < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
attr_accessible :code, :credits, :name, :description, :active
has_many :similars, dependent: :destroy
has_many :similar_courses, through: :similars, source: :similar
end
The similar model:
class Similar < ActiveRecord::Base
attr_accessible :course_id, :similar_id
belongs_to :course
belongs_to :similar, class_name: "Course"
validates :similar_id, presence: true
validates :course_id, presence: true
end
this is the homologate model, the thing with this model is that a course must be approved or rejected if one wants to transfer classes and stuff like that:
class Homologation < ActiveRecord::Base
attr_accessible :homologate_by, :homologate_course, :student_id
belongs_to :user
end
this is the form Im having problems with:
<%= form_for(@homologation) do |f| %>
<%= render 'shared/error_messages', object: @homologation %>
<%= f.label :homologate_course %>
<%= f.collection_select :homologate_course, Course.find(:all), :id, :name, :prompt => "Select a Course" %>
<%= f.label :homologate_by %>
<%= f.collection_select :homologate_by, Similar.find(:all), :similar_id, :name, :prompt => "Select a Similar Course" %>
<div class="form-actions">
<%= f.submit "Create Homologation", class: "btn btn-large btn-primary" %>
</div>
<% end %>
</div>
Im getting the following error
http://dpaste.com/hold/827744/
the Bartolleti thing is the name of the course I want to be able to show, and that of course is not a method, but I dont know Why Im getting the error, I want to be able to show the names of the similar courses given the first collection field course...
Thank you for your help!
Upvotes: 1
Views: 1117
Reputation: 4930
From the doc of collection_select :
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
Then with your code you have Course.find(Similar.find_by_id(:similar_id)).name
as the text_method
, that is why you get this error message.
One solution could be to add a method on your Similar model
to get the similar course name :
def similar_name
similar.name
end
then you can use it as the text_method
in your collection_select
:
<%= f.collection_select :homologate_by, Similar.find(:all), :similar_id, :similar_name, :prompt => "Select a Similar Course" %>
Upvotes: 0
Reputation: 1815
Here the problem is ,
<%= f.collection_select :homologate_by, Similar.find(:all), :similar_id, :name, :prompt => "Select a Similar Course" %>
In this line :name is trying to find a record from course and the course name.
So, it better to write Similar.find(:all) in the controller.
Upvotes: 0
Reputation: 757
First, I'd recommend taking your "Similar" logic out of the form. So do your find.all on it in the controller and use it in your view as an instance variable @similar_list or so.
Second, have a look at options_from_collection_for_select for your form here:
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_from_collection_for_select
Let me know if this helps you at all.
Upvotes: 0