【问题标题】:rails 4 simple_form as: hidden don't sendrails 4 simple_form as:隐藏不发送
【发布时间】:2014-05-10 16:36:32
【问题描述】:

我有这个代码:

<%= simple_form_for(@uniform) do |f| %>
    <%= f.error_notification %>
    <%= f.association :employee, as: :hidden, selected: @employee %>
    <%= f.association :piece %>
    <%= f.button :submit, "Add new piece" %>
<% end %>

但是,隐藏字段“:employee”,不发送选中的员工,只有当这个“f.association”没有隐藏时才发送。

我的控制器是默认脚手架:

def create
    @uniform = Uniform.new(uniform_params)

    respond_to do |format|
        if @uniform.save
            format.html { redirect_to @uniform, notice: 'Uniform was successfully created.' }
        else
            format.html { render action: 'new' }
        end
    end
end

我需要发送这个“f.association :employee”的值,但不显示在浏览器中。

【问题讨论】:

  • 试试&lt;%= f.input :employee, as: :hidden, selected: @employee %&gt;
  • f.input don't have 'selected:' 此代码也有效:

标签: ruby-on-rails field simple-form hidden


【解决方案1】:

只发送@employee.id 吗?

像这样

<%= f.input :employee_id, as: :hidden %>

您可以立即在控制器中设置该值,例如

def new
  @uniform = Uniform.new
  @uniform.employee = @employee
  ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 2014-07-28
    相关资源
    最近更新 更多