Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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 在rails 3中使用ajax呈现片段时出现空白页_Ruby On Rails_Ruby_Ruby On Rails 3_Jquery - Fatal编程技术网

Ruby on rails 在rails 3中使用ajax呈现片段时出现空白页

Ruby on rails 在rails 3中使用ajax呈现片段时出现空白页,ruby-on-rails,ruby,ruby-on-rails-3,jquery,Ruby On Rails,Ruby,Ruby On Rails 3,Jquery,我有一个导航菜单,在show.html.erb文件中有两个选项卡/链接,在userscocontroller.rb中,我想使用ajax为选项卡呈现不同的部分 在show.html.erb中,我有一个名为profile data的div,我想在其中显示内容。 所以我做了这样的事情: 链接结构: <li><%= link_to "College friends", college_friends_path, :remote => true %></li> &

我有一个导航菜单,在
show.html.erb
文件中有两个选项卡/链接,在
userscocontroller.rb
中,我想使用ajax为选项卡呈现不同的部分

show.html.erb
中,我有一个名为
profile data
的div,我想在其中显示内容。 所以我做了这样的事情:

链接结构:

<li><%= link_to "College friends", college_friends_path, :remote => true %></li>
<li><%= link_to "Highschool friends", highschool_friends_path, :remote => true %></li>
match "college_friends" => "users#college_friends", :as => "college_friends"
match "highschool_friends" => "users#highschool_friends, :as => "highschool_friends"
我在我的UserController.rb中定义了必要的方法:

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end

  def college_friends
    respond_to do |format|
      format.js
    end
  end

  def highschool_friends
    respond_to do |format|
      format.js
    end
  end
end

正如上面评论中所建议的,您应该有两个部分,分别命名为
\u college\u friends.html.erb
\u highschool\u friends.html.erb

这些文件将包含您的HTML,这是您希望通过
$('#profile data').HTML()加载的内容

模板错误:

ActionView::Template::Error (undefined method `friends' for nil:NilClass):
您没有在
大学好友
高中好友
中定义
@user
。因此,您的视图正在从该操作请求
@user
,而该操作不存在

你正在得到

ActionView::Template::Error (undefined method `friends' for nil:NilClass):
因为

<% groups = @user.friends.group_by(&:college_name) %>
但是

要实现这一点,您需要更改链接路由以包含用户id,类似于此

match "college_friends/:id" => "users#college_friends", :as => "college_friends"
在你的链接中

<li><%= link_to "College friends", college_friends_path(@user), :remote => true %></li>
  • 正确%>

  • 大学朋友和高中朋友的聚会都有什么内容?您应该在同一文件夹中有_college_friends.format.erb和_highscool_friends.format.erb,以便执行此操作work@anthonyalberto,如果我按照使用说明执行操作,则会出现此错误:模板丢失。\u college\u friends.format.erb=>是否将.format替换为实际格式?在这个例子中,我想应该是.js.erb!当然,我必须对命名为liket的partials进行修改!这不是问题,它们保存在哪个视图目录中?“模板丢失”表示您没有要加载的部分。你能粘贴整个错误信息吗?我怀疑它正在查看视图/用户/I修复模板错误。我现在得到的只是一个空白页面。它会将页面发送到本地主机/用户/大学朋友扫描你的部分代码?此外,请检查浏览器中的http请求,以确保单击链接时返回了某些内容。请检查更新。你说什么东西要退货?谢谢!它可以工作,但每次更改选项卡时都会向数据库发出新的req。。这是非常低效的。你对我如何重新思考这个问题还有什么想法吗。我还是会给你答案的。如果有帮助,我很高兴。它认为你可以问一个新问题,或者寻找一点你的新问题。但我建议您在一个请求中呈现所有内容,并在用户单击链接时“显示/隐藏”页面的相关部分。现在,您不会向服务器发送新的请求,而是触发一些js隐藏或显示好友标签。但这一次,您“可能”没有最新的数据。这也是另一个问题:)
    ActionView::Template::Error (undefined method `friends' for nil:NilClass):
    
    ActionView::Template::Error (undefined method `friends' for nil:NilClass):
    
    <% groups = @user.friends.group_by(&:college_name) %>
    
    def college_friends
      @user = User.find(params[:id])
      respond_to do |format|
        format.js
      end
    end
    
    match "college_friends/:id" => "users#college_friends", :as => "college_friends"
    
    <li><%= link_to "College friends", college_friends_path(@user), :remote => true %></li>