【问题标题】:Rails Create function has stopped functioningRails Create 函数已停止运行
【发布时间】:2021-04-20 11:46:22
【问题描述】:

我有一个基于 Ruby on Rails 的基本应用程序,它允许我通过用户配置文件向产品添加评论。

有人可以解释为什么我无法创建新评论,尽管我已经按照教程进行操作。 它正在失败,因为它正在运行 else 语句而不是 if。没有显示错误。

如果我查看我的数据库,表格评论中没有记录。

我的人际关系是;
审核belongs_to产品,主键:ProductId
查看belongs_to Profile,主键:ProfileId

产品has_many评论,外键:ProductId

审核控制者;

class ReviewsController < ApplicationController
before_action :set_review, only: [:edit, :update]
before_action :authorize, only: [:create] 
def index
    @reviews=Review.all
end
def new 
end
def edit
    @review=Review.find(params[:id])
end
def update
    if @review.update(review_params)
        redirect_to @review
    else
        render 'edit'
    end
end
def set_review
    @review = Review.find(params[:id])
end
def show
    @review=Review.find(params[:id])
end
def create
    @review=Review.new(review_params)
    @review.save
    redirect_to @review
   # @review=Review.new(review_params)
   # @review.save
   # if @review.save
   # redirect_to @review, notice:
   # "You have successfull added a review"
   # else
   # redirect_to reviews_path, notice:
   # "You must be signed in to add reviews"
end
def destroy
    @review = Review.find(params[:id])
    @review.destroy
    redirect_to reviews_path
end
def search
    @reviews = if params[:term]
        Review.where('Author LIKE ? OR ', "%#{params[:term]}")
    else
        Review.all
    end
end
private
def review_params
    params.require(:review) .permit(:ProfileId, :ProductId, :Author, :ProductRating, :ReviewText, :DateofReview)
end

结束

new.html.erb;

   <div class="container">
<h1> Add New Review </h1>
<%= form_for :review, url: reviews_path do |f| %>
    <div class="form-group">
<%= f.label :"Profile" %>
<%= f.select :ProfileId, Profile.all.collect {|x| [x.Name,x.id]}, {:include_blank => 'Select Profile'}, class:'form-control'%><br />
</div>   
<div class="form-group">
<%= f.label :"Product" %>
<%= f.select :ProductId, Product.all.collect {|x| [x.pName,x.id]}, {:include_blank => 'Select Product'}, class:'form-control'%><br />
</div>   
<div class="form-group">
<%= f.label :"Review Author" %>
<%= f.text_field :Author %>
</div>
<div class="form-group">
<%= f.label :"Product Rating" %>
<%= f.number_field :ProductRating %>
</div>
<div class="form-group">
<%= f.label :"Review Text"%>
<%= f.text_field :ReviewText %>
</div>
<div class="form-group">
<%= f.label :"Date of Review" %>
<%= f.date_field :DateofReview %>
</div>
<div class="form-group">
<%= f.submit %>
</div>
<% end %>
</div>

【问题讨论】:

  • 你保存了两次,但是这是否是问题很难说没有任何细节。
  • @DaveNewton 以前只有 1 个 @review.save 但我认为 if @review.save 实际上并没有保存评论,所以我添加了它
  • 它是否在某处产生错误?如果可以,您可以添加日志吗?
  • if @review.save 这件事可能有点令人困惑。如果记录保存,if 返回 true,因此该行正在保存记录,这只是一种基于是否保存的条件的苗条方法。所以你要保存记录两次。我怀疑它由于验证而无法保存第二次(虽然我不能确定没有看到模型),但它可能会保存第一次,所以删除第一个 @review.save 我敢打赌它有效.您是否还缺少该条件的end
  • @Mshka 不,据我所知,它只是运行 if 语句的 else 部分。因此,要么没有创建新的评论实例,要么没有保存评论。不知道如何检查每个操作的确切日志,就像 VS 中的断点一样

标签: html ruby-on-rails


【解决方案1】:

原来是我的模型数据加错了。

原来如此;

Review belongs_to Product, primary key: ProductId
Review belongs_to Profile, primary key: ProfileId

Product has_many reviews, foreign key: ProductId

应该是什么时候;

Review belongs_to Product, foreign key: ProductId
Review belongs_to Profile, foreign key: ProfileId

Product has_many reviews, primary key: ProductId

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 2011-08-12
    • 2012-10-21
    • 2012-09-12
    相关资源
    最近更新 更多