Marat_Galiev
Marat_Galiev

Reputation: 1293

Rails form submit problem

I've get a little problem.

My controller:

def new
@company = Company.new
@title = "Create company"
end

def create
@company = Company.new(params[:company])
@company.admin_id = current_user.id
if @company.save
  flash[:success] = "Company created!"
  redirect_to admin_path
else
  @title = "New company"
  render 'new'
end    
end

new.html.erb

<%= debug params[:company] %>

<% form_for @company, :html => { :multipart => true } do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<%= render 'fields', :f => f %>
<div class="actions">
<%= f.submit "Submit company!" %>
</div>
<% end %>

Company.rb model

 validates :name,
:presence => true,
:length => { :maximum => 20 }

 validates_attachment_presence :logo

But after submitting form I've get anyway only one error:

Name can't be blank

Of course I'm filling name and logo fields.

Any ideas? Thanks.

Upvotes: 1

Views: 743

Answers (1)

Mirko
Mirko

Reputation: 5286

You didn't include your _fields partial, and that's probably where your problem is.
Make sure your inputs have appropriate name attributes.

Try to instantiate your model in console and see if validations really work.
You can try something like this: c = Company.new; c.valid?; c.errors and you'll see your errors hash in console.

Upvotes: 1

Related Questions