Question on passing arguments inside a view.

I'm running into an issue undefined local variable or method `directoryid' for #<EditorialsController:0x23f1bf8>

I have two Models on a legacy database and only one controller called editorials with two actions index and display. I'm trying to pass in a parameter from the results of my search and getting the above error.

Example: two tables one is editorial the other fp_directory

Editorial Model def self.date_range(from,to)     find(:all, :conditions => {:expiration_date => from..to },          :order => 'expiration_date DESC') end

Directory Model def self.full_path(directoryid)     find(:all, :conditions => { :j15c_directory_id => "#{directory_id}" }) end

I have a view that displays my form and submits output to display controller all works until I add in one more piece.

Editorial Controller def display     @get_path = Directory.full_path(directory_id) end

Now my results are working fine until I add in this one piece to get the directory_id

<% for x in @editorials do %> <%= x.directory_id %>   <% end %>

If I want to pass in the x.directory_id into <%= @get_path(x.directory_id)

I get the undefined local variable or method `directoryid' for #<EditorialsController:0x23f1bf8>

I'm realizing its something to do with my scope but I'm failing to find it.

thanks appreciate any pointers.

sc

seems like you have some uninitialized vars.

where is @editorials set?

where does directory_id in @get_path = Directory.full_path(directory_id) come from?

@editorials is set in my EditorialsController actually line -- @editorials = Editorial.date_range(start_date, end_date) @editorials gets the start_date, and end_date from the passed in params during form submit. It then display's my view which I have a for x in @editorials do loop which pulls in x.file_name - x.directory_id - x.record_id etc. So x.directory_id lives in my legacy editorial table. It's then passed into the other command which hits my directory table. I'm assuming that is possible in the from the same view correct?

So The x.directory_id is what I tried passing to the @get_path(x.directory_id)

If I hardwire instead of trying to pass in x.directory_id I will return the path correctly. But I need it for each item returned from @editorials above loop.

I'm very green at Rails/Ruby.

Thanks..

Sc-

It looks like the code you're running and the code you've show us as different: the error message mentions directoryid but not of the source you've show references such a variable. it's going to be stabbing in the dark until you show your actual code (and by the looks of it the error (at least the first one it's hitting) is in the controller.

Fred

So @editorials is an array of Editorial (not of EditorialController). So x is should be an Editorial too, but it seems it's an EditorialControler. The error shows you try to call the method of the controller, not of an instance of Editorial.

But Frederick is right, you should show some more of the code, maybe that would help.

Sorry about the post I had an older version and a new version open in Textmate and copied and pasted incorrect. This is my latest setup which I started over from scratch using the standard restful index and show for my views. I narrowed down my issue but not sure how to solve just yet. So even with this instance @get_path out of my view still get this error below. At least I'm understanding the error more now.

Error -- NameError (undefined local variable or method `directory_id' for #<EditorialsController:0x2160fd8>):

Two Models directory def self.full_path(directory_id)     find(:all, :conditions => { :j15c_directory_id => "#{directory_id}" }) end

editorial Model   def self.date_range(from,to)     find(:all, :conditions => {:expiration_date => from..to },          :order => 'expiration_date DESC', :readonly => true)   end

One Controller editorials_controller.rb

def index     @time_today = Time.now.ctime()

    respond_to do |format|       format.html # index.html.erb       format.xml { render :xml => @editorial }     end end

def show     start_date = "#{params[:editorial][:"from(1i)"]}- #{params[:editorial][:"from(2i)"]}-#{params[:editorial] [:"from(3i)"] }"     end_date = "#{params[:editorial][:"to(1i)"]}-#{params[:editorial] [:"to(2i)"]}-#{params[:editorial][:"to(3i)"] }"    @editorials = Editorial.date_range(start_date, end_date)

    @get_path = Directory.full_path(directory_id)

    respond_to do |format|       format.html # show.html.erb       format.xml { render :xml => @editorial }     end   end end

My Views

Index.html.erb

<h2>Input Date Range to form a Search</h2>   <p>     Todays Date and Time: <%= @time_today %>   </p>   <p>     Please Enter a Start date and End date range to search for expiration dates.   </p>   <table>   <% form_for :editorial, :url => {:action => 'show' } do |form| %>   <tr>   <td>Start Date: <%= form.date_select :from %></td>   <td>End Date: <%= form.date_select :to %></td>   <td>Submit : <%= submit_tag "Search" %></td>   </tr>   <br />   <% end %>   </table>

Show.html.erb <div id="expiration-list"> <p>Your search returned a Total of <%= @editorials.size %> file(s)</p>

<table> <% for x in @editorials do %> <tr valign="top" class="<%= cycle('list-line-odd', 'list-line-even') %>">   <td><%= "File #{x.file_name} has an expiration date of #{x.expiration_date} Directory ID: #{x.directory_id}" %>

******* This is the place I wanted to pass x.directory_id to @get_path(x.directory_id) But it seems even without this I get an error as soon as I hit submit from my index.html.erb page. My thoughts were to just loop over the @get_path array and just grab one field.

</tr> <% end %> <br>

</table> </div>

Thanks again and apologize for the sloppy posting...

Scott,

What is the relationship between your models? I’m guessing an editorial belongs to a directory? Also I ran into some trouble like this when using a legacy system because the “id” field was come weird name like “CustomerContactSomethingStupidID” and so I had to set my model to specify that field as the ID. http://pastie.org/221842 is an example of what i did for a set up in the model.

I hope this helps,

-Chris

Correct Editorial belongs_to :j15t_directory which is the table name I have my editorial.rb model with belongs_to :j15t_directory and my directory.rb model set to has_one :editorial no Foreign Keys.. Just made me think maybe I should be trying to get my find working with a Table include and do one retrieval? Going to look at your site.

I believe I solved this or at least I understand the cause. My index page submits my form_data to my show view which calls the controller and queries for expired files @editorials Array. My view then extracts this data and I assumed I could then pass in info gathered

I did a test and placed my Directory.find_by_directory_id(x.directory_id).full_path directly in my view and things work fine. So that being said this is not clean at all to me and to put this in the view does make sense. I think I'm going to read up on Active Record and create a smarter query when that pulls from both legacy tables. If there is other directions that could be recommended let me know.

Thanks for the responses/

You haven't defined directory_id anywhere, therefore you get an error. The error message will even tell you it happened on this line. You'll get going a lot quicker if you learn to read the error messages rails/ ruby generates.

Fred

Indeed I was aware but I was not understanding the why. I figured that out which if I was paying closer attention should have been very obvious. Oh well. I’m hitting the books to learn more about the routing, and calling of controllers. Need to take a few steps back before I can move forward. Thanks again though hopefully in the very short future I can answer questions.

Thanks All