自引用关联 - Rails中的朋友特征

时间:2014-07-13 16:46:42

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

我正在尝试为基本社交网站构建一个简单的友情系统。我只需要我的" Wag"链接触发操作以将当前宠物收藏到所有者的收藏页面。我按照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

1 个答案:

答案 0 :(得分:0)

您需要更改此行:

<%= 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的实例上调用你正在调用的任何内容(我想它是基于你的db模型的pet