1

用戶可以通過歌曲和電影表擁有許多收藏夾top_songs,top_movies。 用戶註冊用戶(current_user)想要發佈他的最愛電影和歌曲。如何創建新的嵌套資源?

也許所有模型關聯都是正確的,我卡在控制器和視圖(窗體)中。

當我提出,我得到錯誤 -

不能大規模指派保護的屬性:歌曲

我怎麼能做到這一點嗎?所有的代碼都在下面。

用戶模型

class User < ActiveRecord::Base 
attr_accessible :id, :name_special_char, :screenname, :fullname, :username, :prefix, :firstname, :lastname,:middlename, :suffix, :age, :sex, :email, 
:top_movies_attributes,:top_songs_attributes 



    has_many :top_movies 
    has_many :movies, through: :top_movies 

    has_many :top_songs 
    has_many :songs, through: :top_songs 

accepts_nested_attributes_for :top_songs, :allow_destroy => true 
accepts_nested_attributes_for :top_movies, :allow_destroy => true 

end 

電影模型

class Movie < ActiveRecord::Base 
    attr_accessible :name 

    has_many :top_movies 
    has_many :users, through: :top_movies 
end 

TopMovie模型

class TopMovie < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :movie 
    # attr_accessible :title, :body 
end 

宋模型

class Song < ActiveRecord::Base 
    attr_accessible :name 

    has_many :top_songs 
    has_many :users, through: :top_songs 
end 

TopSong模型

class TopSong < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :song 
    # attr_accessible :title, :body 
end 

控制器

class MyTopFivesController < ApplicationController 
    def new 
    @favorites = current_user 
    @[email protected]() 
    @[email protected]() 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @useraccounts_my_top_fife } 
    end 
    end 

def create 
    @favorites = current_user(params[:user]) 
    @favorites.save! 
         # Here i have stuck. i am not sure how to save.  
end 

視圖形式

<%=nested_form_for @favorites ,:url=>favorites_path(@favorites),:method=>'post' do |f| %> 
     <label >Songs</label> 
      <%= f.fields_for :songs do |songs| %> 
      <div id="Topsongs" > 
       <div class="input-control text span5 place-left "> 
        <%= songs.text_field :name,:placeholder=>"songs name.." %> 
       </div> 
       <div class="span1 place-left"> 
        <%= songs.link_to_remove "", :class=>"icon-minus" %> 
       </div> 
      </div> 
     <% end %>  
     <span > 
      <%= f.link_to_add "", :songs, :class=>"icon-plus", :data => { :target => "#Topsongs" } %> 
     </span> 
     <label >movies</label>  
      <%= f.fields_for :movies do |movies| %> 
       <div id="Topmovies"> 
        <div class="input-control text span5 place-left "> 
         <%= movies.text_field :name,:placeholder=>"movies name.." %> 
        </div> 
        <div class="span1 place-left"> 
         <%= movies.link_to_remove "", :class=>"icon-minus" %> 
        </div> 
       </div> 
      <% end %>  
     <span> 
      <%= f.link_to_add "", :movies, :class=>"icon-plus",:style=>"font-size: 14px;", :data => { :target => "#Topmovies" } %> 
     </span> 

    <div class="actions"> 
     <%= f.submit %> 
    </div> 
    <% end %> 

回答

0

這裏有兩種設置關聯的選項。通過爲頂級歌曲和頂級電影創建兩個連接模型,您可以使用它。和其他,使用多態關聯。

讓我們使用多態關聯。我們將使用用戶,電影,歌曲和最喜歡的模型這個東西。 Favorite模型將包含多態字段。

User.rb

class User < ActiveRecord::Base 
    attr_accessible :id, :name_special_char, :screenname, :fullname, :username, :prefix, :firstname, :lastname,:middlename, :suffix, :age, :sex, :email  

    has_many :favourites 
    has_many :movies, through: :favourites, source: :favouritable, source_type: 'Movie' 
    has_many :songs, through: :favourites, source: :favouritable, source_type: 'Song' 

end 

Movie.rb

class Movie < ActiveRecord::Base 
    attr_accessible :name 

    has_many :favourites, as: :favouritable 
    has_many :users, through: :favourites 
end 

Song.rb

class Song < ActiveRecord::Base 
    attr_accessible :name 

    has_many :favourites, as: :favouritable 
    has_many :users, through: :favourites 
end 

喜愛。RB

class Favourite < ActiveRecord::Base 
    belongs_to :users 
    belongs_to :favouritable, polymorphic: true 
end 

我們還需要創造新模式「收藏」的遷移。截至目前,我們只需要3列,即。 user_id, favouritable_id, favouritable_type。這裏favouritable_type和favouritable_id是多態字段。 favouritable_type是一個字符串,favouritable_id是引用類型。

遷移文件

class CreateFavourites < ActiveRecord::Migration 
    def change 
    create_table :favourites do |t| 
    t.integer :user_id 
    t.references :favouritable, polymorphic: true 
    t.timestamps 
    end 
    end 
end 

現在,我們要紀念的一些電影和歌曲是最喜歡的一個用戶,那麼我們可以將代碼在UsersController建設中的數據,而不是創建另一個控制器或我們也可以爲Favourites創建一個控制器。我將在這裏使用UsersController。我正在使用更新操作來更新收藏夾,因爲我們在這裏不需要任何額外的功能。如果你願意,你可以添加一個新的動作。

在UsersController.rb

def edit_favourites #or some generic name 
    @user = current_user.includes(:movies, :songs) 
    @movies = Movie.all 
    @songs = Song.all 
end 

def update 
    @user = User.find(params[:id]) 

    if @user.update_attributes(params[:user]) 
    redirect_to users_path #user index page 
    else 
    if params[:user][:movie_ids].present? or params[:user][:song_ids].present? 
     render :edit_favourites 
    else 
     render :edit 
    end 
    end 
end 

edit_favourites.html.erb

<%= form_for(@user) do |f| %> 
    <div class="fields"> 
    <%= f.label :movie_ids, "Favourite Movies: " %> 
    <%= f.collection_select :movie_ids, @movies, :id, :name, {}, multiple: true %> 
    </div> 
    <div class="fields"> 
    <%= f.label :song_ids, "Favourite Songs: " %> 
    <%= f.collection_select :song_ids, @songs, :id, :name, {}, multiple: true %> 
    </div> 
<% end %> 

此外,添加新的行動路線。

+1

謝謝@Manoj Mong。 – Gagan