【问题标题】:Rails - first item in form select controls after edit/updateRails - 编辑/更新后表单选择控件中的第一项
【发布时间】:2013-06-22 06:09:43
【问题描述】:

当用户编辑用户事件时,他们将被重定向到用户页面,该页面显示指向其用户事件的链接。

更新后,如果他们再次单击同一链接进行编辑,他们会在编辑表单中看到选择控件的第一个值,而不是他们在上次编辑中选择的新值。

新数据实际上正在保存到数据库中。

这只发生在选择控件上。对文本字段和复选框的更改可以正常显示新值。

所以选择的第一项总是显示。选择控件中未选择用户选择的值。

一如既往,提前谢谢...

<%= f.label      :title, "Title (max 150 characters)" %>
<%= f.text_area :title %>

<div>
  <%= f.label      :user_event_type, "Event type" %>
  <%= f.select :user_event_type, options_for_select([['Sales Meeting',1],['Training',2],['Legal Briefing',3]]), {prompt: "select event type"} %>
</div>

  def edit
    @user_event = UserEvent.find(params[:id])    
  end

  def update
    @user_event = UserEvent.find(params[:id])    
    if @user_event.update_attributes(params[:user_event])
      flash[:success] = "Event successully updated"
      redirect_to user_url(current_user)
    else
      render 'edit'
    end
  end  

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    这篇文章给出了答案:

    Ruby on Rails: Why a select box does not show the current object value?

    只需删除 options_for_select,并将二维数组作为第二个参数传递给 select 方法,它会自动分配选定的对象。

    之前:

    <%= f.select :state, options_for_select([['Alaska',1],['Florida',2],['Texas',3]]) %>
    

    之后:

    <%= f.select :state, [['Alaska',1],['Florida',2],['Texas',3]] %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多