Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Instantiate empty model in partial view?

partial view model validation

  • Please log in to reply
5 replies to this topic

#1 JoshMcCullough

JoshMcCullough

    Passenger

  • Members
  • 4 posts

Posted 16 September 2013 - 04:52 PM

What is the proper way to have a form in a partial view reference an empty model in order to handle validation properly as defined on the model. Should I instantiate a new, empty model in the partial view and pass it through to the form? Here is what I'm working with...

 

Model

class NewsletterSignup < ActiveRecord::Base
  def self.columns()
    @columns ||= [];
  end
  
  def self.column(name, sql_type = nil, default = nil, null = false)
    columns << ActiveRecord::ConnectionAdapters::Column.new(name, default, sql_type, null)
  end
  
  def persisted?
    false
  end
  
  column :first_name, :string
  column :last_name, :string
  column :email, :string
  column :zip, :string
  
  validates :first_name, :last_name, :email, :zip, :presence => true
  validates :email, :format => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
  validates :zip, :format => /^\d{5}$/
end

Partial View

<%
	signup = newsletter_signup.new
%>

<%= simple_form_for(signup, ...) do |f| %>
	<%= f.input :first_name, :label => "First Name:" %>	
	<%= f.input :last_name, :label => "Last Name:" %>
	<%= f.input :email, :label => "Email:" %>
	<%= f.input :zip, :label => "Zip:" %>

	...
<% end %>

But I can't seem to instantiate a new model like this. I assume I have to reference it in the view. (Note, I'm new to rails but have a decade+ of professional software development experience, so I apologize if some of rails constructs seem foreign to me and I may just be overlooking something simple!)


  • JoshMcCullough likes this

#2 JoshMcCullough

JoshMcCullough

    Passenger

  • Members
  • 4 posts

Posted 16 September 2013 - 04:57 PM

My mistake, I was using my model's file name instead of the class name, "NewsletterSignup" - man, I wish they matched!



#3 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 16 September 2013 - 08:58 PM

You shouldn't do stuff like this in the view. In the controller method that renders this view you should have stuff like

def new
  @newsletter_signup = NewsletterSignup.new
end 

and then use @newsletter_signup in the view.


  • james likes this

Blog: http://ohm.sh | Twitter: madsohm


#4 JoshMcCullough

JoshMcCullough

    Passenger

  • Members
  • 4 posts

Posted 16 September 2013 - 09:12 PM

My understanding is that partial views are not routed through a controller/action directly. And I always want that form to start out blank. I could pass an empty model through from the original action and then through to the partial, but I don't see that as gaining anything. Did I miss something?

#5 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 16 September 2013 - 09:28 PM   Best Answer

If your controller looks like this

def new
  Model.new
end

def create
  @model = Model.new(params[:model])

  if @model.save
    redirect root_path, notice: "Saved"
  else
    render action: 'new'
  end
end

def edit
  Model.find(params[:id])
end

def update
  if @model.update(params[:model])
    redirect root_path, notice: "Updated"
  else
    render action: 'edit'
  end
end

You can have views like:

# new.html.erb
<%= render 'form' %>

# edit.html.erb
<%= render 'form' %>

# _form.html.erb
<%= form_for @model do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
  ...
<% end %>

and have everything fill out automatically when creating a new one (here everything is blank, unless you tried to save and it failed) or editing an old one.


  • james likes this

Blog: http://ohm.sh | Twitter: madsohm


#6 JoshMcCullough

JoshMcCullough

    Passenger

  • Members
  • 4 posts

Posted 22 September 2013 - 05:29 PM

Makes sense, thanks.







Also tagged with one or more of these keywords: partial view, model, validation

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users