【问题标题】:How to display a models attribute in a read only text area如何在只读文本区域中显示模型属性
【发布时间】:2020-05-06 22:08:05
【问题描述】:

作为背景,我正在使用带有 Devise gem 的 Ruby on Rails。用户属性之一是一个名为 bio 的字符串。我的目标是让个人资料页面在只读文本框中显示简历。我觉得文本框显示的线条给人一种很好的美感。我也在使用引导程序。

页面控制器:

def profile
      user=User.all
      respond_to do |format|
        format.html { render :profile, locals: { user: user } }
      end
    end

我的 profile.html.erb 的代码

<div class="row">
   <div class="col-5">
      <%= current_user.text_area :bio, readonly: true %>%
   </div>

我得到的错误是 Pages#profile 中的 NoMethodError - 未定义的方法 `text_area'

【问题讨论】:

  • text_area 不是用户的属性,是表单的属性。使用这个&lt;%= f.text_area :bio, readonly: true %&gt;
  • @Naveed 它返回一个错误:ActionView::Template::Error (undefined local variable or method `f'
  • 我认为你没有在表单中使用它,在这种情况下使用这个语法&lt;%= text_area_tag :bio, current_user.bio, readonly: true %&gt;

标签: html ruby-on-rails devise


【解决方案1】:

使用 css 来获得与 text_area 相同的结果。将您的 current_user.bio 属性放在 &lt;p&gt;&lt;div&gt; 元素中,并为该元素指定特定宽度,以便线路可以中断。例如:

<div class="row">
    <div class="col-5">
        <p class='specific-width'> <%= current_user.bio %> </p>
    </div>
</div>

然后在css文件中:

.specific-width{
    width: 30px;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    相关资源
    最近更新 更多