Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails中的静态嵌套元素_Ruby On Rails_Nested Forms - Fatal编程技术网

Ruby on rails Rails中的静态嵌套元素

Ruby on rails Rails中的静态嵌套元素,ruby-on-rails,nested-forms,Ruby On Rails,Nested Forms,我有一个“兴趣”列表,系统中的每个用户都有能力对其进行评分。管理员可以随时添加/删除兴趣。当用户去编辑他们的帐户时,我想显示系统中所有兴趣的列表,以及一个带有1..5值的下拉列表。我想知道我是如何设置的 使用accepts\u nested\u attributes for似乎不起作用,因为当我使用字段\u for时,它希望为已保存的每个兴趣创建表单。我想要的是显示每个兴趣,在保存时,如果用户以前对兴趣进行过评级,它会更新该值,如果以前没有对其进行过评级,则添加一个新条目 当前用户: has

我有一个“兴趣”列表,系统中的每个用户都有能力对其进行评分。管理员可以随时添加/删除兴趣。当用户去编辑他们的帐户时,我想显示系统中所有兴趣的列表,以及一个带有1..5值的下拉列表。我想知道我是如何设置的

使用accepts\u nested\u attributes for似乎不起作用,因为当我使用字段\u for时,它希望为已保存的每个兴趣创建表单。我想要的是显示每个兴趣,在保存时,如果用户以前对兴趣进行过评级,它会更新该值,如果以前没有对其进行过评级,则添加一个新条目

当前用户:

  has_many :user_interests, :dependent => :destroy
  has_many :interests, :through => :user_interests, :foreign_key => :user_id  

  accepts_nested_attributes_for :user_interests
当前用户兴趣:

  belongs_to :user
  belongs_to :interest
  has_many :user_interests, :dependent => :destroy
  has_many :users, :through => :user_interests, :foreign_key => :interest_id, :dependent => :destroy
目前的利益:

  has_many :user_interests, :dependent => :destroy
  has_many :users, :through => :user_interests, :foreign_key => :interest_id, :dependent => :destroy

我最终只是翻转循环,所以它首先在兴趣中循环,然后为每个兴趣创建表单元素

<% Interest.all.group_by(&:interest_category).each do |category, interests| %>
    <p>
        <h4 id="interests"><%= category.title %></h4>
        <ul>
            <% interests.each do |interest| %>
            <% user_interest = @current_user.user_interests.find_by_interest_id(interest) || @current_user.user_interests.new(:interest_id => interest.id) %>
                <% form.fields_for "user_interests[#{interest.id}]", user_interest do |user_interests_form| %>
                    <li><%= user_interests_form.select :rating, options_for_select([1, 2, 3, 4, 5], user_interest.rating || ""), {:prompt => "-- Select One --"} %> <%= interest.title %></li>
                <% end %>
            <% end %>
        </ul>
    </p>
    <% end %>