Topic: 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
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
"

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

Hi Jabo,

The code you posted here looks fine but I'm not sure I completely understand the problem.

If I understand your post, you have 2 controllers, Dir and Admin and you want to use the same form in views of both controllers. The render :partial call shows the form in /app/view/admin/new.rhtml but not in /app/view/dir/new.rhtml. Is that right?

A few questions: Are any errors showing up in development.log? Have you made doubly sure you have _form.rhtml in both /app/view/admin and /app/view/dir?

Would it be easier to move the _form.rhtml to /app/view/partials and call render :partial => "partials/form" instead? That way there is only 1 form file which helps you make sure the form file itself works. If you need to render certain fields only in the admin controller you can always pass a parameter to the render method like this for example:

# In your dir view
render :partial => "partials/form", :params => { :context = "dir" }

# In your admin view
render :partial => "partials/form", :params => { :context = "admin" }

# In /app/view/partials/_form.rhtml
<p><label for="company_name">Name</label>
<%= text_field 'company', 'name'  %></p>

... etc ...

<% if( params[:context] == "admin" ) -%>
  <p><label for="company_admin_only">Admin field</label>
  <%= text_field 'company', 'admin_only'  %></p>
<% end -%>


This way "Admin field" will only be displayed when the render is called with "admin" as its :context parameter.

I don't know if any of this was helpful. If not you could post some more complete code so we have a better view of the problem wink

-- Marsvin

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

Dear Mr. Marsvin,

Thank you very much for your quick reply.
Thank you also for the very handy tip to use a "centralized" form with a context parameter. (couldn't get it to work with :params => { :context = "admin" }, but I used a class variable context in the controller def)
I seems that I have to get used to program in a non-repetitive way..

To give you some more info :
In admin, I would like to have access to more fields as you noticed.

The funny part is that the 'admin' routine works like a charm (the one with the more fields), whereas the 'dir' routine gives me the error.
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...

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 thanks to Marsvin,

Anyone ?

jabo

Last edited by jabo (2006-11-29 04:35:46)

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

You see ruby code in the output? That's strange. Did you place any ruby files in the public directory?

It may be that it doesn't like the name "dir" for a controller. Might want to try renaming it.

Railscasts - Free Ruby on Rails Screencasts

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

I did rename 'dir' but it does not solve the problem
I deleted the layout file, removed the create definition in the dir controller, no effect.
At the end, made a lot of combinations and finally discovered that the problem shoud be in the new definition, but then this one contains nothing more than :

   def new
     @company = Company.new
   end

Strange... why doesn't it work whereas

def show
    @company = Company.find(params[:id])
  end

does not seem to be affected by the problem...

Anyone ??

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

Reusable code is what Rails is all about.. I'm still getting used to it too I have to admit wink

Back to the problem.. It all sounds very strange to me.. The view works when you use Company.find but not when you use Company.new? Honestly that sounds almost impossible. Unless maybe the view tries to access certain elements in @company which are still nil (.new only creates the object, it doesn't make sure all its attributes have values.)

I don't know it all sounds very confusing. Would it be possible to copy and paste the full error message to this forum? That might give us some more clues, especially if there is raw ruby code being output.

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

There is no actual error message, the page renders its HTML and in Safari when I ask the source code I get this :
--------------------- From here
<h1>New company</h1>

<%= start_form_tag :action => 'create' %>
  <%=render :partial => "partials/form"  %>
  <%= submit_tag "Create" %>
<%= end_form_tag %>

<%= link_to 'Back', :action => 'list' %>
----------------------To Here

When I use the layout file, the HTML source code shows :
----------------------From Here
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
                        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <title>Welcome to the public pages</title>
  <link href="/stylesheets/scaffold.css?1164785980" media="all" rel="Stylesheet" type="text/css" />
</head>
<body>
<div class="rightMenu">
    <p><b>
        Directory
    </b></p>
    <p>
        <a href="/repert/new">Add your company</a>
    </p>
    <p>
        <a href="/repert">Directory</a>
    </p>
    <p><b>
        Admin Area
    </b></p>
    <p>
        <a href="/admin">Administration</a>
    </p>
</div>
<div class="mainText">