How can I display the name instead of an ID without the name column

点点圈 提交于 2020-01-17 08:20:13

问题


I have 3 tables, Activity, Activity_type and Itineraries. In activity type, I insert a new entry name => Testing => Example. In my activity, I can use the activity type I created earlier in my dropdown and type a location.

In creating a new itinerary there's a dropdown activity and user(who created the new itinerary and choose what activity for the specific itinerary) but it only shows the activity #1 and user #1. In my list and i think this is the reason why it only display the Activity #1 instead Testing.

Question:

1) In creating a new itinerary how can I change the activity #1 to the specific name?(example: In dropdown it should be Testing not Activity #1)

2)How can I display the name instead of an ID without the name column?

Activity table

activity_type_id
location_id

Itinerary table

title
created_by

Activity type table

name
description

NOTE: I also tried this below but nothing happened

Rails admin

config.model Itinerary do
list do

  field :title
  # field :schedule
  field :no_of_days
  field :activity
  field :user      
  # field :location      
  # field :activity_type
  # field :location_name
end
create do
  field :title    
end
object_label_method do 
    :activity_label_method
    end
  def activity_label_method
     "#{self.activity_type_id}" 

  end

end

Itinerary model

class Itinerary < ApplicationRecord
  belongs_to :user, foreign_key: :created_by
  belongs_to :activity
  has_many :activity_itineraries, foreign_key: :created_by

  validates :user, presence: true
  validates :title, presence: true, length: { maximum: 20 }
  validates :no_of_days, presence: true,
               :numericality => true


  def name
    activity_type.name
  end

end

回答1:


Define a method title or name in you model as follows,

def name
  activity_type.name
end

in rails_admin.rb, (you may need to modify based on your model associations, it is just the concept from comment from this thread)

field :activity_type_id, :enum do
  enum do
    Activity.collect {|act| [act.name, act.id]}
  end
end


来源:https://stackoverflow.com/questions/44234531/how-can-i-display-the-name-instead-of-an-id-without-the-name-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!