我正在制作一个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将附加到绘画上。我想知道我表单中的数据到底去了哪里?为什么不节省呢?我错过了什么吗?
预先感谢
答案 0 :(得分:1)
您似乎不允许这样做。为此,您将在控制器中添加以下内容:
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