uday
uday

Reputation: 8720

Rails- in place editing using best_in_place gem for an array collection defined in model itself

I have seen RailsCasts#302 which describes about the in-place editing using the best_in_place gem. Over there in for gender option Ryan uses the array inside the show.html.erb and makes it a dropdown box(see the gender section where he explicitly defines an array).

<p>
  <b>Gender:</b>
  <%= best_in_place @user, :gender, type: :select, collection: [["Male", "Male"], ["Female", "Female"], ["", "Unspecified"]] %>
</p>

But what I want is I have defined an array inside the Model itself like: (because my array elements are not simple and short in count)

For eg:

user.rb

class User < ActiveRecord::Base
  def authencity_types
    ['Asian', 'Latin-Hispanic', 'Caucasian']
  end
end

How am I going to use this array elements as dropdown using the best_in_place syntax.

PS: I did try something like this

<% @users.each do |user| %>
  <%= best_in_place user, :authencity, type: :select, :collection => User::authencity_types  %>
<% end %>

But it says undefined method authencity_types

Upvotes: 2

Views: 2006

Answers (1)

ryanb
ryanb

Reputation: 16287

You're defining an instance method on the User model, so try this.

<% @users.each do |user| %>
  <%= best_in_place user, :authencity, type: :select, :collection => user.authencity_types  %>
<% end %>

Alternatively you can define that as a class method like this.

class User < ActiveRecord::Base
  def self.authencity_types
    ['Asian', 'Latin-Hispanic', 'Caucasian']
  end
end

Or you may want to consider using a constant if it does not need to be dynamic.

Upvotes: 4

Related Questions