javascript - Receiving Action::Template::Error after Submitting Form with image AJAX acts_as_votable -
the issue having, form submit when body_text included. but, form not submit when image included. i'm using remotipart, acts_as_votable , carrierwave together. issue seems revolve around acts_as_votable route. have displayed below. how can partial form include image while using ajax?
error showing in console
started post "/posts" 127.0.0.1 @ 2017-08-20 15:29:00 -0400 processing postscontroller#create js parameters: {"utf8"=>"✓", "post"=>{"body_text"=>"photo", "post_photo"=>#<actiondispatch::http::uploadedfile:0x4b4e7f8 @tempfile=#<tempfile:c:/users/john/appdata/local/temp/rackmultipart20170820-10496-jajixx.jpg>, @original_filename="img_0110.jpg", @content_type="image/jpeg", @headers="content-disposition: form-data; name=\"post[post_photo]\"; filename=\"img_0110.jpg\"\r\ncontent-type: image/jpeg\r\n">}, "commit"=>"create post"} user load (0.0ms) select "users".* "users" "users"."id" = $1 order "users"."id" asc limit $2 [["id", 5], ["limit", 1]] (0.0ms) begin (0.0ms) rollback rendering posts/create.js.erb rendered posts/_post.html.erb (118.8ms) rendered posts/create.js.erb (212.6ms) completed 500 internal server error in 3660ms (activerecord: 0.0ms) actionview::template::error (no route matches {:action=>"like", :controller=>"posts", :id=>nil}, missing required keys: [:id]): 9: <div class="post-container-charm-bar"> 10: <ul> 11: <li class="votes" id="#post_<%= post.id %>"> 12: <%= link_to like_post_path(post), style: 'text-decoration: none', class: 'like-btn', method: :put, remote: true %> 13: <p id="thumb-id" class="thumbs-up">b</p> 14: <% end %> 15: </li> app/views/posts/_post.html.erb:12:in `_app_views_posts__post_html_erb__907999817_40537128' app/views/posts/create.js.erb:1:in `_app_views_posts_create_js_erb___754512395_40214292'
routes.rb
resources :posts member post :share put 'like', to: 'posts#like' end end
create.js.erb
$("#container_posts").prepend("<%= j render partial: "posts/#{@posts.posts_type}", locals: {posts: @posts } %>"); $("#posts_<%= @posts.id %>").hide().fadein(1000); $("#text-area-reset").val('');
destroy.js.erb
$("#post_<%= @post.id %>").fadeout("slow", function(){ $(this).remove(); });
post _form.html.erb
<div class="container"> <%= simple_form_for(@post, multipart: true, remote: true) |f| %> <%= f.error_notification %> <div class="row"> <div class="col-6 post-textarea"> <p class="emoji-picker-container"> <%= f.input :body_text, as: :text, class: 'form-control post-placeholder', label: false, id: 'text-area-reset', placeholder: 'write new post' %> </p> </div> </div> <div class="row"> <div class="col-5"> <%= f.button :submit, class: 'form-control btn btn-outline-success' %> </div> <div class="col-4"> <h6>add photo</h6> <%= f.file_field :post_photo %> <% if f.object.post_photo? %> <%= image_tag f.object.post_photo.feed_preview.url %> <%= f.label :remove_image %> <%= f.check_box :remove_image %> <% end %> </div> </div> <% end %> </div>
posts_controller.rb
# acts_as_votable def @post = post.find(params[:id]) if !current_user.liked? @post @post.liked_by current_user elsif current_user.liked? @post @post.unliked_by current_user end
_post.html.erb
<div class="post-container" id="post_<%= post.id %>"> <div class="media" style="padding-bottom: 2em;"> <img class="d-flex align-self-start mr-3 purple-rounded rounded" src="http://via.placeholder.com/60x60"> <div class="media-body post-user-name"> <h5><%= fa_icon 'user' %> <%= post.user.user_full_name %></h5> <p><%= content_with_emoji(post.body_text) %> </p> </div> </div> <div class="post-container-charm-bar"> <ul> <li class="votes" id="#post_<%= post.id %>"> <%= link_to like_post_path(post), style: 'text-decoration: none', class: 'like-btn', method: :put, remote: true %> <p id="thumb-id" class="thumbs-up">b</p> <% end %> </li> <li><strong class="likes-count"><%= number_with_delimiter(post.get_likes.size) %></strong></li> <li><%= link_to '#', data: {toggle: "modal", target: "#commentmodal"} %> <%= image_tag 'post/chat-state-1-30x30.png' %> <% end %></li> <li><strong><%= post.comment_threads.size %></strong></li> <li><%= link_to share_post_path(post), method: :post if user_signed_in? && post.user_id != current_user.id %><%= image_tag 'post/share-state-1-30x30.png' %> <% end %></li> <li><%= link_to post, style: 'text-decoration: none;' %><%= image_tag 'post/opened-eye-state-1-30x30.png' %> <% end %></li> <li><%= link_to edit_post_path(post), style: 'text-decoration: none;' %><%= image_tag 'post/edit-state-1-30x30.png' %> <% end %> </li> <li> <% if current_user == post.user %> <%= link_to post_path(post), style: 'text-decoration: none;', remote: true, method: :delete, data: {confirm: 'are sure?'} %><%= image_tag 'post/garbage-state-1-30x30.png', class: 'trash-can-icon' %> <% end %> <% end %></li> </ul> </div> </div>
i used wrong form extension method. now, it's working small change below.
<%= f.input :post_photo, as: :file, label: false %>
Comments
Post a Comment