Pergunta

I'm having three models with nested resource.

class User < ActiveRecord::Base
  has_many :basiccases
end

class Basiccase < ActiveRecord::Base
  belongs_to :user
  has_one :form3_c      
end

class Form3C < ActiveRecord::Base
 belongs_to :basiccases
end


resources :basiccases  do
  resources :form3_cs
  end

I'm trying to send the basiccase to the new form using

<%= link_to "ADD Form3C", new_basiccase_form3_c_path(@basiccase)%>

and fetch the new value in the form using

<%= form_for([@form3c, @basiccase]) do |f| %> 

in new view in form_for method. My controller code

class Form3CsController <  ApplicationController
  #before_filter :authenticate

  def new
    @title = "New Form 3C"
    @basiccase = Basiccase.find_by_id(params[:id])

    @form3c = Form3C.new if signed_in?
  end

  def create       
       @form3c = @basiccase.build_form3_c(params[:form3c])
          if @form3c.save
            flash[:success] = "Form created!"
            redirect_to current_user
          else
            flash[:warning] ="Failed to create a Form"
            render 'users/show'
          end
  end
end

My main aim is to get the basiccase_id in the create method of form3_c controller and assign the foreign key attribute. Is my approach correct ?
I'm getting an error

undefined method `model_name' for NilClass:Class
Extracted source (around line #2):

1: <h1>Add Form 3C </h1>
2: <%= form_for(@basiccase) do |f| %>
3:     <%= render 'shared/error_messages', :object => f.object %>
4: 
5:     <div class="field">
Foi útil?

Solução

Try

<%= form_for([@basiccase, @form3c]) do |f| %> 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top