Ruby on rails 我应该如何处理这个Rails用例?

Ruby on rails 我应该如何处理这个Rails用例?,ruby-on-rails,Ruby On Rails,我有一个练习模型,其中包含以下列: model Exercise do string :name calories_burned :float end 我希望当用户添加练习时,能够以这种方式进行: 如果以前的练习存在 显示包含已添加的现有元素名称的select元素 显示一个复选框,允许添加一个新的,切换输入 字段转换为文本字段 其他的 显示文本字段 问题是,我不知道该如何看待这一点。以下是else案件的处理方式: <div class="field"> <%= f

我有一个练习模型,其中包含以下列:

model Exercise do
  string :name
  calories_burned :float
end
我希望当用户添加练习时,能够以这种方式进行:

如果以前的练习存在 显示包含已添加的现有元素名称的select元素 显示一个复选框,允许添加一个新的,切换输入 字段转换为文本字段 其他的 显示文本字段 问题是,我不知道该如何看待这一点。以下是else案件的处理方式:

<div class="field">
  <%= f.label :name %><br />
  <%= f.text_field :name %>
</div>
我现在有这样的东西:

  <div class="field">
    <% if @exercise_added %>
        <div id="select_div">
            <%= select_tag :name,options_for_select(@exercise_added) %>
            <input type="checkbox" name="custom_type_checked" id="which_type">New type?</input>
        </div>
    <% end %>
    <div id="regular_field">
        <%= f.label :name %><br />
        <%= f.text_field :name %>
    </div>
  </div>
在@exercise_中,我添加了数据库中所有练习的名称列表。处理此问题的最佳/最干净的方法是什么


编辑:现在,我有一个textfield和一个select,通过使用Javascript,我正在更改元素的名称并隐藏另一个。到目前为止,它仍在运行,但我仍然对是否存在其他方法感兴趣。

您应该仔细查看和了解。祝你好运

默认情况下,我会显示选择框和按钮,文本框隐藏,除非变量@show\u textbox为true。大概是这样的:

<div class="field">
    <div id="select_div">
        <%= select_tag :name,options_for_select(@exercise_added) %>
        <%= f.submit "New Exercise" %>
    </div>
    <div id="regular_field" <%= hidden_unless @show_textbox %> >
        <%= f.label :name %><br />
        <%= f.text_field :name %>
    </div>
</div>
然后,在我的控制器中,检查是否按下了新的练习按钮。如果是这样,请将@show_textbox设置为true,然后重新呈现新表单

您可以签入控制器:name字段中是否包含任何文本,并使用该文本覆盖选择框

这应该在没有javascript的情况下工作。我会添加一些jQuery,用链接或复选框替换按钮,并使用连接到显示文本框的函数的单击处理程序,即$'regular\u field'.toggle


我没有处理隐藏选择框的问题。事实上,我认为最好还是留着它。无论如何,您可以使用类似的方法隐藏它。

您可以检查添加的数组@exercise\u是否为空,并相应地显示选择字段或文本字段

      <div class="field">
        <% if !@exercise_added.empty? %>
            <div id="select_div">
                <%= select_tag :name,options_for_select(@exercise_added) %>
                <input type="checkbox" name="custom_type_checked" id="which_type">New type?</input>
            </div>
        <% else %>
        <div id="regular_field">
            <%= f.label :name %><br />
            <%= f.text_field :name %>
        </div>
        <%end%>
      </div>

如果你用两种形式呢?一个表单用于处理在@exercise_中添加练习时的情况,另一个表单用于处理新练习的创建。这甚至可以归结为POST和PUT之间的区别,这取决于从下拉列表中提交练习后您所做的操作


我很想看到更多的代码和控制器,因为看起来这个表单可能是嵌套的?

但是我没有不同的模型关联。这仍然适用吗?抱歉,我误解了你的问题。我以为你有一个用户类,有很多:练习。你应该听从@Mahesh的建议。你可以替换@你加了什么,空的?添加了@exercise\u。有吗?或者除非@exercise_添加了.empty?有人能解释为什么他们否决了这个吗?我想知道是出于我自己的目的,因为我使用了这个策略。
def create
  # .....

  # did we get here because the user pressed "New Exercise"?
  if params[:commit].eql?("New Exercise")
    @show_textbox = true
    # probably some other code to replicate what happens in your #new action
    render :action => new
  end

  # ....
end
      <div class="field">
        <% if !@exercise_added.empty? %>
            <div id="select_div">
                <%= select_tag :name,options_for_select(@exercise_added) %>
                <input type="checkbox" name="custom_type_checked" id="which_type">New type?</input>
            </div>
        <% else %>
        <div id="regular_field">
            <%= f.label :name %><br />
            <%= f.text_field :name %>
        </div>
        <%end%>
      </div>