print page and Link_to problem

Hi,

I am trying to make a product label maker, so as soon as I enter a Tire into the DB/website I can then print a nice little label from the website. This is all being done from a admin account so I'm not worried about other users.

I added a print_label.html.erb to my tire view. I also added a

  def print_label     @tire = Tire.find(params[:id])   end to my tire controller

So I figured I could add a

<%= link_to 'Print Label', print_label_tire_path(@tire) %>

on the show.html.erb. So that Print label will show only the information from that tire ... brand name stock number so forth

but I'm getting a error of

undefined method `print_label_tire_path' for #<ActionView::Base:0x7c391a8>

So do need to define a path in the routes or map a route...???

Thanks

So do need to define a path in the routes or map a route...???

Yes. You would need to define a "print_label_tire" route.

The other option is to specify the controller and action in the link_to call.

The other option is to specify the controller and action in the link_to call.

How would i do that ....

the controller is named tires_controller.rb and the view is named print_label.html.erb

So what would the link_to look like?

This is a "teach a man to fish" kind of reply...

Tim Shaffer wrote in post #955599:

This is a "teach a man to fish" kind of reply...

What does that mean? Is it derogatory? Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime

I can't begin to tell you how insulted I am and Your little link taught me nothing...

I do have a AAS in software develoment.... I have "Programming Ruby" the pic axe book          "Agile Web Development with rails" Sitting in front of me which niether goes over routes to any depth nor do they or your "Fishing" Website explain very well or at all how to pass a varriable to the page your linking to...

I wrote the following question before I relized that you were mocking me I'll leave it...

Again finding ruby developers to be rather mean maybe it's just this forum

the link didn't help

lets take the basic crud we get when we run a scaffold...

Index Edit Show Delete

from the the index view if we hit the edit button it takes us to the edit view populating it with what ever :id was for that link_to

The index page has a link_to that looks like

<td><%= link_to 'Edit', edit_tire_path(tire) %></td>

now maybe I'm missing it but I've tried to find out were the scaffold put the route or mapping of this edit link_to so that it works.

And thats were I'm confused, I figure I should be able to use the same mechanizm to get from the show page to the print_label page using the current :id as the scaffold built for getting from index to edit.

So that link you gave me doesn't really explain how to make a route or the sytax to use to call print_label and send it the current :id.

if there is a edit route some were could you let me know so I can see how the scaffold maped and or routed the views it created. i can't find them.

your link says

link_to(body, url, html_options = {})

what does body represent?

So would it be " link_to(body, "www.mywebsite/tires/print_label") " So were do I tell it that I want print_labels to grab the tire with the current :id? and populate fields with that object?

I suspect it was intended to be a self-deprecatory apology for merely sending the link. With regard to your problem you might find it useful to look at the Rails Guide on routes.

Colin

I can't begin to tell you how insulted I am and Your little link taught me nothing...

My apologies. I was not intending to be derogatory.

Supplying controller and action to the link_to method is one of the most fundamental things you can do in Rails. Instead of just giving you the answer (after giving you a hint how to find it), I was pointing you to the documentation so you may hopefully learn how to do what you wanted, and also learn how to use the documentation to quickly find answers to simple questions in the future.

ActionView::Helpers::UrlHelper… the link didn't help

lets take the basic crud we get when we run a scaffold...

Index Edit Show Delete

from the the index view if we hit the edit button it takes us to the edit view populating it with what ever :id was for that link_to

The index page has a link_to that looks like

<td><%= link_to 'Edit', edit_tire_path(tire) %></td>

now maybe I'm missing it but I've tried to find out were the scaffold put the route or mapping of this edit link_to so that it works.

And thats were I'm confused, I figure I should be able to use the same mechanizm to get from the show page to the print_label page using the current :id as the scaffold built for getting from index to edit.

When you specify "resources :tire" in routes.rb, it creates a default set of named routes and the helper methods to go along with them. Again, this is basic rails functionality and is explained in-depth in the Routing guide:

So that link you gave me doesn't really explain how to make a route or the sytax to use to call print_label and send it the current :id.

If you scroll down to the third example under the link_to method, it clearly shows how to create a link using a controller, action, and id.

  link_to "Profile", :controller => "profiles", :action => "show", :id => @profile

So in your case, you could use the following syntax:

  link_to "Print Label", :controller => "tires", :action => "print_label", :id => @tire

Although it's probably preferable to add another RESTful action. Then you would be able to use a print_label_tire_path helper like you have been trying to do. This again is explained in the Routing guide.

Best of luck.

Cameron Vessey wrote in post #955645:

The index page has a link_to that looks like

<td><%= link_to 'Edit', edit_tire_path(tire) %></td>

now maybe I'm missing it but I've tried to find out were the scaffold put the route or mapping of this edit link_to so that it works.

Try executing a "rake routes >routes.lst" in your app folder, then take a look at the contents of routes.lst.

There you'll see the all routes (and path aliases) that Rails 'magically' creates from the info in your routes.rb file.

edit_tire new_tire tire

in all the supported flavors of HTTP verbs. Just append a _path, and there's your routing alias.

edit_tire_path(tire) is equivalent to

:controller => 'tires', :action => 'edit', :id => tire.id

unless I haven't had enough coffee this morning.

Tim Shaffer wrote in post #955737:

My apologies. I was not intending to be derogatory.

  I get frustrated after reading and researching routes in 4 different places books and websites and not understanding what they explaining. Then have some say " go read about it"..like I was simpy trying to take a short cut. I'm probable hypersensitive

  I do much better in C#, but get tierd of it's criptic Database managment. But there are so many magic words in ruby that if I put it down for a month I forget the basic syntax....

Supplying controller and action to the link_to method is one of the most fundamental things you can do in Rails. Instead of just giving you the answer (after giving you a hint how to find it), I was pointing you to the documentation so you may hopefully learn how to do what you wanted, and also learn how to use the documentation to quickly find answers to simple questions in the future.

  I new it was a simple question...and I knew what the problem was ...but for the life of me the syntax wasn't coming... and there was some thing fundametal I wasn't getting .. maybe it's take a break after 4 hours or some thing lol

from the the index view if we hit the edit button it takes us to the mechanizm to get from the show page to the print_label page using the current :id as the scaffold built for getting from index to edit.

When you specify "resources :tire" in routes.rb, it creates a default

  ok so thats all you put in routes.rb file? "resource :tire" ?   I can't seem to get my head around what the heck that would do... ruby magic makes my head hurt.... doing this creates the helper method so I can just go _path?

set of named routes and the helper methods to go along with them. Again, this is basic rails functionality and is explained in-depth in the Routing guide:

Rails Routing from the Outside In — Ruby on Rails Guides

So that link you gave me doesn't really explain how to make a route or the sytax to use to call print_label and send it the current :id.

If you scroll down to the third example under the link_to method, it clearly shows how to create a link using a controller, action, and id.

  link_to "Profile", :controller => "profiles", :action => "show", :id => @profile

So in your case, you could use the following syntax:

  link_to "Print Label", :controller => "tires", :action => "print_label", :id => @tire

   I am confused again ... when the scaffold built the link_to .. to the edit page it's syntax does not have the hashes... nor does it have :controller, :action, :id

Although it's probably preferable to add another RESTful action. Then you would be able to use a print_label_tire_path helper like you have been trying to do. This again is explained in the Routing guide.

  Yes, I was trying the restful approach then every thing got into routes   _path is a restful technique? always? To use path were would the restful action go ..in the model? or in the routes.rb?

Best of luck.

I get the the link_to now though...

Ar Chron wrote in post #955762:

Cameron Vessey wrote in post #955645:

The index page has a link_to that looks like

<td><%= link_to 'Edit', edit_tire_path(tire) %></td>

now maybe I'm missing it but I've tried to find out were the scaffold put the route or mapping of this edit link_to so that it works.

Try executing a "rake routes >routes.lst" in your app folder, then take a look at the contents of routes.lst.

  Useful... Thank you ...

There you'll see the all routes (and path aliases) that Rails 'magically' creates from the info in your routes.rb file.

(in C:/Documents and Settings/Administrator/My Documents/NetBeansProjects/all_used_tires)     tires GET /tires(.:format) {:controller=>"tires", :action=>"index"}           POST /tires(.:format) {:controller=>"tires", :action=>"create"} new_tire GET /tires/new(.:format) {:controller=>"tires", :action=>"new"} edit_tire GET /tires/:id/edit(.:format) {:controller=>"tires", :action=>"edit"}      tire GET /tires/:id(.:format) {:controller=>"tires", :action=>"show"}           PUT /tires/:id(.:format) {:controller=>"tires", :action=>"update"}           DELETE /tires/:id(.:format) {:controller=>"tires", :action=>"destroy"}                  /:controller/:action/:id                  /:controller/:action/:id(.:format)

  So heres were I go dumb again... I added a print_label view to the tire view folder ...   "map.resources :tires" does nothing for this new view .... I must force the route into tires?

edit_tire new_tire tire

in all the supported flavors of HTTP verbs. Just append a _path, and there's your routing alias.

edit_tire_path(tire) is equivalent to

:controller => 'tires', :action => 'edit', :id => tire.id

unless I haven't had enough coffee this morning.

I think my mind keeps bouncing back in forth between restful and routes and that won't work lol

Cameron Vessey wrote in post #955866:

  So heres were I go dumb again... I added a print_label view to the tire view folder ...   "map.resources :tires" does nothing for this new view .... I must force the route into tires?

map.resources :tires

generates the 'standard' CRUD routing. There are two additional options I'll mention (probably should have done it earlier)...

:collection and :member

:collection lets you define additional routes that work on a collection of that resource

map.resources :tires, :collection => {:thumbnailed => :get}

would give you

<%= link_to "Thumbnails!", thumbnailed_tires_path %>

targeted at

def thumbnailed   # your method code here end

in the tires controller, which by default expects a

thumbnailed.html.erb view file

:member lets you define additional routes that work on a specific instance of the collection

map.resources :tires, :member => {:print_label => :get}

would give you a

<%= link_to "Print a Label", print_label_tire_path(tire) %>

targeted at

def print_label   # your method code here end

in the tires controller, which by default expects a

print_label.html.erb view file.

You can add as many collection and member routes as you like in routes.rb

This is all probably better explained at http://api.rubyonrails.org/v2.3.8/classes/ActionController/Resources.html around the middle of the page.

Remember to restart your server after changing the routes.rb