模型关联并从DB获取值到视图 - ruby​​ on rails

时间:2014-02-28 09:50:55

标签: ruby-on-rails ruby activerecord

以下是我的“通讯”表的架构,

id | UserID | CommunicationMode | CommunicationDetail

UserID是“Communications”表中的外键,下面是users表的架构:

id | First_name | email

我想获取值“CommunicationMode”和“CommunicationDetail”列并将其放入text_field:

控制器:

def new
   @user_communication=User.where(UserID: current_user.id)
end

以下是沟通模式:

class Communication < ActiveRecord::Base
    belongs_to :user
    validates :UserID, presence: true
end

以下是用户模型:

class User < ActiveRecord::Base
  has_many :educations, dependent: :destroy
  has_many :professions, dependent: :destroy
  has_many :communications, dependent: :destroy
  has_many :availabilities,dependent: :destroy
    before_save { self.email = email.downcase }
    before_create :create_remember_token
    validates :First_Name, presence: true,length: {maximum: 50}
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                uniqueness:  { case_sensitive: false }
    validates :password, length: { minimum: 6 }
    has_secure_password


    def User.new_remember_token
        SecureRandom.urlsafe_base64
    end

    def User.encrypt(token)
        Digest::SHA1.hexdigest(token.to_s)
    end

    private

    def create_remember_token
        self.remember_token = User.encrypt(User.new_remember_token)
    end
end

以下是new.html.erb页面的代码:

 <table>
          <tr style="text-align: left; vertical-align: middle;">
             <td style="font-size: large; color: #212121;">
                Email:
             </td>
             <td style="font-size: large; color: #212121;">
                <%= text_field :email,:placeholder => "Email" %>
             </td>
          </tr>
          <tr style="text-align: left; vertical-align: middle;">
             <td style="font-size: large; color: #212121;">
                Phone:
             </td>
             <td style="font-size: large; color: #212121;">
                <%= text_field :phone, :placeholder => "Phone" %>
             </td>
          </tr>
          <tr style="text-align: left; vertical-align: middle;">
             <td style="font-size: large; color: #212121;">
                Skype:
             </td>
             <td style="font-size: large; color: #212121;">
                <%= text_field :skype,:placeholder => "Skype" %>
             </td>
          </tr>
          <tr style="text-align: left; vertical-align: middle;">
             <td style="font-size: large; color: #212121;">
                Website:
             </td>
             <td style="font-size: large; color: #212121;">
                <%= text_field :website,:placeholder => "Website" %>
             </td>
          </tr>
          <tr style="text-align: left; vertical-align: middle;">
             <td style="font-size: large; color: #212121;">
                Twitter:
             </td>
             <td style="font-size: large; color: #212121;">
                <%= text_field :twitter, :placeholder => "Twitter" %>
             </td>
          </tr>
    </table>

我如何从DB获取值并将其放入text_field?

1 个答案:

答案 0 :(得分:0)

如果您在@user_communication中拥有有效用户,则可以按照以下说明进行通信。

<强>控制器:

unless @user_communication == nil
  @user_communications = @user_communication.communications
end

然后从模型规范中可以看出,一个用户可以进行多次通信。

因此,在视图中,只需循环浏览@user_communications并从每次通信中获取字段值CommunicationModeCommunicationDetail

查看:

<% unless @user_communications.blank? %>
  <% @user_communications.each do |user_com| %>

     <tr style="text-align: left; vertical-align: middle;">
         <td style="font-size: large; color: #212121;">
            Communication Mode:
         </td>
         <td style="font-size: large; color: #212121;">
            <%= text_field :cmode, :placeholder => "Communication Mode", :value => user_com.CommunicationMode %>
         </td>
      </tr>

      <tr style="text-align: left; vertical-align: middle;">
         <td style="font-size: large; color: #212121;">
            Communication Mode:
         </td>
         <td style="font-size: large; color: #212121;">
            <%= text_field :cdetail, :placeholder => "Communication Detail", :value => user_com.CommunicationDetail %>
         </td>
      </tr>

  <% end %>
<% end %>

希望有所帮助:)