使用ajax请求的用户数据在重新加载页面后未保存

时间:2016-06-01 19:31:27

标签: javascript jquery ruby-on-rails ajax

我正在做一个form_for一个嵌套在食谱#show页面下的评论部分,并使用ajax请求执行此操作。我正在按照我的意图获得ajax POST方法,并且它在页面上显示得很好,但问题是它在我重新加载页面后没有保存到页面。

这是我的代码:

reviews_controller.rb

class ReviewsController < ApplicationController

    respond_to :html, :js

    def create

        @recipe = Recipe.find(params[:recipe_id])
        @review = @recipe.reviews.new(reviews_params)
        @review.chef = current_user
        @new_review = Review.new

        if @review.save
            flash[:success] = "Review was saved"
        else
            flash[:danger] = "Review failed to save, please try again"
        end

    end

    private

    def reviews_params
        params.require(:review).permit(:body)
    end

end

recipes_controller.rb

class RecipesController < ApplicationController
    before_action :set_recipe_find, only: [:edit, :update, :show, :like]
    before_action :require_user_like, only: [:like]
    before_action :admin_to_recipes, only: [:destroy]
    before_action :require_same_user_to_recipes, expect: [:show, :index, :like]
    before_action :require_same_user_to_recipes, only: [:edit, :update]


    def index
        @recipes = Recipe.paginate(page: params[:page], per_page: 4)
    end

    def show
        @review = @recipe.reviews.build
    end

    def create
        @recipe = Recipe.new(recipe_params)
        @recipe.chef = current_user

        if @recipe.save
            flash[:success] = "Your recipe was created successfully!"
            redirect_to recipes_path
        else
            render :new

        end
    end

    def edit

    end

    def update

        if @recipe.update(recipe_params)
            flash[:success] = "Your recipe was updated successfully!"
            redirect_to recipes_path
        else
            render :edit
        end
    end

    def new
        @recipe = Recipe.new
    end

    def like
        like = Like.create(like: params[:like], chef: current_user, recipe: @recipe)
        if like.valid?
            flash[:success] = "Your selection was successful"
            redirect_to :back
        else
            flash[:danger] = "You can only like/dislike a recipe once"
            redirect_to :back
        end
    end

    def destroy
        Recipe.find(params[:id]).destroy
        flash[:success] = "The Recipe was deleted successfully"
        redirect_to recipes_path
    end

    private

    def recipe_params
        params.require(:recipe).permit(:name, :summary, :description, :picure, style_ids: [], ingredient_ids: [])
    end

    def require_same_user_to_recipes
        if current_user != @recipe.chef and !current_user.admin?
            flash[:danger] = "You can only edit/update your own recipes"
            redirect_to root_path
        end
    end

    def set_recipe_find
        @recipe = Recipe.find(params[:id])  
    end

    def admin_to_recipes
        redirect_to recipes_path unless current_user.admin?
    end

    def require_user_like
        if !logged_in?
            flash[:danger] = "You must be logged in"
            redirect_to :back
        end
    end
end

以下是我的评论部分

<h4> Create a Review </h4>
<div class = "row">
    <div class="well col-md-8 col-md-offset">
        <%= form_for [@recipe, Review.new], remote: true do |f| %>
        <%= f.label :body %>
        <%= f.text_field :body, rows: 10%>
        <%= f.submit "Create Review", class: "btn btn-success" %>
        <% end %>
    </div>
</div>

和我的javascript代码:

create.js.erb

<% if @review.valid? %>
     $('.js-reviews').append("<%= escape_javascript(render(@review)) %>");
     $('.new-review').html("<%= escape_javascript(render partial: 'reviews/form', locals: { recipe: @recipe, review: @new_review }) %>");
<% else %>
    $('.new-review').html("<%= escape_javascript(render partial: 'reviews/form', locals: { recipe: @recipe, review: @review }) %>");
<% end %>

更新

审核部分参赛作品

<div class = "row review-row">
    <div class = "media-left">
        <%= link_to gravatar_for(@recipe.chef, size: 35), chef_path(@recipe.chef) %>
        <small><%= @recipe.chef.name %></small>
    </div>
    <div class ="media-body review-words">
        <h4> <%= @review.body %> </h4>
    </div>
</div>
<br>
<hr>
show.html.erb中的

部分显示了呈现部分

的html
<div class ="row">
    <br>
    <h4 class= "review-heading"> Reviews </h4>
    <hr>
    <div class = 'col-md-12'>
        <div class='js-reviews'>
            <%= render 'reviews/review'%>
        </div>
    </div>
</div>

<% if logged_in? %>
<div class='new-review'>
    <%= render 'reviews/form', recipe: @recipe, review: @review %>
</div>
<% else %>
    <p> <%= link_to "Log in", login_path %> or <%= link_to "create an account", register_path %> to create an review! </p>
<% end %>

数据库录入

Started POST "/recipes/6/reviews" for ::1 at 2016-06-02 16:08:55 -0400
Processing by ReviewsController#create as JS
  Parameters: {"utf8"=>"✓", "review"=>{"body"=>"review example"}, "commit"=>"Create Review", "recipe_id"=>"6"}
  Recipe Load (0.2ms)  SELECT  "recipes".* FROM "recipes" WHERE "recipes"."id" = ?  ORDER BY "recipes"."updated_at" DESC LIMIT 1  [["id", 6]]
  Chef Load (0.1ms)  SELECT  "chefs".* FROM "chefs" WHERE "chefs"."id" = ? LIMIT 1  [["id", 6]]
   (0.1ms)  begin transaction
  SQL (27.3ms)  INSERT INTO "reviews" ("body", "recipe_id", "chef_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["body", "review example"], ["recipe_id", 6], ["chef_id", 6], ["created_at", "2016-06-02 20:08:55.316206"], ["updated_at", "2016-06-02 20:08:55.316206"]]
   (1.4ms)  commit transaction
  Chef Load (0.1ms)  SELECT  "chefs".* FROM "chefs" WHERE "chefs"."id" = ? LIMIT 1  [["id", 6]]
  Rendered reviews/_review.html.erb (1.7ms)
  Rendered reviews/_form.html.erb (1.7ms)
  Rendered reviews/create.js.erb (30.4ms)
Completed 200 OK in 110ms (Views: 73.4ms | ActiveRecord: 29.2ms)

0 个答案:

没有答案