如何在 Rails 中同时包含动态选择菜单和嵌套属性? [英] How to include both dynamic select menu and nested attributes together in rails?

查看:30
本文介绍了如何在 Rails 中同时包含动态选择菜单和嵌套属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑我有三个表用户、国家、州.我有一个页面来添加一个新用户,当我添加一个新用户时,我必须在选择框中列出国家/地区,在选择国家/地区时,多重选择框应该加载该国家/地区的状态,我应该能够选择所需的状态.

同样,我可以点击添加按钮添加另一个选择框并选择另一个国家并选择属于该国家的州等等.而且我知道这需要嵌套属性和动态选择菜单功能,但不知道如何将它们一起使用.

以下是我尝试过的

模型:

class Country 

class User 真的has_many :状态attr_accessible :用户名,:user_countries_attributes结尾

类状态

class UserCountry 

下面的图片也清楚地显示了我想要完成的事情

更新

<%= form_for(@user) do |f|%><% if @user.errors.any?%><div id="error_explanation"><h2><%=pluralize(@user.errors.count, "error") %>禁止保存此用户:</h2><ul><% @user.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>

<%结束%><div class="field"><%= f.label :username %><br/><%= f.text_field :用户名 %>

<%= f.fields_for :user_countries do |country|%><%= 渲染 "user_country_fields", :f =>国家%><%结束%><div class="add_variant"><%= link_to_add_fields "添加国家", f, :user_countries %></div><div class="actions"><%= f.submit %>

<%结束%>

更新 1:

<label>国家:</label><div id="field_country"><%= f.collection_select :country_id, Country.all, :id, :name, :prompt =>'选择国家'%>

<div id="field_state"><%= f.collection_select :state_id, State.all, :id, :name, {:prompt =>'选择状态'}, { :multiple =>真 } %>

<%= link_to_remove_fields "remove", f %></div>

解决方案

你可以使用nested_form (https://github.com/ryanb/nested_form) 以下列方式实现这一点,

 <%= nested_form_for(@user) do |f|%>....<%= f.fields_for :user_countries do |country|%><%= 渲染 "user_country_fields", :f =>国家%><%结束%><div class="add_variant"><%= f.link_to_add "Add" :user_countries %></div><div class="actions"><%= f.submit %>

同理,局部

 <%= f.link_to_remove "删除" %>

Consider i have three tables users, countries, states. I have a page to add a new user and when i add a new user i have to list the countries in the select box and on selecting the country the multiple select box should be loaded with the states of the country and i should be able to select the desired states.

Similarly i can click on the add button to add another select box and select another country and select states that belongs to that country and so on. And i know this needs nested attributes and dynamic select menu functionality but do not know how i can use these together.

The following are the ones that i tried

Models:

class Country < ActiveRecord::Base
    has_many :states
  attr_accessible :name
end

and

class User < ActiveRecord::Base
     serialize :state_id
    has_many :user_countries
    accepts_nested_attributes_for :user_countries, :allow_destroy => true
    has_many :state
    attr_accessible :username, :user_countries_attributes
end

and

class State < ActiveRecord::Base
    belongs_to :country
    attr_accessible :name, :country_id
end

and

class UserCountry < ActiveRecord::Base
    serialize :state_id
    belongs_to :users
    attr_accessible :country_id, :user_id, :state_id
end

Also the following image shows what i am trying to accomplish clearly

UPDATE

<%= form_for(@user) do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% @user.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :username %><br />
    <%= f.text_field :username %>
  </div>
      <%= f.fields_for :user_countries do |country| %>
        <%= render "user_country_fields", :f => country %>
      <% end %>
    <div class="add_variant"><%= link_to_add_fields "Add Country", f, :user_countries %></div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Update1:

<div class="entry_field">
    <label>Country :</label>
    <div id="field_country">

      <%= f.collection_select :country_id, Country.all, :id, :name, :prompt => 'Select Country' %>
    </div>

  <div id="field_state">

      <%= f.collection_select :state_id, State.all, :id, :name, {:prompt => 'Select State'}, { :multiple => true } %>
    </div>
    <%= link_to_remove_fields "remove", f %></div>

解决方案

You can use nested_form (https://github.com/ryanb/nested_form) to achieve this in following way,

 <%= nested_form_for(@user) do |f| %>
           ....

  <%= f.fields_for :user_countries do |country| %>
     <%= render "user_country_fields", :f => country %>
  <% end %>
  <div class="add_variant"><%= f.link_to_add "Add" :user_countries %></div>

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

Similarly, in partial

 <%= f.link_to_remove "Remove" %>

这篇关于如何在 Rails 中同时包含动态选择菜单和嵌套属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆