Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.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 在RubyonRails中添加好友_Ruby On Rails - Fatal编程技术网

Ruby on rails 在RubyonRails中添加好友

Ruby on rails 在RubyonRails中添加好友,ruby-on-rails,Ruby On Rails,我跟随这篇文章在RubyonRails中将用户添加为好友 我可以通过rails控制台将用户添加为好友,但不能通过实际页面……这是我的控制器代码 class FriendshipsController < ApplicationController def new end def create @friendship = current_user.friendships.build(:friend_id => params[:friend_id])

我跟随这篇文章在RubyonRails中将用户添加为好友

我可以通过rails控制台将用户添加为好友,但不能通过实际页面……这是我的控制器代码

class FriendshipsController < ApplicationController
  def new
  end

  def create  
    @friendship = current_user.friendships.build(:friend_id => params[:friend_id])  
    if @friendship.save  
      flash[:notice] = "Added friend."  
      redirect_to phones_index_path  
    else  
      flash[:notice] = "Unable to add friend."  
      redirect_to phones_index_path
    end  
  end 

  def show
  end
end
类友谊控制器params[:friend\u id])
如果@friendy.save
flash[:notice]=“添加了朋友。”
重定向到电话索引路径
其他的
flash[:notice]=“无法添加好友。”
重定向到电话索引路径
结束
结束
def秀
结束
结束
型号: 用户:

class用户:友谊
有很多:反向友谊,:类名称=>“友谊”;:外键=>“朋友id”
有很多:反向朋友,:通过=>:反向朋友,:来源=>:用户
#设置模型的可访问(或受保护)属性
属性可访问:电子邮件、:密码、:密码确认、:记住我、:用户id
#可访问属性:标题,:正文
结束
朋友们:

class Friendship < ActiveRecord::Base
  attr_accessible :friend_id, :user_id
  belongs_to :user
  belongs_to :friend, :class_name => 'User'
end
类友谊'User'
结束
视图:

Phones#索引
系统中的用户

@user),:方法=>:post%> 朋友

我的观点是正确的。。如果我通过控制台添加朋友并提交,它将显示在视图中。。
我在这里哪里出错了???

我能够纠正它..我的视图文件中有错误的代码。。非常感谢jvnill…如果不是因为您,我不会检查我的视图文件…:)

更改
友谊\u路径(:friend\u id=>@user)

friendships\u path(:friend\u id=>变量)执行了此操作

如何获取
参数[:friend\u id]
?您应该发布您的模型和视图。我只是在视图文件中显示了firend的电子邮件id。您误解了我要求的视图。您能否显示用户最有可能选择要添加的朋友的视图?在这一行:

@user),:method=>:post%>
,如果将
@user
更改为
@user.id
,会发生什么情况?如果它不起作用,您可以发布日志跟踪吗?无法提供user.id,因为在rails 3中禁止大规模访问
class Friendship < ActiveRecord::Base
  attr_accessible :friend_id, :user_id
  belongs_to :user
  belongs_to :friend, :class_name => 'User'
end
<h1>Phones#index</h1>
<center>Users in the system<center><br/>
<p><% @phones.each do |variable|%>
    <% if @phn.email!= variable.email %>
    <br/><%= variable.email %> &nbsp; <%= link_to 'Add as Friend' , friendships_path(:friend_id => @user), :method => :post %>
    <%end%>
    <%end%>
<p>friends</p>
<% for user in current_user.friends %>
<p><%= user.email %></p>
<%end%>