Topic: Passing arguments

Hi,

I have an application that I'm working on where I have an invoice page that can have multiple line items.

Creating the invoice page is done, but now I want to add the line items.  The work flow goes:

Create the new invoice, and then click on the Add Line Item link.  This is what I have in the edit.rhtml for editing the invoice (where I want the user to click for adding line items):

<%= link_to 'Add Line Item', :action => 'new_item', :id => @invoice %>

I have a view and controller for adding the line item, and saving it to the row to the line item table, but I am having trouble figuring out how to know what the invoice id is.  In the above link the invoice id is appended to the end of the URL for adding the line item, how do I access that in either controller when I'm saving the line item?

Thanks for any assistance.

Damon

Re: Passing arguments

params(:id) should have it when you're inside the receiving controller

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.

Re: Passing arguments

If I understand you right your problem is passing the id of the invoice through *two* actions, new_item and then create_item (or whatever), and the question is how to get the invoice id in the second action.

First you should make the invoice accessible to the view of new_item:

def new_item
  @invoice = Invoice.find(params[:id])
end

Then in new_item.rhtml, you can pass it through to the create_item action:
<%= start_form_tag :action => 'create_item', :id => @invoice %>

where it is accessible as params[:id] again.

Last edited by lucraft (2006-06-19 14:02:31)

Re: Passing arguments

This is what I have in the controller currently:

  def create_lineitem
    @lineitem = Lineitem.new(params[:lineitem])
    if @lineitem.save
      flash[:notice] = 'linetem was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

It gets the details of the line item, but I'm not sure where I put the params(:id) for the id of the invoice.

Thanks!

Damon

Re: Passing arguments

you're trying to find something in params[:lineitem].  This is useful only if you've passed a hash called :lineitem with various lineitem stuff in it:

:lineitem => { :invoice_id => 3, :product_id => 4 }

or something like that.

What you might want:

<%= link_to 'Add Line Item', :action => 'new_item', :lineitem => { :invoice_id => @invoice.id, :product_id => @product.id %>

And then your controller could have:
  def create_lineitem
    # Lineitem.create is sorta like:
    #   Lineitem.new.save
    @lineitem = Lineitem.create(params[:lineitem])
    if @lineitem.new_record?
      flash[:error] = 'problem creating lineitem'
      render :action => 'new'
    else
      flash[:notice] = 'lineitem was successfully created.'
      redirect_to :action => 'list'
    end
  end

Re: Passing arguments

Thanks for the replies so far.  Things are starting to make more sense.

On a related note, is there a way to access a variable that is passed in a link_to, in the html of the page that is being linked to?

For example:

<%= link_to 'Add', :action => 'new_page', :newvar=> @invoice.id %>

This results in a link that points to:  http://127.0.0.1:3000/admin/listing/new_page/123

the 123 is the invoice.id

What I want to do have the access to that variable in the new_page.rhtml:

<html>
<body>
    The value is <% :newvar %>
</body>
</html>

What's the "right" way to do that?

Thanks!

Damon

Re: Passing arguments

I, too, am having problems with this.  I thought I had it somewhat figured out, but now I'm not so sure.  It's a similar situation, except adding a Sample with a pre-existing association to both a Job and Employee.  Here's what I've got so far:

In the jobs controller:

  def new_sample
    @sample = Sample.new
  end
   
  def create_sample
    @sample = Sample.new(params[:sample])
    if @sample.save
      flash[:error] = "Addition Done"
      redirect_to :action => 'list'
    else
      flash[:notice] = "Addition Failed"
      redirect_to :action => 'list'
    end
  end

In my job show view:
<%= link_to 'Add Sample', :action => 'new_sample', :job_id => @job.id, :employee_id => @job.employee.id %>

Yields URL: http://127.0.0.1:3000/jobs/new_sample?j … loyee_id=1

Which would, presumably, pass the :job_id and :employee_id foreign key associations to the newly created sample.  The new_sample view contains the form with the following paramters:

<%= start_form_tag :action => 'create_sample', :id => @job %>
  (All of my fields without the selection drop-downs to manually change associations)
  <%= submit_tag "Add!" %>
<%= end_form_tag %>

As it stands now, the attempt to add an item through this yields my specified addition failed message.  A quick look through the log shows that the foreign keys for job_id and employee_id just aren't being set, which would cause the creation to fail the validations I have set up..  Any help would be appreciated.

(Also, I bought the pdf version of the Agile Web Dev. W/Rails 2nd Ed. book and it's been a great help with some other areas of the program.  I recommend it highly)

-Jacob

Last edited by TheMuffinMan (2006-07-05 14:19:03)

Re: Passing arguments

Figured it out.  Just the relatively minor problem of fixing my redirections now.