ruby-on-rails - Ruby on Rails - 以一种形式提交评论文本和照片
问题描述
这是我在这里的第一个问题,我大约在 4-5 个月前才开始正确编写代码,所以请善待。
我正在尝试使用 Ruby on Rails 构建一个简单的评论网站。我希望我的用户能够使用评论表单(在 Bootstrap 模式内)提交评论并同时上传多张照片,但似乎无法通过一个按钮将其提交到两个数据库表(表是评论和照片)。
目前,用户可以使用表单上的两个单独按钮单独提交照片和评论,但不能同时使用一个按钮。这两个按钮解决方案的问题是,当单击任一按钮并提交表单时,模式会关闭。我已经研究了照片上传部分的 AJAX,但不确定这是否可行?
离开审查按钮触发模态弹出
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-normal leave-review-button" data-toggle="modal" data-target="#myModal_<%= @area.id %>">
Leave Review
</button>
<% if !user_signed_in? %>
<!-- Modal -->
<div id="myModal_<%= @area.id %>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Please <%= link_to "login", new_user_session_path %> or <%= link_to "sign up", new_user_registration_path %> to leave a review</h4>
</div>
<% else %>
<!-- Modal -->
<div id="myModal_<%= @area.id %>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Your Review</h4>
<h5> Leave your honest review below </h5>
<%= render 'areas/photo_upload_form' %>
</div>
<div class="modal-body">
<%= form_for current_user.reviews.new, html: {multipart: true} do |f| %>
<div class="form-group text-center">
<div id="stars"></div>
</div>
<div class="form-group">
<%= f.text_area :comment, rows: 3, class: "form-control" %>
</div>
<%= f.hidden_field :area_id, value: @area.id %>
<%= form_for @area, url: area_photos_path(@area), method: 'post', html: {multipart: true} do |f| %>
<div class="text-center">
<%= f.submit "Add Review", class: "btn btn-normal" %>
</div>
<% end %>
<% end %>
<% end %>
</div>
</div>
</div>
</div>
</div>
<script>
$('#stars').raty({
path: '/assets',
scoreName: 'review[star]',
score: 1
});
</script>
_photo_upload_form 部分
<%= form_for @area, url: area_photos_path(@area), method: 'post', html: {multipart: true} do |f| %>
<span class="btn btn-default btn-file text-babu">
<i class="fa fa-cloud-upload" aria-hidden="true"></i> Select Photos
<%= file_field_tag "images[]", type: :file, multiple: true %>
</span>
<div class="text-center">
<%= f.submit "Add Photos", class: "btn btn-form" %>
</div>
<% end %>
评论控制器.rb
class ReviewsController < ApplicationController
skip_before_action :verify_authenticity_token
def create
@area = Area.where(
id: review_params[:area_id]
).first
if !@area.nil?
@has_reviewed = Review.where(
area_id: @area.id,
user_id: review_params[:user_id]
).first
if @has_reviewed.nil?
# Allow to review
@review = current_user.reviews.create(review_params)
redirect_back(fallback_location: request.referrer)
flash[:success] = "Review created..."
else
# Already reviewed
flash[:success] = "You already reviewed this Area"
end
else
flash[:alert] = "No area found"
end
end
def destroy
@review = Review.find(params[:id])
@review.destroy
redirect_back(fallback_location: request.referrer, notice: "Review successfully deleted")
end
private
def review_params
params.require(:review).permit(:comment, :star, :area_id, :user_id, :photo)
end
end
照片控制器.rb
class PhotosController < ApplicationController
def create
@area = Area.find(params[:area_id])
if params[:images]
params[:images].each do |img|
@area.photos.create(image: img, user_id: current_user.id)
end
@photos = @area.photos
redirect_back(fallback_location: request.referer, notice: "Saved...")
end
end
end
解决方案
您将 form_for 嵌套在 form_for 中。这不是这样做的方法。
请查看指南中的示例。
http://guides.rubyonrails.org/form_helpers.html#nested-forms
推荐阅读
- java - 将 Java 流与 ORMLite 一起使用
- javascript - Axios - 跨域 cookie
- leaflet - 如何将 Mapbox js 函数转换为 Mapbox gl js
- arrays - 使 JSON 响应符合结构和数组 Swift
- python - 在 PyCharm 中使用子进程执行 sudo 命令
- javascript - 从 d3 事件处理程序访问类函数
- java - Spring 安全休息登录
- c - 使用C逐字节读取文件
- google-app-engine - 无法使用 spring cloud gcp starter 将 Spring Boot 与 Cloud SQL 连接
- c - C: sem_open 上的“没有这样的文件或目录”,带有 O_CREAT 和正确的信号量名称