简体   繁体   English

Ruby on Rails:获取具有模型中未包含的属性的参数

[英]Ruby on Rails:Get params with attribute which not in model

I have a field which is not in my model, but it is in my _form.html.erb . 我有一个不在模型中的字段,但在我的_form.html.erb Now i want to get it from form, but i get this error : 现在我想从表单中获取它,但出现此错误:

undefined method 'description' for Object

My controller: 我的控制器:

def job_params
@job_params ||=params.permit(:description) # (description is not an attribute in job model ).

How can i get description ?? 我如何获得描述? My form: 我的表格:

<%= form_for(@job) do |f| %>
<div class="field">
  <%= f.label :description %><br>
  <%= f.text_field :description %>
</div>

use attr_accessor in your model for description . 在模型中使用attr_accessor进行description

class DummyModel < ActiveRecord::Base
  attr_accessor: description
end

Reference 参考

You might also look at this one. 您可能还会看到一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM