Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/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中的自引用关联友好特性_Ruby On Rails_Ruby On Rails 4_Associations_Social Networking - Fatal编程技术网

Ruby on rails Rails中的自引用关联友好特性

Ruby on rails Rails中的自引用关联友好特性,ruby-on-rails,ruby-on-rails-4,associations,social-networking,Ruby On Rails,Ruby On Rails 4,Associations,Social Networking,我正在尝试为一个基本的社交网站建立一个简单的交友系统。我只需要我的“Wag”链接就可以触发一个动作,将当前宠物收藏到主人的收藏夹页面。我按照这里的指示来安排一切 这是我正在尝试设置的路径链接 <% @pets.each do |pet| %> <div> <%= link_to "Wag", owner_friendships_path(friend_id: owner), method: :post %> </div> <%

我正在尝试为一个基本的社交网站建立一个简单的交友系统。我只需要我的“Wag”链接就可以触发一个动作,将当前宠物收藏到主人的收藏夹页面。我按照这里的指示来安排一切

这是我正在尝试设置的路径链接

<% @pets.each do |pet| %>
  <div>
   <%= link_to "Wag", owner_friendships_path(friend_id: owner), method: :post %>
  </div>
 <% end %>
</div>
相关架构如下所示:

create_table "friendships", force: true do |t|
 t.integer  "owner_id"
 t.integer  "friend_id"
 t.datetime "created_at"
 t.datetime "updated_at"
end

create_table "owners", force: true do |t|
 t.string   "email",                  default: "", null: false
 t.string   "encrypted_password",     default: "", null: false
 ...(devise defaults)
 t.datetime "created_at"
 t.datetime "updated_at"
 t.string   "profile_photo"
end
我的友谊控制员:

class FriendshipsController < ApplicationController
 def index
  @friendships = Friendship.all
 end

 def show
  @friendship = Friendship.find(params[:id])
 end

def create
 @friendship = current_owner.friendships.build(params[:friend_id])
 if @friendship.save
  flash[:notice] = "Added puppy pal."
  redirect_to root_url
 else
  flash[:error] = "Oops, we couldn't save that puppy pal."
  redirect_to root_url
 end
end

def destroy
  @friendship = current_owner.friendships.find(params[:id])
  @friendship.destroy
  flash[:notice] = "Removed puppy pal."
  redirect_to root_url
 end
end
Class PetsController < ApplicationController

 def index
  @pets = Pet.order("RANDOM()").limit(1)
 end

 def new
  @pet = Pet.new
 end

def create
 @pet = Pet.new(pet_params)
 @pet.owner_id = current_owner.id if current_owner

if @pet.save
  flash[:notice] = 'Your pet profile was saved!'
  redirect_to pets_path
else
  flash.now[:notice] = 'Your pet profile could not be saved.'
  render :new
 end
end

def show
 @pet = Pet.find(params[:id])
end

def edit
 @pet = Pet.find(params[:id])
end

def update
 @pet = Pet.find(params[:id])

 if @pet.update(pet_params)
  redirect_to pets_path
 else
  render :edit
 end
end

def destroy
 @pet = Pet.find(params[:id])

 if current_owner == @pet.owner
  @pet.destroy
  redirect_to pets_path
 end
end

private

def pet_params
 params.require(:pet).permit(:name, :breed, :age,
  :color, :weight, :personality, :favorite_things, :owner_id, :profile_photo)
 end
end
 class OwnersController < ApplicationController
  def show
  @pet = Pet.find(params[:id])
 end

  def edit
   @owner = Owner.find(params[:id])
  end
 end
业主控权人:

class FriendshipsController < ApplicationController
 def index
  @friendships = Friendship.all
 end

 def show
  @friendship = Friendship.find(params[:id])
 end

def create
 @friendship = current_owner.friendships.build(params[:friend_id])
 if @friendship.save
  flash[:notice] = "Added puppy pal."
  redirect_to root_url
 else
  flash[:error] = "Oops, we couldn't save that puppy pal."
  redirect_to root_url
 end
end

def destroy
  @friendship = current_owner.friendships.find(params[:id])
  @friendship.destroy
  flash[:notice] = "Removed puppy pal."
  redirect_to root_url
 end
end
Class PetsController < ApplicationController

 def index
  @pets = Pet.order("RANDOM()").limit(1)
 end

 def new
  @pet = Pet.new
 end

def create
 @pet = Pet.new(pet_params)
 @pet.owner_id = current_owner.id if current_owner

if @pet.save
  flash[:notice] = 'Your pet profile was saved!'
  redirect_to pets_path
else
  flash.now[:notice] = 'Your pet profile could not be saved.'
  render :new
 end
end

def show
 @pet = Pet.find(params[:id])
end

def edit
 @pet = Pet.find(params[:id])
end

def update
 @pet = Pet.find(params[:id])

 if @pet.update(pet_params)
  redirect_to pets_path
 else
  render :edit
 end
end

def destroy
 @pet = Pet.find(params[:id])

 if current_owner == @pet.owner
  @pet.destroy
  redirect_to pets_path
 end
end

private

def pet_params
 params.require(:pet).permit(:name, :breed, :age,
  :color, :weight, :personality, :favorite_things, :owner_id, :profile_photo)
 end
end
 class OwnersController < ApplicationController
  def show
  @pet = Pet.find(params[:id])
 end

  def edit
   @owner = Owner.find(params[:id])
  end
 end
class OwnersController
您需要更改此行:

<%= link_to "Wag", friendships_path(friend_id: owner), method: :post %>

为此:

<%= link_to "Wag", friendships_path(friend_id: pet.owner_id), method: :post %>


所有者
从未自行定义。无论您在呼叫什么(我猜它是基于您的db模型的
owner\u id
)需要在块中的
pet
实例上调用
@pets
定义在哪里?更新了我的帖子以包括宠物管理员在这种情况下我的答案是-你应该在块中的
pet
实例上调用
owner\u id
你可以发布你的
pets
表吗?我用宠物更新了表。哦,对不起,我没提到我有一个所有者控制器/模型。基于此,这应该仍然有效。错误来自pets控制器中的索引操作-与所有者控制器Rokay无关,因此在正确的嵌套路由中添加该操作后,从技术上讲,它是所有者友谊路径,而不是友谊路径。有了“pet.owner\u id”,我得到的
没有路由匹配{:action=>“index”,:controller=>“friendships”,:friends\u id=>nil}缺少所需的键:[:owner\u id]
没有该链接,宠物索引在其他情况下工作正常。
pet.owner\u id
是正确的-也许你需要将密钥名-
friend\u id
更改为
owner\u id
将其从friend\u id更改为owner\u id似乎没有什么区别。然而,朋友链接应该在'owners/:id/friendships'处的路径告诉我,nil:NilClass`的
未定义方法
friendships',所以可能整个问题是它链接到了一个断开的页面?