Rails Ajax 使用局部变量渲染部分
Rails Ajax render partial with local variable
嘿,我正在尝试使用 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}
我不喜欢 link 是这样的,它是下面的部分:
<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
您的 Feed 定义在您的 javascript 文件中看起来有误。试试这个:
$('#feed_like').html("<%= j render :partial => 'shared/dislike', :locals => { feed: @feed} %>");
您的 Feed 为零,这就是它崩溃的原因
请将此添加到您的 feed_likes 控制器中。
before_action :get_feed ,only: [:create, :destroy]
并在底部添加此方法
def get_feed
@feed= Feed.find(params[:feed_id])
end
嘿,我正在尝试使用 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}
我不喜欢 link 是这样的,它是下面的部分:
<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
您的 Feed 定义在您的 javascript 文件中看起来有误。试试这个:
$('#feed_like').html("<%= j render :partial => 'shared/dislike', :locals => { feed: @feed} %>");
您的 Feed 为零,这就是它崩溃的原因
请将此添加到您的 feed_likes 控制器中。
before_action :get_feed ,only: [:create, :destroy]
并在底部添加此方法
def get_feed
@feed= Feed.find(params[:feed_id])
end