Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on rails link_to syntax

After following a tutorial Ive found. Im now redoing it again, without the scaffolding part, to learn it better.

However, editing my \app\views\home\index.html.erb to contain:

<h1>Rails test project</h1>
<%= link_to "my blog", posts_path>

I get an error:

undefined local variable or method `posts_path' for #<ActionView::Base:0x4e1d954>

Before I did this, I ran rake db:create, defined a migration class and ran rake db:migrate, everything without a problem.

So the database should contain a posts table. But that link_to command cant seem to find posts_path. That variable (or is it even a function?) is probably defined through the scaffold routine.

My question now is; how do I do that manually myself, define posts_path?

like image 938
Mizipzor Avatar asked Feb 13 '09 13:02

Mizipzor


People also ask

How does Link_to work in Rails?

Rails is able to map any object that you pass into link_to by its model. It actually has nothing to do with the view that you use it in. It knows that an instance of the Profile class should map to the /profiles/:id path when generating a URL.

What is Link_to?

method: symbol of HTTP verb - This modifier will dynamically create an HTML form and immediately submit the form for processing using the HTTP verb specified. Useful for having links perform a POST operation in dangerous actions like deleting a record (which search bots can follow while spidering your site).

How do I link pages in Ruby on Rails?

open index. html. erb file in app>>views>>home folder use in atom editor. After you open index file now add following HTML link code in that file and end with <br /> tag.


3 Answers

You will need to define a path to your posts in config/routes.rb

Rails 2.x syntax:

map.resources :posts

Rails 3.x syntax:

resources :posts
like image 54
DanSingerman Avatar answered Oct 21 '22 01:10

DanSingerman


The _path methods are dynamically generated typically. The method missing error comes about when there isn't a route to the object specified or in this case the method you're calling explicitly.

Defining a route should fix this. HermanD above showed one way to do this.

You can run 'rake routes' from the root of your rails app to see all the routes that are configured

like image 35
Kevin Davis Avatar answered Oct 20 '22 23:10

Kevin Davis


<%= link_to "my blog", posts_path>

If this is exactly what your erb contained, it's missing the percent sign at the end of the scriptlet element. Not sure if that caused your problem, or maybe I'm taking things too literally....

like image 41
Tom Avatar answered Oct 20 '22 23:10

Tom