Rails Ajax使用局部变量呈现部分

时间:2015-07-22 14:11:26

标签: ruby-on-rails ajax partial

嘿我试图渲染部分使用ajax一切工作在rails正常形式但是当我渲染部分使用ajax局部变量不起作用我试图创建一个喜欢和不喜欢的系统为我创建了一个这里叫做FeedLike的模型我喜欢和不喜欢使用简单的创建和销毁但是当我使用ajax时我在下面调用这个动作:

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

我收到错误没有路线匹配

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

我不喜欢的链接是这样的,它是一个部分在下面:

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

当我不使用ajax时,一切正常,但是当我呈现部分为什么@feed没有得到任何价值我应该做什么。获取feed.id的值。 feed.id来自和称为Feed的模型,它正在看,在那里我使用partials渲染这两个表单。

现在我粘贴了一些代码,这些代码会给你我正在做的事情:

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

          </ol>
          <%= will_paginate @feed_items %>
        <% end %>

@feed_items来自:

      #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>

现在feed_like部分:

    #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 %>

控制器代码:

     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控制器:

                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

静态页面控制器:

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

2 个答案:

答案 0 :(得分:1)

您的Feed定义在您的javascript文件中显示错误。试试这个:

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

答案 1 :(得分:1)

您的Feed为零,这就是崩溃的原因

请在Feed_likes控制器中添加。

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

并在底部添加此方法

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