5

I am trying to create a hyperlink in the index page but it doesn't show and it doesn't give any errors either.Here is my index.html.erb code.

<h1> Listing articles </h1>
<% link_to 'New article', new_article_path %>   <---- Everything works perfectly except this doesnt show anything
<table>
<tr>
 <th>Title</th>
 <th>Textssss</th>
<tr>

<% @articles.each do |article| %>
  <tr>
     <td><%= article.title %> </td> 
     <td><%= article.text %> </td> 
   </tr>
 <% end %>

</table>

I have checked my routes and i think they are okay too.

    Prefix Verb   URI Pattern                  Controller#Action
    welcome_index GET    /welcome/index(.:format)     welcome#index
    articles      GET    /articles(.:format)          articles#index
    POST                 /articles(.:format)          articles#create
    new_article  GET      /articles/new(.:format)      articles#new
    edit_article GET     /articles/:id/edit(.:format) articles#edit
    article      GET     /articles/:id(.:format)      articles#show
      PATCH              /articles/:id(.:format)      articles#update
                 PUT     /articles/:id(.:format)      articles#update
          DELETE         /articles/:id(.:format)      articles#destroy
     root GET            /                            welcome#index

i can manually access the article/new from the localhost but i don't think thats the problem. Any help is appreciated..

2
  • 3
    add = to link_to. <%= link_to 'New article', new_article_path %> and read about ERB Commented Dec 7, 2014 at 16:25
  • Thanks dude that cleared up alot of things.. Commented Dec 7, 2014 at 16:33

1 Answer 1

8

The correct syntax to print anchor tag using rails link_to helper is below. You forgot '=' symbol.

<%= link_to 'New article', new_article_path %> 

Ruby code processing within <% %> in ERB template.

%  enables Ruby code processing for lines beginning with %
<% Ruby code -- inline with output %>
<%= Ruby expression -- replace with result %>
<%# comment -- ignored -- useful in testing %>
% a line of Ruby code -- treated as <% line %> 
%% replaced with % if first thing on a line and % processing is used
<%% or %%> -- replace with <% or %> respectively
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.