0

我已经构建了一个简单的应用程序,其中包含业务,并且这些业务有很多评论。我正在使用部分 _form 来创建/更新评论。create 操作可以正常工作,但 update 操作会创建一个新评论,而不是像预期的那样更新评论。这是相关代码(为简洁起见,删除了无关代码):

代码来自routes.rb

resources :businesses do
  resources :reviews
end

代码来自models/business.rb

class Business < ActiveRecord::Base
  has_many :reviews, dependent: :destroy
  validates_associated :reviews
end

代码来自models/review.rb

class Review < ActiveRecord::Base
  belongs_to :business
  belongs_to :user
end

代码来自controllers/reviews_controller.rb

class ReviewsController < ApplicationController
  load_and_authorize_resource

  before_filter :load_business

  def new
  end

  def index
  end

  def create
    @review = @business.reviews.create(review_params)
    @review.user_id = current_user.id
    @review.reviewer = current_user.first_name + ' ' + current_user.last_name
    if @review.save
        redirect_to business_path(@business)
        flash[:notice] = 'Review posted!'
    else
        redirect_to business_path(@business)
        flash[:danger] = 'Your review has an error. Please double check!'
    end
  end

  def edit
    @review = @business.reviews.find(params[:id])
  end

  def update
    @review = @business.reviews.find(params[:id])
    if @review.update(params[review_params])
        redirect_to businesses_path(@business)
        flash[:notice] = 'Review updated!'
    else
        render :action => 'edit'
    end
  end

  def destroy
    @review = @business.reviews.find(params[:id])
    @review.destroy
    redirect_to business_path(@business)
    flash[:notice] = "Review deleted."
  end

  private

  def load_business
    @business = Business.find(params[:business_id])
  end

  def review_params
    params.require(:review).permit(:review, :rating)
  end
end

`reviews/_form.html.erb 的代码:

<%= simple_form_for([@business, @business.reviews.build]) do |f| %>
  <%= f.error_notification %>
  <p>
    <%= f.input :review %>
  </p>
  <p>
    <%= f.input :rating, collection: 1..10 %>
  </p>
  <p>
    <%= f.submit :class => 'btn btn-primary' %>
  </p>
<% end %>

评论在 business#show 视图中呈现为部分。`reviews/_review.html.erb 的代码:

<% if !review.user_id.nil? %>
  <div class="well">
    <p>
      <strong>Reviewer:</strong>
      <%= review.reviewer %>
    </p>
    <p>
      <strong>Review:</strong>
      <%= review.review %>
    </p>
    <p>
      <strong>Rating:</strong>
      <%= review.rating %>
    </p>
    <span class = "timestamp">
      posted <%= time_ago_in_words(review.created_at) %> ago.
    </span>
    <% if can? :update, review %>
      <span class = "timestamp">
        <%= link_to "Edit", [:edit, review.business, review], class: 'btn btn-small' %>|
        <%= link_to "Delete", [review.business, review], method: :delete,
                   data: { confirm: 'Are you sure?' } %>
      </span>
    <% end %>
  </div>
<% end %>

代码reviews/edit.html.erb

<h1>Edit Review</h1>

<%= render 'form', review: @review %>

<button type="button" class="btn btn-default"><%= link_to 'Back', businesses_path %></button>

奇怪的是,当我点击 中的“编辑”链接时_review.html.erb,生成的 URL 似乎是正确的,例如我得到http://localhost:3000/businesses/10/reviews/82/edit. 但是,_form.html.erb 是空的,我希望它填充当前数据以供审查 #82。此外,当我单击“创建评论”按钮时,它会创建一个新评论并且不会编辑评论 #82。

这是我点击“编辑”查看现有评论后的服务器日志:

Started GET "/businesses/3/reviews/81/edit" for 127.0.0.1 at 2014-08-28 19:18:58 -0500
Processing by ReviewsController#edit as HTML
  Parameters: {"business_id"=>"3", "id"=>"81"}
  User Load (0.8ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 8  ORDER BY "users"."id" ASC LIMIT 1
  Review Load (6.9ms)  SELECT  "reviews".* FROM "reviews"  WHERE "reviews"."id" = $1 LIMIT 1  [["id", 81]]
  Business Load (0.5ms)  SELECT  "businesses".* FROM "businesses"  WHERE "businesses"."id" = $1 LIMIT 1  [["id", 3]]
  Review Load (27.4ms)  SELECT  "reviews".* FROM "reviews"  WHERE "reviews"."business_id" = $1 AND "reviews"."id" = $2 LIMIT 1  [["business_id", 3], ["id", 81]]
  Rendered reviews/_form.html.erb (107.0ms)
  Rendered reviews/edit.html.erb within layouts/application (143.4ms)
Completed 200 OK in 967ms (Views: 669.2ms | ActiveRecord: 35.6ms)


Started POST "/businesses/3/reviews" for 127.0.0.1 at 2014-08-28 19:19:23 -0500
Processing by ReviewsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"<redacted>", "review"=>{"review"=>"Edit review #81.", "rating"=>"1"}, "commit"=>"Create Review", "business_id"=>"3"}
  User Load (0.8ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 8  ORDER BY "users"."id" ASC LIMIT 1
  Business Load (0.6ms)  SELECT  "businesses".* FROM "businesses"  WHERE "businesses"."id" = $1 LIMIT 1  [["id", 3]]
    (28.9ms)  BEGIN
  SQL (132.9ms)  INSERT INTO "reviews" ("business_id", "created_at", "rating", "review", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["business_id", 3], ["created_at", "2014-08-29 00:19:23.470750"], ["rating", 1], ["review", "Edit review #81."], ["updated_at", "2014-08-29 00:19:23.470750"]]
    (39.7ms)  COMMIT
    (0.3ms)  BEGIN
  SQL (0.9ms)  UPDATE "reviews" SET "reviewer" = $1, "updated_at" = $2, "user_id" = $3 WHERE "reviews"."id" = 83  [["reviewer", "<redacted>"], ["updated_at", "2014-08-29 00:19:23.692880"], ["user_id", 8]]
    (17.5ms)  COMMIT
Redirected to http://localhost:3000/businesses/3
Completed 302 Found in 553ms (ActiveRecord: 221.6ms)


Started GET "/businesses/3" for 127.0.0.1 at 2014-08-28 19:19:23 -0500
Processing by BusinessesController#show as HTML
  Parameters: {"id"=>"3"}
  User Load (0.7ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 8  ORDER BY "users"."id" ASC LIMIT 1
  Business Load (0.5ms)  SELECT  "businesses".* FROM "businesses"  WHERE "businesses"."id" = $1 LIMIT 1  [["id", 3]]
    (0.5ms)  SELECT AVG("reviews"."rating") AS avg_id FROM "reviews"  WHERE "reviews"."business_id" = $1  [["business_id", 3]]
  Rendered reviews/_form.html.erb (5.7ms)
  Review Load (0.6ms)  SELECT "reviews".* FROM "reviews"  WHERE "reviews"."business_id" = $1  ORDER BY "reviews"."id" DESC  [["business_id", 3]]
  Rendered reviews/_review.html.erb (58.1ms)
  Rendered businesses/show.html.erb within layouts/application (173.5ms)
  Rendered users/_avatar.html.haml (13.1ms)
  Rendered layouts/_navigation.html.haml (30.9ms)
  Rendered layouts/_messages.html.haml (1.5ms)
  Rendered layouts/_footer.html.haml (16.5ms)
  Rendered layouts/_analytics.html.haml (0.6ms)
  Rendered layouts/_javascripts.html.haml (0.5ms)
Completed 200 OK in 421ms (Views: 391.5ms | ActiveRecord: 2.3ms)

很明显,服务器日志中的第二个操作是“Started POST...”,这是不正确的。在此期间,我不妨提供以下相关路线rake routes

business_reviews     GET      /businesses/:business_id/reviews(.:format)          reviews#index
                     POST     /businesses/:business_id/reviews(.:format)          reviews#create
new_business_review  GET      /businesses/:business_id/reviews/new(.:format)      reviews#new
edit_business_review GET      /businesses/:business_id/reviews/:id/edit(.:format) reviews#edit
business_review      GET      /businesses/:business_id/reviews/:id(.:format)      reviews#show
                     PATCH    /businesses/:business_id/reviews/:id(.:format)      reviews#update
                     PUT      /businesses/:business_id/reviews/:id(.:format)      reviews#update
                     DELETE   /businesses/:business_id/reviews/:id(.:format)      reviews#destroy

我已经尝试实施我可以在这里找到的所有答案,但无济于事。我发现最接近我的确切问题的是这个,但它也没有帮助:Rails:使用相同的部分来创建和编辑嵌套项

非常感谢。

4

1 回答 1

0

根据您在控制台中的输出,您在更新 stmt 之前有一个 sql insert stmt。您在表单中的代码会导致它。@business.reviews.build即使在编辑时也会创建新的评论对象。

将您的 form_for 更新为:

<%= simple_form_for([@business, @review]) do |f| %>

而不是@business.reviews.build. 您应该在控制器而不是视图中创建对象。

因此,您可能还需要在新操作中重新审核对象。

  def new
     @review = @business.reviews.build
  end

我有更多建议给你,以简化你的代码,让事情变得更容易。

使用评论 ID 查找评论,而不是通过业务。
例如编辑和更新中的代码,您可以:

  def edit
    @review = Reviews.find(params[:id])
  end

  def update
    @review = Reviews.find(params[:id])
    ...
  end
于 2014-08-29T02:31:02.660 回答