Twice the same code, but only once it seems to work

Hello,

I am confronted with a very strange problem which I can not solve… and
God I tried a lot, lost almost a whole day, but couldn’t find an answer:
The idea is to have a form both in admin and in dir, in admin there will
be some more fields that is why I have twice a form. In admin the form
appears, in dir the form does not appear instead
“þÿ
New company” why is it that the code is not interpreted and how can I
solve the problem ?

Thank you very much,

The code here below is fairly simple as you can see…

in two controllers (resp. Dir and Admin)

def index
list
render :action => ‘list’
end

def new
@company = Company.new
end

def create
@company = Company.new(params[:company])
if @company.save
flash[:notice] = ‘Company was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

in both views (resp. Dir and Admin)

New company

<%= start_form_tag :action => ‘create’ %>
<%= render :partial => ‘form’ %>
<%= submit_tag “Create” %>
<%= end_form_tag %>

<%= link_to ‘Back’, :action => ‘list’ %>

in both _forms (resp. Dir and admin)

<%= error_messages_for ‘company’ %>

Name <%= text_field 'company', 'name' %>

Adline <%= text_field 'company', 'adline' %>

Adline2 <%= text_field 'company', 'adline2' %>

Postcode <%= text_field 'company', 'postcode' %>

City <%= text_field 'company', 'city' %>

State <%= text_field 'company', 'state' %>

Country <%= text_field 'company', 'country' %>

The model Company :

class Company < ActiveRecord::Base
validates_presence_of :name, :email, :url
validates_uniqueness_of :email, :url
validates_format_of :email,
:with => %r{.@.},
:message => “not a valid email”

def self.find_companies
find(:all, :order => “name”)
end
end

I can’t see your partials
However, partials are relative to the current controller.

<%= render :partial => ‘form’ %> works for admin if the file
_form.rhtml
is located in the /views/admin folder.

Change it to this

<%= render :partial => ‘admin/form’ %>

and let me know if that works for you :slight_smile:

Brian H. wrote:

I can’t see your partials
However, partials are relative to the current controller.

<%= render :partial => ‘form’ %> works for admin if the file
_form.rhtml
is located in the /views/admin folder.

Change it to this

<%= render :partial => ‘admin/form’ %>

and let me know if that works for you :slight_smile:

Thank you very much for your quick reply.
Thank you also for the very handy tip to use a “centralized” form.
I seems that I have to get used to program in a non-repetitive way…

The funny part is that the ‘admin’ routine works like a charm, whereas
the ‘dir’ routine gives me the error, although they both refer to the
same form, use exactly the same definition in their respective
controller and have the same view files as well !

If I look in the source file that is generated, I can still see the ruby
code, so the code is not interpreted…

This is particulary strange, as I for test purposes copied the view file
from admin to dir, and also copy pasted the def. in the controllers…

Hope that makes it clearer…

BTW I want to have separate controller definitions, because in a later
phase I will add fields to the ‘admin’ view that shouldn’t be accessible
by the user…

I thought that the dir_controller couldn’t access the company data, but
for displaying it, like in the ‘list’ routine it can… the mystery isn’t
solved yet, but my code looks nicier already…,

Anyone ?

jabo