在Rails中使用一种表单更新多个模型

问题描述

我是Rails的新手,他试图构建一个允许用户完成并提交表单的表单。表单完成了他们的个人资料。我希望表单更新library(unglue) unglue_unnest( df,string_col,c("{}{date1=\\d+\\.\\d+\\.\\d+}-{date2=\\d+\\.\\d+\\.\\d+}{}","{}{date1=\\d+\\.\\d+\\.\\d+}{}"),remove = FALSE) #> string_col date1 date2 #> 1 my string a-maxeka UU-AA-19.01.03-20.01.22-bamdanool 19.01.03 20.01.22 #> 2 my string a-maxeka UU-AA-20.01.08-20.04.01-jdasdasd 20.01.08 20.04.01 #> 3 asdad asda-adsad KK-ASD-20.05.05-jjj 20.05.05 <NA> 模型(和数据库表)以及Users模型(爱好)。我正在使用Interests表单。

但是,提交表单不会更新fields_for模型。它要么只更新Interests模型,要么返回各种错误(以下产生User错误):

型号:

Interest user must exist

控制器:

class User < ApplicationRecord
    has_many :interests,foreign_key: "interests_user_id"
    accepts_nested_attributes_for :interests
    
end

class Interest < ApplicationRecord
    belongs_to :user

    self.primary_key = "interest_id"

    
end

FORM:

class UsersController < ApplicationController
      
      def index
        @allUsers = User.all

      end

      def new
        @user = User.new
        @user.interests.build
        
      end

      def create
    @user = User.new permitted_params

    =begin
        @user = User.new(
                :author => params[:user][:author],:user_name => params[:user][:user_name],:user_bio => params[:user][:user_bio],)

    @user.interests.new(
      :interest_name => params[:user][:interests_attributes][:interest_name],:interest_experience_level => params[:user][:interests_attributes][:interest_experience_level],:interest_positon => params[:user][:interests_attributes][:interest_positon],)
    =end

        if @user.save
            redirect_to users_url

        else
            render 'new'

            end
            
          end
    
    
    
        private
    
            def permitted_params
              #params.require(:interest).permit(User.attribute_names.map(&:to_sym),interest.attribute_names.map(&:to_sym))
    
              params.require(:user).permit(User.attribute_names.map(&:to_sym),interests_attributes: [:interest_name,:interest_experience_level,:interest_positon])
    
    
            end
    
    end

模式:

 <%= form_with model: @user do |newUser| %>
            <% if @user.errors.any? %>
                <ul>
                    <% @user.errors.full_messages.each do |message| %>
                        <li> <%= message %></li>
                    <%end%>
                </ul>
            <%end%>

            <div class="form-sections" id="user-profile">
                <h3>General information</h3>
                <table class="formTables" id="formMainInfo">

                    <%= newUser.hidden_field :author,value:1 %>
                
                    <tr class="formRows">
                        <td class="formDescriptionCol"><%= newUser.label :user_name,"Your Name" %></td>
                        <td class="formInputCol">
                            <%= newUser.text_field :user_name %>
                        </td> 
                    </tr>
                    
                    
                    <tr class="formRows">
                        <td class="formDescriptionCol"> <%= newUser.label :user_bio,"Biography" %></td>
                        <td class="formInputCol">
                            <%= newUser.text_area :user_bio %>
                        </td> 
                    </tr>
                    
                    
                <h3>Interests</h3>


                    <%= newUser.fields_for :interests do |newInterest| %>
                        <tr class="interestRows">
                            <td >Interest Name:</td>
                            <td>
                                <%= newInterest.text_field :interest_name%>
                            </td> 
                        </tr>
                        <tr class="interestRows">
                            <td ><%=newInterest.label :interest_experience_level,"Experience Level" %></td>
                            <td >
                                <%= newInterest.text_field :interest_experience_level %>
                            </td> 
                        </tr>
                        <tr class="interestRows">
                            <td ><%= newInterest.label :interest_position,"Position" %></td>
                            <td >
                                <%= newInterest.text_field :interest_position %>
                            </td> 
                        </tr>
                        
                        
                    <%end%>
            
                </table>
            </div>
            
            <div class="formSections" id="buttonsSection">
                <%= newUser.submit "submit" %>
            </div>



        <%end%>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)