Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.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 是否可以将数据插入一个控制器内的两个不同表中_Ruby On Rails_Ruby_Ruby On Rails 5 - Fatal编程技术网

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

Ruby on rails 是否可以将数据插入一个控制器内的两个不同表中,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,我有这张桌子,名字是什么 用户表 及 图片表 其主要工作是存储图片路径和名称以及用户idAm,这样做是因为我希望允许我的应用程序的用户上载多张图片,如果我在users表中为图片创建列,则不允许我这样做 现在在formSignup表单中,如果用户决定上传图片,我希望图片保存在pictures表中,同时仍保存在 用户\u控制器创建方法 1. def create 2. @user = User.new(user_params) # Not the final implementation! 3.

我有这张桌子,名字是什么

用户表

图片表

其主要工作是存储图片路径和名称以及用户idAm,这样做是因为我希望允许我的应用程序的用户上载多张图片,如果我在users表中为图片创建列,则不允许我这样做

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

用户\u控制器创建方法

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
这就是我到目前为止所做的new.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>
如果你看第6行和第7行,我有这些

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

这就是现在的问题所在,我如何将图片名称插入到user_controller内部的数据库中

我想您需要的是嵌套表单。 嵌套表单也接受子类的参数,在视图中可以添加任意数量的图像,并将其存储在相应的表中。 退房-

并用手书写字段名称:

<%= f.fields_for :pictures_attributes do |picture_form| %>
  <%= picture_form.text_field :name, name: "user[pictures_attributes][][name]" %>

简短的回答是肯定的,你们可以做到这一点,无论你们从哪个控制器调用它,它看起来都并没有什么不同。没有明确的规则要求您只能在控制器中创建单一类型的记录。我可以回答,但首先,一个问题-你用回形针来处理你的上传吗?或者其他宝石?我建议你使用,除非你有理由不使用。@maxpleaner不,我使用mini magick
# 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
<%= f.fields_for :pictures_attributes do |picture_form| %>
<%= f.fields_for :pictures_attributes do |picture_form| %>
  <%= picture_form.text_field :name, name: "user[pictures_attributes][][name]" %>