Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 仅在ajax上渲染部分内容失败_Ruby On Rails_Ajax - Fatal编程技术网

Ruby on rails 仅在ajax上渲染部分内容失败

Ruby on rails 仅在ajax上渲染部分内容失败,ruby-on-rails,ajax,Ruby On Rails,Ajax,我的代码在概念上与MichaelHartl的railstutorial非常相似——以下是用户部分,但它的不同足以提出这个问题 所有细节之前的问题: 当我从_like中删除remote:true时,一切都很顺利, 当_like.html.erb具有remote:true时,我得到: 没有路由匹配{:action=>“destroy”,:controller=>“likes”,:artwork\u id=>1}缺少必需的键:[:id] 我不明白为什么 代码片段: 视图/艺术品 show.html.e

我的代码在概念上与MichaelHartl的railstutorial非常相似——以下是用户部分,但它的不同足以提出这个问题

所有细节之前的问题: 当我从_like中删除
remote:true
时,一切都很顺利, 当_like.html.erb具有
remote:true
时,我得到:

没有路由匹配{:action=>“destroy”,:controller=>“likes”,:artwork\u id=>1}缺少必需的键:[:id]

我不明白为什么

代码片段:

视图/艺术品
show.html.erb:


在这里,您可以创建

resources :likes, only: [:create, :destroy]

根据RAILS惯例,它为likes控制器创建路由,它使用:id例如
“likes/:id”
而不是
likes/artwork\u id
,因此请手动创建likes路由或遵循惯例。

下面是有问题的一行:

# _like.html.erb
<%= link_to 'Like', likes_path(artwork_id: @artwork.id), method: :post, remote: true %>
正如您所看到的那样,
likes\u路径
除了格式之外没有任何参数,因此您的
likes\u路径(artwork\u id:@artwork.id)
无法映射到任何路径

应使用以下内容更新管线定义:

# app/config/routes.rb
resources :artworks do 
  resources :likes, only: [:create, :destroy]
end
使用此路由定义,您的
链接到
呼叫将是:

# _like.html.erb
<%= link_to 'Like', artwork_likes_path(artwork_id: @artwork.id), method: :post, remote: true %>

# _unlike.html.erb
# Replace this_like_id with like_id attribute for this like being destroyed.
<%= link_to 'UNLike', artwork_likes_path(artwork_id: @artwork.id, id: this_like_id), method: :delete, remote: true %>
#u like.html.erb
#_.html.erb
#将要销毁的该类用like_id属性替换此like_id。

您没有o pass artwork id

<%= link_to 'Like', likes_path(@artwork), method: :post, remote: true %>

<%= link_to 'UNLike', like_path(@artwork), method: :delete, remote: true %>

我以前没有遇到过这种“路线嵌套”。。。(我是一个noob)-这不妨碍likes是一个独立的模型/控制器吗?@Boaz,如果我理解正确,你的
likescoller
只负责创建和删除文章likes。如果情况并非如此,并且您有其他可能使用
LikesController
的资源,那么最好为每个喜欢和不喜欢定义自定义路由,否则我认为此解决方案最能解决您的问题。嗯,我想推断控制器内的like\u id(因为这是当前用户和@artwork之间的关系)-感觉更正确…在这种情况下,定制路线是合适的。但您的解决方案确实不错。谢谢
class LikesController < ApplicationController
  respond_to :html, :js

  def create
    @artwork = Artwork.find(params[:artwork_id])
    current_user.like!(@artwork)
    respond_with @artwork
  end

  def destroy
    @artwork = Artwork.find(params[:artwork_id])
    current_user.unlike!(@artwork)
    respond_with @artwork
  end
end
resources :artworks
resources :likes, only: [:create, :destroy]
resources :likes, only: [:create, :destroy]
# _like.html.erb
<%= link_to 'Like', likes_path(artwork_id: @artwork.id), method: :post, remote: true %>
likes POST    /likes(.:format)               likes#create
like  DELETE  /likes/:id(.:format)           likes#destroy
# app/config/routes.rb
resources :artworks do 
  resources :likes, only: [:create, :destroy]
end
# _like.html.erb
<%= link_to 'Like', artwork_likes_path(artwork_id: @artwork.id), method: :post, remote: true %>

# _unlike.html.erb
# Replace this_like_id with like_id attribute for this like being destroyed.
<%= link_to 'UNLike', artwork_likes_path(artwork_id: @artwork.id, id: this_like_id), method: :delete, remote: true %>
<%= link_to 'Like', likes_path(@artwork), method: :post, remote: true %>

<%= link_to 'UNLike', like_path(@artwork), method: :delete, remote: true %>
params[:id]