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

トップムービー モデル

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

トップソングモデル

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

コントローラ

class MyTopFivesController < ApplicationController
  def new
    @favorites = current_user
    @favorites=@favorites.movies.build()
    @favorites=@favorites.songs.build()

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

1 に答える 1