【问题标题】:Self-Referential Association - Friending Feature in Rails自引用关联 - Rails 中的交友功能
【发布时间】:2014-07-13 16:46:42
【问题描述】:

我正在尝试为一个基本的社交网站构建一个简单的交友系统。我只需要我的“摇摆”链接来触发将当前宠物收藏到所有者收藏页面的操作。我在这里按照RailsCast 进行了设置。

这是我要设置的路径链接。

<% @pets.each do |pet| %>
  <div>
   <%= link_to "Wag", owner_friendships_path(friend_id: owner), method: :post %>
  </div>
 <% end %>
</div>

应该放在这里:

<ul>
 <% for friendship in @owner.friendships %>
  <li>
   <%= friendship.friend.name %>
   (<%= link_to "remove", friendship, method: :delete %>)
   </li>
  <% end %>
</ul>

<p><%= link_to "Find Puppy Pals", pets_path %></p>

路线:

 Rails.application.routes.draw do
  devise_for :owners

  resources :pets

  resources :owners do
   resources :pets, shallow: true
   resources :friendships
  end

  root to: "pets#index"
end

相关架构如下所示:

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

宠物桌:

create_table "pets", force: true do |t|
 t.string   "name"
 t.string   "breed"
 t.integer  "age"
 t.integer  "weight"
 t.text     "personality"
 t.text     "favorite_things"
 t.integer  "owner_id"
 t.datetime "created_at"
 t.datetime "updated_at"
 t.string   "profile_photo"
end

所有者控制器:

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

  def edit
   @owner = Owner.find(params[:id])
  end
 end

【问题讨论】:

  • @pets 定义在哪里?
  • 更新了我的帖子以包含宠物控制器
  • 在这种情况下,我的答案是正确的 - 您应该在块中的 pet 实例上调用 owner_id
  • 你能发布你的pets 表吗?
  • 我更新了宠物表。

标签: ruby-on-rails ruby-on-rails-4 associations social-networking


【解决方案1】:

你需要改变这一行:

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

到这里:

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

owner 永远不会被自己定义。无论您调用什么(我猜它是 owner_id 基于您的数据库模型)都需要在块中的 pet 实例上调用

【讨论】:

  • 哦,抱歉,我没有提到我确实有一个所有者控制器/模型。
  • 基于此,这应该仍然有效。错误来自宠物控制器中的索引操作 - 与所有者控制器无关
  • 好的,所以在正确的嵌套路由中添加了它,从技术上讲,它是 owner_friendships_path 而不是friendships_path。使用“pet.owner_id”,我得到No route matches {:action=&gt;"index", :controller=&gt;"friendships", :friend_id=&gt;nil} missing required keys: [:owner_id] 没有那个link_to,pets#index 可以正常工作。
  • pet.owner_id 是正确的-也许您需要将密钥名称-friend_id 更改为owner_id
  • 将其从friend_id 更改为owner_id 似乎并没有什么不同。然而,朋友链接应该去往'owners/:id/friendships'的路线告诉我undefined method friendships' for nil:NilClass`,所以也许整个问题是它链接到一个损坏的页面?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多