2014-05-20 63 views
1

我目前有一個演示應用程序即時製作,它將允許用戶選擇一個節目,然後將該節目添加到他們的帳戶。但是,多個用戶可以擁有相同的演出。Rails協會簡單問題

我有它成立的那一刻的方法如下:

###models/user.rb#### 
class User < ActiveRecord::Base 
    has_many :shows, through: :user_shows 

###models/show.rb#### 
class Show < ActiveRecord::Base 
    has_many :user_shows 
    has_many :users, through: :user_shows 

###models/user_show.rb#### 
class UserShow < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :show 

那麼需要增加,這樣,當用戶點擊一個按鈕「添加」(目前只鏈接到一個# )那麼這會將該節目添加到他們的帳戶中?

這是我的看法目前Add按鈕:

<% @shows.each do |show| %> 
      <div class="well well-sm welly"> 
      <p><%= link_to show.title, show.url, class: "btn btn-primary", target: "blank" %> 
       <%= link_to "Add Show", users_add_show_path, class: "btn btn-danger addbt" %> 
      </p> 
      </div> 
     <% end %> 

我也更新了我的控制器按照理查德的幫助。

UPDATE 這裏是我的其他文件 的routes.rb

ShowReminder::Application.routes.draw do 
    devise_for :users 
    resources :shows 
    resources :users do 
    collection do 
     post :add_show #-> should create /users/add_show, considering you have current_user 
    end 
end 
    root 'pages#home' 
    get 'features' => 'pages#features' 
    get 'my_shows' => 'pages#my_shows' 

users_controller.rb

class UsersController < ApplicationController 



    def add_show 
     show = Show.find params[:id] 
     current_user.shows << show 
     redirect_to root_path 
    end 

    def show 
    end 

end 

的意見/顯示/ index.html.erb(只是其中的一部分)

<div> 
     <% @shows.each do |show| %> 
      <div class="well well-sm welly"> 
      <p><%= link_to show.title, show.url, class: "btn btn-primary", target: "blank" %> 
       <%= link_to "Add Show", add_show_users(show.id), class: "btn btn-danger addbt" %> 
      </p> 
      </div> 
     <% end %> 
</div> 
<div> 
    <%= will_paginate @shows, renderer: BootstrapPagination::Rails, :inner_window => 1, :outer_window => 0, class: "paginates" %> 

個錯誤消息:

錯誤1:無

def show 
end 

在我Userscontroller

The action 'show' could not be found for UsersController 

誤差2:隨着它在我的控制器:

Missing template users/show, application/show with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :coffee]}. Searched in: * "C:/Users/Harrison/Documents/Projects/show_reminder/app/views" * "C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/devise-3.2.4/app/views" 

回答

1

多個用戶可以有相同的節目

你會是最好看在Rails的many-to-many relationships 。這是您使用join model或類似工具將多個項目關聯到多個對象的地方。有兩種類型的m-t-m關係:


代碼

你會最好做這樣的事情:

#app/models/user.rb 
has_many :user_shows 
has_many :shows, through: :user_shows 

#app/models/user_show.rb 
belongs_to :user 
belongs_to :show 

#app/models/show.rb 
has_many :user_shows 
has_many :users, through: :user_shows 

這是經典has_many :through

enter image description here

這將允許你打電話@user.shows@show.users


按鈕

添加showsUser實際上是相對簡單的

使用many-to-many結構,您的shows將是ActiveRecord Collection。這樣,你就可以訪問<<方法,可以這樣調用:

#app/controllers/users_controller.rb 
def add_show 
    show = Show.find params[:id] 
    current_user.shows << show # -> only works with AR objects. current_user is AR object 
end 

這將允許你做這樣的事情:

#app/views/users/add_show.html.erb 
<%= form_tag users_add_show_path do %> 
    <%= text_field_tag :id, placeholder: "Show ID" %> #-> should be a select box - can refactor 
    <%= submit_tag "Add" %> 
<% end %> 

更新

#config/routes.rb 
resources :users do 
    collection do 
     post :add_show #-> should create /users/add_show, considering you have current_user 
    end 
end 
+1

感謝Rich,這幫助我更多地理解它。我更新了我的問題,包括我目前的添加按鈕,但由於某種原因,它不起作用,如果你可以看看,這將是一個很大的幫助。 Thaks – HarryLucas

+0

你有'users_add_show_path',但沒有傳入任何'id'。你需要'users_add_show_path(show.id)'或類似的 –

+0

hmm我嘗試了一些變化,但似乎無法讓它工作。此外,我還以爲我正在經歷@ show.each | show |這意味着它會從那裏採取show_id?這是正確的嗎? – HarryLucas

0

在用戶模型中定義一個以上關係

class User < ActiveRecord::Base 
    has_many :user_shows 

在行動,其中添加按鈕會打發送show_id params中id

current_user.user_shows.create(:show_id => params[:id]) 
+0

感謝您的回覆!只是最後一點我有點困惑。我將如何將它添加到此? '<%= link_to「Add Show」,'#'%>' 因爲如果我用它替換#我得到一個錯誤 – HarryLucas