代码之家  ›  专栏  ›  技术社区  ›  gbenga wale

是否可以在一个控制器内将数据插入两个不同的表中

  •  0
  • gbenga wale  · 技术社区  · 5 年前

    用户表

    图片表

    它的主要工作是存储图片路径、名称和用户id(这样做是因为我想让我的应用程序的用户上传不止一张图片,如果我在users表中为图片创建一列,我将不允许我这样做)。

    现在在表单(注册表单)中,如果用户决定上传图片,我希望图片保存在pictures表中,同时仍在

    用户控制器创建方法

    这就是我到目前为止所做的(新建.html.erb)

    <% provide(:title, 'Sign up') %>
    <h1>Sign up</h1>
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <%= form_for(@user, url: signup_path) do |f| %>
              <%= render'shared/error_messages', object: f.object %>
              <%= f.label :name %>
              <%= f.text_field :name, class: 'form-control' %>
    
              <%= f.label :email %>
              <%= f.email_field :email, class: 'form-control' %>
    
              <%= f.label :username %>
              <%= f.text_field :username, class: 'form-control' %>
    
              <%= f.label :password %>
              <%= f.password_field :password, class: 'form-control' %>
    
              <%= f.label :password_confirmation, "Confirmation" %>
              <%= f.password_field :password_confirmation, class: 'form-control' %>
    
              <span class="picture">
                <%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
              </span>
              <%= f.submit "Create an account", class: "btn btn-primary" %>
            <% end %>
        </div>
    </div>
    

    1. def create
    2.  @user = User.new(user_params) # Not the final implementation!
    3.
    4.  if @user.save
    5.      # Save to the picture name to the picture table
    6.               if params[:picture].present?
    7.                 # How do I save the picture to the picture table from users controller
    8.            @user.send_activation_email
    9.        flash[:info] = "Please check your email to activate your account."
    10.       redirect_to root_url
    11. else
    12.     #@states = State.all
    13.         render 'new'
    14. end
    15.  end
    

    如果你看6号线和7号线,我有这些

    if params[:picture].present?
      # How do I save the picture to the picture table from users controller
    

    这就是现在的问题所在,如何将图片名称插入user-controller中的数据库

    0 回复  |  直到 5 年前
        1
  •  1
  •   SunTechnique    5 年前
    # new action
    def new 
      @user = User.new
      @user.pictures.build
    end
    
    # create action
    def create
      @user = User.new(user_params)
      @user.save
    end
    
    private
    def user_params
      params.require(:user).permit(:name, pictures_attributes: [:id]) 
    # in addition to the id you need to specify the other required fields.
    end
    
    # User model
    class User
      has_many :pictures
      accepts_nested_attributes_for :pictures, allow_destroy: true
    end
    
    # form view
    
    <%= form_for @user do |f| %>
      pictures:
        <%= f.fields_for :pictures do |picture_form| %>
        # your code
    

    我希望这能帮助你理解

        2
  •  0
  •   Rishabh Aggarwal    5 年前