Rails Ajax render partial with local variable -


hey trying render partial using ajax work in rails normal form when rendering partial using ajax local variables don't work trying create , dislike system have created model called feedlike here can , dislike using simple create , destroy when using ajax , there calling action below :

$('#feed_like').html("<%= j render :partial => 'shared/dislike', :locals => { feed: @feed= @feed} %>"); 

i getting error no route matches

{:action=>"destroy", :controller=>"feed_likes", :feed_id=>nil, :user_id=>1}

and dislike link partial under :

 <div id="feed_dislike">     <%= link_to "dislike",{ :action => 'destroy', :controller => 'feed_likes', :feed_id => @feed, :user_id => current_user.id }, class: "btn btn-primary" %>         </div> 

everything work fine when not using ajax when rendering partial why @feed not getting value supposed . value of feed.id . feed.id coming , model called feed , under , there rendering these 2 forms using partials .

now pasting codes give doing :

     #shared/_feed.html.erb     <% if @feed_items.try(:any?) %>           <ol class="microposts">             <%= render @feed_items %>            </ol>           <%= will_paginate @feed_items %>         <% end %> 

and @feed_items coming :

      #feeds/_feed.html.erb      <li id="feed-<%= feed.id %>">       <%= image_tag(current_user.avatar.url(:thumb), :size => '50x50') %>       <span class="user"><%= link_to feed.user.name, feed.user %></span>       <span class="content"><%= feed.content %></span>       <span class="timestamp">         posted <%= time_ago_in_words(feed.created_at) %> ago.       </span>        <%  @like =feedlike.where(:user_id => "#{current_user.id}", :feed_id => "#{feed.id}")         %>       <%= render :partial => 'shared/feed_like', locals: { feed: @feed= feed.id} %>     </li> 

and feed_like partial :

    #shared/_feed_like.html.erb    <% if @like.count == 0 %>                 <%= render :partial => 'shared/like', locals: { feed1: @feed= @feed} %>                <% else %>             <%= render :partial => 'shared/dislike', locals: { feed:  @feed} %>             <% end %> 

controllers code :

     class feedscontroller < applicationcontroller              before_action :authenticate_user! ,only: [:create, :destroy]           def create               @feed = current_user.feeds.build(feed_params)               @feed_likes = feedlike.new               if @feed.save                  #redirect_to root_url                 respond_to |format|                  format.html { redirect_to root_url }                 format.js               end                else               respond_to |format|                  format.html { redirect_to root_url }                 format.js { render :js=>'alert("you can not leave post empty");' }               end             end             end              def destroy             end              private                def feed_params                 params.require(:feed).permit(:content)               end           end 

feedslike controller :

                class feedlikescontroller < applicationcontroller             before_action :authenticate_user! ,only: [:create, :destroy]                def index                 @fees = feedlike.all                 respond_to |format|                          format.html                          format.js                           end               end             def update                 @feed_likes = feedlike.find_or_create_by(feed_like_params)                     respond_to |format|                         if @feed_likes.save                         format.html { redirect_to root_url, notice: 'like ' }                          else                          end                    end             end               def create                     @feed_likes = feedlike.find_or_create_by(:feed_id => params[:feed_id],:user_id =>params[:user_id])                     respond_to |format|                         if @feed_likes.save                         format.html { redirect_to root_url, notice: 'like ' }                         format.js                         else                          end                    end               end                 def delete                end                def destroy                   @feed_likes = feedlike.where(:feed_id => params[:feed_id],:user_id =>params[:user_id])                    respond_to |format|                        if @feed_likes.destroy_all                        format.html { redirect_to root_url, notice: 'unlike ' }                        else                         end                    end               end               def feed_like_params                   params.require(:feed_like).permit(:user_id, :feed_id)                   #params[:market_place]                 end                 end 

and staticpage controller :

class staticpagescontroller < applicationcontroller                 before_action :authenticate_user!               def home               if user_signed_in?                  @feed_likes = current_user.feed_likes.new                  @feed  = current_user.feeds.build                  @feed_items = current_user.feed.paginate(page: params[:page])                  # @likes = feed.find(:all,                   #    :select => '"feeds".id, "feeds".content, count("feed_likes".id) like',                   #    :from => :feeds,                   #    :left_join => '"feed_likes"',                   #    :on => '"feeds".id = "feed_likes"."feed".id',                   #    :limit => 5                  #  )                   # @like =feed.find_by_sql "                   #        select id                  #        feed_likes                  #        user_id = #{current_user.id}                  #        , feed_id =#{feed.id}                  #        limit 0 , 30                   #    "                 end               end                def               end                 def               end               def contact              end               end 

your feed nil why crashing

please add in feed_likes controller.

before_action :get_feed ,only: [:create, :destroy] 

and @ bottom add method

def get_feed   @feed= feed.find(params[:feed_id]) end 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -