简体   繁体   English

如何修改 Rails 中的嵌套参数?

[英]How to modify nested params in Rails?

I am working on similar app.I have 3 models: User, Group and Relation.I want to make a form where a logged in user can create a group and invite a college(other user registered in db).I am using has_many through association.Here are these models:我正在开发类似的应用程序。我有 3 个模型:用户、组和关系。我想制作一个表单,登录用户可以在其中创建一个组并邀请一所大学(在数据库中注册的其他用户)。我正在使用 has_many协会。以下是这些模型:

class Group < ApplicationRecord
  has_many :relations
  has_many :users, through: :relations

  accepts_nested_attributes_for :relations
end 

class Relation < ApplicationRecord
  belongs_to :group
  belongs_to :user
end

class User < ApplicationRecord
  attr_accessor :remember_token


  has_many :transactions, dependent: :destroy
  has_many :relations
  has_many :groups, through: :relations
  <some validation >

end

My GroupsController我的群组控制器

class GroupsController < ApplicationController

  def new
    @group = Group.new
    @group.relations.build
  end
  
  def create
    @group = Group.new(groups_params)
    if @group.save
      flash[:success] = "Group has been created!"
      redirect_to root_path
    else  
      flash[:danger] = "Something went wrong!"
      render 'groups/new'
    end 
  end 


  private
  def groups_params
    params.require(:group).permit(:group_name, :group_from, :group_to, 
                                  relations_attributes: Relation.attribute_names.map(&:to_sym) )
                                  #Relation.attribute_names.map(&:to_sym) this grabs all the column names
                                  #of the relations tabel, puts it in the array and maps each element with hash
                                  #[:id, :group_id, :user_id, :created_at, :updated_at] 
  end

end

And my view for "new" action我对“新”行动的看法

<%= provide(:title, "New Group") %>
<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for @group do |group| %>
      <%= group.label :group_name, "Group name"%>
      <%= group.text_field :group_name %>

      <%= group.label :group_from, "Group from"%>
      <%= group.date_field :group_from %>

      <%= group.label :group_to, "Group to"%>
      <%= group.date_field :group_to %>
      <!-- :relations is a symbol for assosiation between the group and user -->
      <%= group.fields_for :relations do |relations_form| %>
        <%= relations_form.label :user_id, "Member #1" %>
        <%= relations_form.text_field :user_id %>
      <% end %>

      <%= group.submit "Create a group!" %>
    <% end %>
  </div>
</div>

screenshot: https://i.stack.imgur.com/u1LDX.png截图: https://i.stack.imgur.com/u1LDX.png

With this code a logged in user is able to create group record and relation record simultaneously but it has to pass an id of user it wants to invite to a group(for example: "7" not "John" - the name of the user with id 7) What I want to achieve is to get the id of the user names pass in the "Member #1" field.Example: 1.A logged in user puts in the "Member #1" field: "John" 2.Some function to get the id of the "John" - modify params 3.if "John" exists in users tabel then save the group -> create a group record and relation record.使用此代码,登录用户能够同时创建组记录和关系记录,但它必须传递要邀请到组的用户的 ID(例如:“7”而不是“John” - 用户名id 7)我想要实现的是在“成员#1”字段中获取用户名的ID。示例:1.登录用户放入“成员#1”字段:“John” 2 .Some function 获取“John”的 id - 修改参数 3.如果用户表中存在“John” 然后保存组 -> 创建组记录和关系记录。

I think I have to modify nested params, but I do not know how to do this.我想我必须修改嵌套参数,但我不知道该怎么做。 Thanks for any help, Lukas感谢您的帮助,卢卡斯

Instead of an input text field, you should try out a select dropdown where you can choose the existing members.您应该尝试使用 select 下拉菜单,而不是输入文本字段,您可以在其中选择现有成员。 Like this it will show the name to the user but it will select the ID which will be sent to the controller.像这样它将向用户显示名称,但它将 select 将发送到 controller 的 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM