Rails协会简单的问题

时间:2014-05-20 10:21:43

标签: ruby-on-rails ruby

我目前正在制作一个演示应用程序,允许用户选择一个节目,然后将该节目添加到他们的帐户。但是,多个用户可以拥有相同的节目。

我现在设置的方式如下:

###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

那么需要添加什么才能让用户点击按钮&#34;添加&#34; (目前它只链接到#)然后将该节目添加到他们的帐户?

这是我在视图中的当前添加按钮:

<% @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 %>

我还根据Rich的帮助更新了我的控制器。

更新 这是我的其他文件 的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

views / shows / 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"

2 个答案:

答案 0 :(得分: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


按钮

shows添加到User实际上相对简单

使用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 :(得分:0)

在用户模型中定义另一个关系

class User < ActiveRecord::Base
  has_many :user_shows

执行添加按钮将在params id

中发送show_id
current_user.user_shows.create(:show_id => params[:id])