Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a new nested resource?
    primarykey
    data
    text
    <p>A users can have many favorites top_songs,top_movies through songs and movies table. A user registered user(current_user) want to post his favorites movies and songs.</p> <p>Perhaps all Model association are right, i am stuck in controller and view (form). </p> <p>When i submit from, i gets errors-</p> <p><strong>Can't mass-assign protected attributes: songs</strong></p> <p>How can i achieve this please? all codes are below. </p> <p><strong>User Model</strong></p> <pre><code> class User &lt; 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 =&gt; true accepts_nested_attributes_for :top_movies, :allow_destroy =&gt; true end </code></pre> <p><strong>Movie Model</strong></p> <pre><code>class Movie &lt; ActiveRecord::Base attr_accessible :name has_many :top_movies has_many :users, through: :top_movies end </code></pre> <p><strong>TopMovie Model</strong></p> <pre><code>class TopMovie &lt; ActiveRecord::Base belongs_to :user belongs_to :movie # attr_accessible :title, :body end </code></pre> <p><strong>Song Model</strong></p> <pre><code>class Song &lt; ActiveRecord::Base attr_accessible :name has_many :top_songs has_many :users, through: :top_songs end </code></pre> <p><strong>TopSong Model</strong></p> <pre><code>class TopSong &lt; ActiveRecord::Base belongs_to :user belongs_to :song # attr_accessible :title, :body end </code></pre> <p><strong>Controller</strong></p> <pre><code>class MyTopFivesController &lt; 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 </code></pre> <p><strong>view form</strong></p> <pre><code> &lt;%=nested_form_for @favorites ,:url=&gt;favorites_path(@favorites),:method=&gt;'post' do |f| %&gt; &lt;label &gt;Songs&lt;/label&gt; &lt;%= f.fields_for :songs do |songs| %&gt; &lt;div id="Topsongs" &gt; &lt;div class="input-control text span5 place-left "&gt; &lt;%= songs.text_field :name,:placeholder=&gt;"songs name.." %&gt; &lt;/div&gt; &lt;div class="span1 place-left"&gt; &lt;%= songs.link_to_remove "", :class=&gt;"icon-minus" %&gt; &lt;/div&gt; &lt;/div&gt; &lt;% end %&gt; &lt;span &gt; &lt;%= f.link_to_add "", :songs, :class=&gt;"icon-plus", :data =&gt; { :target =&gt; "#Topsongs" } %&gt; &lt;/span&gt; &lt;label &gt;movies&lt;/label&gt; &lt;%= f.fields_for :movies do |movies| %&gt; &lt;div id="Topmovies"&gt; &lt;div class="input-control text span5 place-left "&gt; &lt;%= movies.text_field :name,:placeholder=&gt;"movies name.." %&gt; &lt;/div&gt; &lt;div class="span1 place-left"&gt; &lt;%= movies.link_to_remove "", :class=&gt;"icon-minus" %&gt; &lt;/div&gt; &lt;/div&gt; &lt;% end %&gt; &lt;span&gt; &lt;%= f.link_to_add "", :movies, :class=&gt;"icon-plus",:style=&gt;"font-size: 14px;", :data =&gt; { :target =&gt; "#Topmovies" } %&gt; &lt;/span&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload