开发者

Rails: How to use select in formtastic with activeRecord

开发者 https://www.devze.com 2023-01-11 19:00 出处:网络
I\'m new to rails and I guess you can answer this question easily. What I got so far is = f.input :task, :as => :select, :collection => @tasks, :include_blank => true

I'm new to rails and I guess you can answer this question easily.

What I got so far is

= f.input :task, :as => :select, :collection => @tasks, :include_blank => true

where the tasks collection is defined by

Task.find(:all)

within in the controller.

This does in fact work, shows me a dropdown-list of all Tasks to select from and connects them. The problem here is, that the dropdown menu shows me values like

#<Task:0x123456789d1234>

Where do I define w开发者_C百科hat value is being displayed?


I believe you can use the :label_method to solve your problem...

f.input :task, :as => :select, :collection => @tasks, 
   :include_blank => true, :label_method => :title

where :title is what you want to show.

This may help a little more.


You can define a to_s method in the model:

class Task < ActiveRecord::Base

  def to_s
    title # the attribute to display for the label
  end

end
0

精彩评论

暂无评论...
验证码 换一张
取 消