ajax - Rails Tutorial partial refresh -
i went through michael hartl's rails tutorial book , want use ajax automatic feed refresh, on micropost creation , micropost deletion.
as far understand, need specify remote: true parameter in form inside correct partial , respond js inside appropriate method.
nevertheless, when try accomplish partial refresh create action i'm getting strange nomethoderror indicating @feed_items nil object.
started post "/microposts" 77.70.8.167 @ 2016-06-28 10:21:32 +0000 processing micropostscontroller#create js parameters: {"utf8"=>"✓", "authenticity_token"=>"qjhp9flp+ev+cxdef69l8eszc1fmsjr+mi57f3u3z2y/fji9dl1to9t4jlrx4g2uhip67fiwvjwl7sp2hmc4fw==", "micropost"=>{"content"=>"dsdsds"}, "commit"=>"post"} user load (0.2ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 1]] (0.1ms) begin transaction sql (0.3ms) insert "microposts" ("content", "user_id", "created_at", "updated_at") values (?, ?, ?, ?) [["content", "dsdsds"], ["user_id", 1], ["created_at", "2016-06-28 10:21:32.797365"], ["updated_at", "2016-06-28 10:21:32.797365"]] (11.1ms) commit transaction @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@nilclass rendered shared/_feed.html.erb (7.7ms) rendered microposts/create.js.erb (11.5ms) completed 500 internal server error in 43ms (activerecord: 11.7ms) nomethoderror (undefined method `any?' nil:nilclass): app/views/shared/_feed.html.erb:3:in `_app_views_shared__feed_html_erb___1168566426182367941_69986820092480' app/views/microposts/create.js.erb:1:in `_app_views_microposts_create_js_erb__3336806296105309297_69986601152860' app/controllers/microposts_controller.rb:9:in `create'
here's _micropost.html.erb partial:
<%= form_for(@micropost, remote: true) |f| %> <%= render 'shared/error_messages', object: f.object %> <div class="field"> <%= f.text_area :content, placeholder: "compose new micropost..." %> </div> <%= f.submit "post", class: "btn btn-primary" %> <span class="picture"> <%= f.file_field :picture, accept: 'image/jpeg, image/gif, image/png' %> </span> <% end %> <script type="text/javascript"> $('#micropost_picture').bind('change', function(){ size_in_megabytes = this.files[0].size/1024/1024; if (size_in_megabytes > 5) { alert('maximum file size 5mb. please chose smaller file.') } }) </script>
create.js.erb:
$(".ajaxreloadposts").html("<%= escape_javascript(render('shared/feed')) %>");
_feed.html.erb:
<% if @feed_items.any? %> <ol class="microposts"> <%= render @feed_items %> </ol> <%= will_paginate @feed_items %> <% end %>
create action:
def create @micropost = current_user.microposts.build(micropost_params) if @micropost.save! # flash[:success] = "micropost created!" respond_to |format| format.html { redirect_to root_path } format.js end else @feed_items = [] render 'static_pages/home' end end
_home_logged_in.html.erb:
<div class="row"> <aside class="col-md-4"> <section class="user_info"> <%= render 'shared/user_info' %> </section> <section class="stats"> <%= render 'shared/stats' %> </section> <section class="micropost_form"> <%= render 'shared/micropost_form' %> </section> </aside> <div class="col-md-8"> <h3>where buddies right now?</h3> <%= render 'shared/gmaps' %> </div> <div class="col-md-8"> <h3>micropost feed</h3> <span class="ajaxreloadposts"> <%= render 'shared/feed' %> </span> </div> </div>
and finally, guess, relevant part of static_pages_controller:
def home if logged_in? @micropost = current_user.microposts.build @feed_items = current_user.feed.paginate(page: params[:page]) @gmaps_users = current_user.gmaps_feed @hash = gmaps4rails.build_markers(@gmaps_users) |spot, marker| marker.lat spot.lat marker.lng spot.lng marker.infowindow spot.user.name end if !current_user.checkin.nil? @user_lat = current_user.checkin.lat @user_lng = current_user.checkin.lng end end end
i tried following these resources, in vain: ajax update micropost vote up/down partial refreshes recent post , ajax feed update on post , how update feed using ajax in rails 4.0
i might have missed something, since i'm new rails, hope you'll merciful.
thanks time, , cheers.
okay, might bit noobish, managed fix problem inserting instance variable called @feed_items inside both create , destroy methods.
def create @feed_items = current_user.feed.paginate(page: params[:page]) @micropost = current_user.microposts.build(micropost_params) if @micropost.save! # flash[:success] = "micropost created!" respond_to |format| format.html { redirect_to root_path } format.js end else @feed_items = [] render 'static_pages/home' end end def destroy @micropost.destroy @feed_items = current_user.feed.paginate(page: params[:page]) # flash[:success] = "micropost deleted!" respond_to |format| format.html { redirect_to request.referrer || root_url } format.js end end
here's rails output micropost creation , deletion:
started post "/microposts" 77.70.8.167 @ 2016-06-28 12:20:56 +0000 processing micropostscontroller#create js parameters: {"utf8"=>"✓", "authenticity_token"=>"wd7xeh4mydinh2+prdy8perrj77vktwjz82tshdesbnwm6yw8tj11kgu9otscfr6iuhwbueruoh2dvu5eps+qg==", "micropost"=>{"content"=>"workworkwork"}, "commit"=>"post"} user load (0.2ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 1]] (0.1ms) begin transaction sql (0.3ms) insert "microposts" ("content", "user_id", "created_at", "updated_at") values (?, ?, ?, ?) [["content", "workworkwork"], ["user_id", 1], ["created_at", "2016-06-28 12:20:56.699428"], ["updated_at", "2016-06-28 12:20:56.699428"]] (11.0ms) commit transaction (3.5ms) select count(*) "microposts" (user_id in (select followed_id relationships follower_id = 1) or user_id = 1) micropost load (1.2ms) select "microposts".* "microposts" (user_id in (select followed_id relationships follower_id = 1) or user_id = 1) order "microposts"."created_at" desc limit 30 offset 0 cache... rendered microposts/_micropost.html.erb (43.1ms) rendered shared/_feed.html.erb (60.3ms) rendered microposts/create.js.erb (65.5ms) completed 200 ok in 99ms (views: 76.4ms | activerecord: 17.6ms) started delete "/microposts/348" 77.70.8.167 @ 2016-06-28 12:20:59 +0000 processing micropostscontroller#destroy js parameters: {"id"=>"348"} user load (0.3ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 1]] micropost load (0.1ms) select "microposts".* "microposts" "microposts"."user_id" = ? , "microposts"."id" = ? order "microposts"."created_at" desc limit 1 [["user_id", 1], ["id", 348]] (0.1ms) begin transaction sql (0.4ms) delete "microposts" "microposts"."id" = ? [["id", 348]] (9.5ms) commit transaction (0.5ms) select count(*) "microposts" (user_id in (select followed_id relationships follower_id = 1) or user_id = 1) micropost load (1.3ms) select "microposts".* "microposts" (user_id in (select followed_id relationships follower_id = 1) or user_id = 1) order "microposts"."created_at" desc limit 30 offset 0 cache... rendered microposts/_micropost.html.erb (50.9ms) rendered shared/_feed.html.erb (65.8ms) rendered microposts/destroy.js.erb (71.0ms) completed 200 ok in 99ms (views: 75.6ms | activerecord: 18.9ms)
Comments
Post a Comment