【发布时间】:2018-04-25 03:17:21
【问题描述】:
这是我在这里的第一个问题,我大约在 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 %>
reviews_controller.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
photos_controller.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
【问题讨论】:
标签: ruby-on-rails ruby bootstrap-modal