javascript - User data using ajax requests not saved after a reload page -
i'm doing form_for reviews section nested under recipes#show page , doing ajax requests. i'm getting ajax post method intended , it's showing on page fine, problem it's not saving page after page reload.
here code:
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 saved" else flash[:danger] = "review failed 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 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 updated successfully!" redirect_to recipes_path else render :edit end end def new @recipe = recipe.new end def = like.create(like: params[:like], chef: current_user, recipe: @recipe) if like.valid? flash[:success] = "your selection successful" redirect_to :back else flash[:danger] = "you can like/dislike recipe once" redirect_to :back end end def destroy recipe.find(params[:id]).destroy flash[:success] = "the recipe 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 , !current_user.admin? flash[:danger] = "you can edit/update 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 logged in" redirect_to :back end end end
here reviews form partial
<h4> create review </h4> <div class = "row"> <div class="well col-md-8 col-md-offset"> <%= form_for [@recipe, review.new], remote: true |f| %> <%= f.label :body %> <%= f.text_field :body, rows: 10%> <%= f.submit "create review", class: "btn btn-success" %> <% end %> </div> </div>
and javascript code:
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 %>
update
review partial entries
<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>
section in show.html.erb showing html rendering partial
<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 account", register_path %> create review! </p> <% end %>
db entry
started post "/recipes/6/reviews" ::1 @ 2016-06-02 16:08:55 -0400 processing reviewscontroller#create js parameters: {"utf8"=>"✓", "review"=>{"body"=>"review example"}, "commit"=>"create review", "recipe_id"=>"6"} recipe load (0.2ms) select "recipes".* "recipes" "recipes"."id" = ? order "recipes"."updated_at" desc limit 1 [["id", 6]] chef load (0.1ms) select "chefs".* "chefs" "chefs"."id" = ? limit 1 [["id", 6]] (0.1ms) begin transaction sql (27.3ms) insert "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".* "chefs" "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)