0

Hey I am trying to render a partial using ajax everything work in rails normal form but when I am rendering a partial using ajax local variables don't work I am trying to create a like and dislike system for that I have created a model called FeedLike here I can like and dislike using simple create and destroy but when I am using ajax and there I am calling this action below :

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

I am getting the error No route matches

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

And my dislike link is like this it is a partial which is 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 I am not using ajax but when I am rendering partial why @feed is not getting any value what am I supposed to do . To get the value of feed.id . feed.id is coming from and model called Feed and it is under look and there I am rendering these two forms using partials .

now I am pasting some codes which will give you what I am 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 is coming from :

      #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 now 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 for :

     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 do |format|

                format.html { redirect_to root_url }
                format.js
              end
               else
              respond_to do |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 do |format|

                        format.html 
                        format.js


                        end
              end
            def update
                @feed_likes = FeedLike.find_or_create_by(feed_like_params)
                    respond_to do |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 do |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 do |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) as Like', 
                 #    :from => :feeds, 
                 #    :left_join => '"feed_likes"', 
                 #    :on => '"feeds".id = "feed_likes"."feed".id', 
                 #    :limit => 5
                 #  )

                 # @like =Feed.find_by_sql "

                 #        SELECT id
                 #        FROM feed_likes
                 #        WHERE user_id = #{current_user.id}
                 #        AND feed_id =#{feed.id}
                 #        LIMIT 0 , 30

                 #    "
                end
              end

              def help
              end


              def about
              end

             def contact
             end


            end
16
  • try this $('#feed_like').html("<%= j render :partial => 'shared/dislike', :locals => { feed: @feed} %>"); Commented Jul 22, 2015 at 14:15
  • I had tried this but not working Commented Jul 22, 2015 at 14:47
  • is this the $('#feed_like').html("<%= j render :partial => 'shared/dislike', :locals => { feed: @feed} %>"); the response for action in feed_likes controller. is your @feed object really exists in the file.? Commented Jul 22, 2015 at 15:07
  • No it is coming form <%= render :partial => 'shared/feed_like', locals: { feed: @feed= feed.id} %> location #feeds/_feed.html.erb Commented Jul 22, 2015 at 15:17
  • I was talking about after controller action.which controller action is this response Commented Jul 22, 2015 at 15:45

2 Answers 2

1

your feed is nil which is why it is crashing

please add this in your feed_likes controller.

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

and at the bottom add this method

def get_feed
  @feed= Feed.find(params[:feed_id])
end
Sign up to request clarification or add additional context in comments.

Comments

1

Your feed definition looks wrong in your javascript file. Try this instead:

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

1 Comment

Added controller details

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.