代码之家  ›  专栏  ›  技术社区  ›  Nilay Singh

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

  •  0
  • Nilay Singh  · 技术社区  · 9 年前

    嘿,我正在尝试使用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没有得到任何值,我应该做什么。id提要。id来自一个名为Feed的模型,它正在查看中,我正在使用分部渲染这两个表单。

    现在我正在粘贴一些代码,这些代码将为您提供我正在做的事情:

         #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
    

    FeedLike控制器:

                    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 回复  |  直到 9 年前
        1
  •  1
  •   Athar    9 年前

    你的饲料是零,这就是它崩溃的原因

    请将其添加到feedlikes控制器中。

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

    在底部添加此方法

    def get_feed
      @feed= Feed.find(params[:feed_id])
    end
    
        2
  •  1
  •   crispychicken    9 年前

    javascript文件中的提要定义看起来错误。请尝试以下操作:

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