1

我正在制作一个 Rails 应用程序,我正在上传图片,有几个复选框可以勾选或不勾选您上传的图片,一些文本输入框和一些下拉框。我为未设置的内容设置了默认值。该表格正在捕获图片和一些答案,但不是所有内容,我不知道为什么

这是有问题的表格:

<%= form_with(model: @painting, local: true) do |form| %>
  <% if @painting.errors.any? %>
    <div>
      <% pluralize(@painting.errors.count, "error") %>
      <% @painting.errors.full_messages.each do |message| %>
      <% end %>
    </div>
<% end %>

<% if current_user.id == 1 %>
  <div class="field">
    <%= form.label :river %>
    <%= form.check_box :river %>
  </div>

  <div class="field">
    <%= form.label :mountains %>
    <%= form.check_box :mountains %>
  </div>

  <div class="field">
    <%= form.label :cabin %>
    <%= form.check_box :cabin %>
  </div>

  <div class="field">
    <%= form.label :guest %>
    <%= form.text_field :guest %>
  </div>

  <div class="field">
    <%= form.label :startcolour, "Start Colour" %>
    <%= select_tag(:startcolour, options_for_select(['white', 'black', 'clear', 'acrylic black', 'grey', 'mixed', 'other'])) %>
  </div>

  <div class="field">
    <%= form.label :other %>
    <%= form.text_field :other %>
  </div>

<% end %>

  <div class="field">
    <%= form.label :season %>
    <%= select_tag(:season, options_for_select([1, 2])) %>
  </div>

  <div class="field">
    <%= form.label :episode %>
    <%= select_tag(:episode, options_for_select([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13])) %>
  </div>

  <div class="field">
    <%= form.label :artwork %>
    <%= form.file_field :artwork %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

这是来自使用默认值调用表单的控制器:

 def new

    if current_user
      if current_user.profile
        @painting = Painting.new(river: false, mountains: false, cabin: false, startcolour: "white")
        @painting.save
      else
        redirect_to new_profile_path
      end
    else 
      redirect_to new_user_session_path
    end
  end

这是它在表单之后进入控制器的位置,本质上是我连接后端,因此配置文件用户和绘画被链接:

  def create
    @painting = Painting.new(painting_params) 
    @painting.artwork.attach(params[:painting][:artwork])
    @profile = Profile.new 
    @profile.id = current_user.profile.id
    @profile.save
    @painting.profile_id = current_user.profile.id

    respond_to do |format|
      if @painting.save
        format.html { redirect_to @painting, notice: 'Painting was successfully created.' }
        format.json { render :show, status: :created, location: @painting }
      else
        format.html { render :new }
        format.json { render json: @painting.errors, status: :unprocessable_entity }
      end
    end
  end

但是,在上传绘画后,我发现无论哪个用户签名它总是将绘画分配给用户 2。除了图片和偶尔的复选框之外,它也没有保存表单中的大部分信息(甚至我指定的那些)。

我真正期待的是登录的人——他们的用户 ID 会附加到画作上。我想知道我表单中的数据到底去了哪里?为什么不省钱?我错过了什么吗?

提前致谢

4

1 回答 1

2

好像你不允许。为了做到这一点,你将在你的控制器中有这样的东西:

private 

def painting_params 
  params.require(:painting).permit(:river, :mountains, :cabin, :startcolour) # and any other column that you want to pass from the html fields 
end
于 2019-07-15T14:15:41.513 回答