View Model Form From Within Another Rails -
i working in rails, , have application want 1 user able create instance of model user show page.
i have 2 tables, 1 users , 1 cars. have relationships set 1 user has_many :cars, , each car belongs_to :user
when user logs in first time, if user.cars = 0 want page display form add car @ users show page.
i working michael hartl's tutorial great, , tried use micropost model work in situtation getting error can't figure out.
here show.html.erb:
<p id="notice"><%= notice %></p> <% provide(:title, @user.name) %> <div class="userwrap"> <div class="usershow"> <div class="row"> <section> <h1> <%= gravatar_for @user %> <%= @user.name %> </h1> </section> </div> <p> <%= image_tag @user.image.url(:medium) %> </p> <p> <strong>name:</strong> <%= @user.name %> </p> <p> <strong>email:</strong> <%= @user.email %> </p> <%= link_to 'edit', edit_user_path(@user) %> | <%= link_to 'back', users_path %> </div> <div class="usercars"> <%= render 'shared/carform' %> </div> </div> here car form
<%= form_for(@car) |f| %> <% if @car.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@car.errors.count, "error") %> prohibited car being saved:</h2> <ul> <% @car.errors.full_messages.each |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :name %><br> <%= f.text_field :name %> </div> <div class="field"> <%= f.label :nick %><br> <%= f.text_field :nick %> </div> <div class="field"> <%= f.label :model %><br> <%= f.text_field :model %> </div> <div class="field"> <%= f.label :make %><br> <%= f.text_field :make %> </div> <div class="field"> <%= f.label :neighborhood %><br> <%= f.text_field :neighborhood %> </div> <div class="field"> <%= f.label :year %><br> <%= f.number_field :year %> </div> <div class="field"> <%= f.label :picture %><br> <%= f.text_field :picture %> </div> <div class="actions"> <%= f.submit %> </div> <% end %> here controller show
def show @user = user.find(params[:id]) @cars = @user.cars end the error
first argument in form cannot contain nil or empty i think have been stumped long on one. deep in forest see trees. i'm sure it's relatively simple. should car controller handle within user view? if how accomplish that?
hopefully clear. if need see else, i'd glad post it. help
there 2 things should point out
- your @car object empty, hence says, first argument in form cannot contain nil or empty, because sending @car form_for expects @car initialized, not initialized in show action of users controller.
- partials not have access instance variables in controller. hence if @car variable initialized in show action of users controller, can't use @car in partial, instead need pass render function in show view page. (see: pass variable partial, rails 3?)
basically in controller need have like
def show stuff here @car = car.new end render form should like
<%= render 'shared/carform', :car => @car %> and need create form this
<%= form_for(car) %> or
<%= form_for(@car) %> depending on form gets processed may or may not need add additional arguments form_for function.
Comments
Post a Comment