Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I create form_for for namespaced nested resources in Rails 3.1.3 and Mongoid?
    text
    copied!<p>I am new to Rails and am trying to create an Api for a mobile app. Although the Api will generate json responses, I also need a web interface to create the data. My routes look like this:</p> <pre><code>Appone::Application.routes.draw do namespace :api1 do resources :reldates do resources :movies end end end </code></pre> <p>The release_date model:</p> <pre><code>class Api1::Reldate include Mongoid::Document include Mongoid::MultiParameterAttributes include Mongoid::Timestamps field :release_date, :type =&gt; Date embeds_many :api1_movies validates_associated :movie accepts_nested_attributes_for :movie end </code></pre> <p>The Movie Model:</p> <pre><code>class Api1::Movie include Mongoid::Document include Mongoid::MultiParameterAttributes include Mongoid::Timestamps field :title, :type =&gt; String field :release_type, :type =&gt; String field :release_number, :type =&gt; Integer embedded_in :api1_reldate, :inverse_of =&gt; :api1_movies end </code></pre> <p>and the reldate show.html.erb:</p> <pre><code>&lt;p id="notice"&gt;&lt;%= notice %&gt;&lt;/p&gt; &lt;p&gt; &lt;b&gt;Release date:&lt;/b&gt; &lt;%= @api1_reldate.release_date %&gt; &lt;/p&gt; &lt;% if @api1_reldate.api1_movies.size &gt; 0 %&gt; &lt;h2&gt;Movies&lt;/h2&gt; &lt;% for api1_movie in @api1_reldate.api1_movies %&gt; &lt;h3&gt;&lt;%= api1_movie.title %&gt;&lt;/h3&gt; &lt;p&gt;&lt;%= api1_movie.release_type %&gt;&lt;/p&gt; &lt;p&gt;&lt;%= api1_movie.release_number %&gt;&lt;/p&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;h2&gt;New Movies&lt;/h2&gt; &lt;% form_for([:api1], :url =&gt; { :action =&gt; :create ,:id =&gt; @reldate.movie}) do |form| %&gt; &lt;p&gt;&lt;%= f.label :title %&gt; &lt;%= f.text_field :title %&gt;&lt;/p&gt; &lt;p&gt;&lt;%= f.label :release_type %&gt; &lt;%= f.text_field :release_type %&gt;&lt;/p&gt; &lt;p&gt;&lt;%= f.label :release_number %&gt; &lt;%= f.number_field :release_number %&gt;&lt;/p&gt; &lt;p&gt;&lt;%= f.submit %&gt;&lt;/p&gt; &lt;% end %&gt; &lt;%= link_to 'Edit', edit_api1_reldate_path(@api1_reldate) %&gt; | &lt;%= link_to 'Back', api1_reldates_path %&gt; </code></pre> <p>I am unable to get this to work. I can get the reldate page working but after I click on submit, I get an error like this:</p> <pre><code>undefined method `movie' for nil:NilClass Extracted source (around line #18): 15: &lt;% end %&gt; 16: 17: &lt;h2&gt;New Movies&lt;/h2&gt; 18: &lt;% form_for([:api1], :url =&gt; { :action =&gt; :create ,:id =&gt; @reldate.movie}) do |form| %&gt; 19: &lt;p&gt;&lt;%= f.label :title %&gt; &lt;%= f.text_field :title %&gt;&lt;/p&gt; 20: &lt;p&gt;&lt;%= f.label :release_type %&gt; &lt;%= f.text_field :release_type %&gt;&lt;/p&gt; 21: &lt;p&gt;&lt;%= f.label :release_number %&gt; &lt;%= f.number_field :release_number %&gt;&lt;/p&gt; </code></pre> <p>The movie controller look like this:</p> <pre><code>class Api1::MoviesController &lt; ApplicationController def create @api1_reldate = Api1::Reldate.find(params[:api1_reldate_id]) @api1_movie = @api1_reldate.api1_movies.create!(params[:api1_movie]) redirect_to @api1_reldate, :notice =&gt; "Movie successfully created!" end end </code></pre> <p>How can I get this to work? I have tried every possible solution available, but it keeps failing. Also, I am using Ryan Bates <a href="http://railscasts.com/episodes/238-mongoid" rel="nofollow">Railcast</a> </p> <p>Thanks in advance.</p>
 

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