簡體   English   中英

RoR-簡單表格輸入

[英]RoR - Simple form inputs

使用用於Ruby on Rails的簡單表單gem,我有一個看起來像這樣的表單:

<%= simple_form_for(article, html: {class: 'form-vertical'}) do |f| %>
  ...
  <div class="field">
    <%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose a category"} %>
    <%= f.collection_select :subcategory_id, Subcategory.all, :id, :name, {prompt: "Choose a subcategory"} %>
    <%= f.input :title %>
    <%= f.input :content, input_html: { rows: 20 }%>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

有沒有一種方法可以使子類別輸入中的選項基於類別輸入中的選擇? 我的意思是,如果我有一個“食品”類別,那么我可以選擇一個子類別“蘋果”,但不能選擇“餐桌”。 這可能嗎?



類別模型:

class Category < ApplicationRecord
  has_many :articles
  has_many :subcategories
end

子類別模型:

class Subcategory < ApplicationRecord
  belongs_to :category
end

您想發生的事情不能僅憑滑軌來完成。 Rails對頁面更新執行操作。 您需要的是一個像jQuery這樣的JavaScript庫,以便在諸如.change()這樣的頁面上事件之后更改頁面的內容。

$("#article_category_id").change(function() {
  # Code to update content of subcategory
}

可以在此站點上找到示例,如下所示: 使用jQuery根據第一個選擇列表選項更改第二個選擇列表

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM