Rails 4 _form partial创建新记录但不会编辑当前记录

时间:2014-08-29 01:20:07

标签: ruby-on-rails-4 simple-form-for

我已经构建了一个包含商家的简单应用,这些商家有很多评论。我使用部分_form来创建/更新评论。 create动作正常,但更新操作会创建一个新的审阅,而不是按照应该更新的方式更新审阅。这是相关代码(为简洁起见,删除了相关代码):

来自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视图中呈现为部分。 `review / _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>

奇怪的是,当我点击&#34;编辑&#34; _review.html.erb中的链接,生成的网址似乎是正确的,例如我获得了http://localhost:3000/businesses/10/reviews/82/edit。但是,_form.html.erb为空,我希望在其中填充当前数据以供审核#82。此外,当我点击&#34;创建评论&#34;按钮,它会创建一个新评论,并且不会编辑评论#82。

点击&#34;编辑&#34;点击服务器日志。对于现有的评论:

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)

很明显,服务器日志中的第二个操作是&#34;已开始POST ...&#34;这是不正确的。虽然我在谈论它,但我也可以提供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: Use same partial for creating and editing nested items

非常感谢。

1 个答案:

答案 0 :(得分: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