Beginner Question

41 views
Skip to first unread message

Hitesh Sancheti

unread,
Mar 6, 2016, 6:00:16 PM3/6/16
to rubyonra...@googlegroups.com
Hello everyone, I just started off with RoR and had a question.

I wanted to link my 'Show All Posts' to my allposts.html.erb which will
display all the posts that are created but I am having difficulty in
doing so. Do I need to define this in routes.rb file. I just want to
display all posts when that link is clicked.

<ul class="nav navbar-nav">
<li><%= link_to "New Post", new_post_path %></li>
<li><%= link_to "Show All Posts", '#' %></li>
<li><a href="#contact">Contact</a></li>
</ul>

allposts.html.erb contents
<% @posts.each do |post| %>
<h2><%= post.title %></h2>
<p><%= post.body%></p> <br>
<%= link_to "Show Posts", post %>
<% end %>

--
Posted via http://www.ruby-forum.com/.

tamouse pontiki

unread,
Mar 6, 2016, 7:46:06 PM3/6/16
to rubyonra...@googlegroups.com
This is normally Colin's answer, but I suggest you first work a complete tutorial such as Michael Hartl's http://railstutorial.org/book, which is free to read online.

 

Dave Porter

unread,
Mar 6, 2016, 9:51:31 PM3/6/16
to Ruby on Rails: Talk
Would that not normally be just   posts_path

Hitesh Sancheti

unread,
Mar 7, 2016, 10:41:12 AM3/7/16
to rubyonra...@googlegroups.com
http://damp-dusk-43146.herokuapp.com/
From the top nav bar, the 'Show Post' link takes me back to the home
page. But I want it to take me to
https://github.com/hitesh99/ror_blog/blob/master/app/views/posts/allposts.html.erb

Is there anything that needs to be defined in routes?
https://github.com/hitesh99/ror_blog/blob/master/config/routes.rb

Colin Law

unread,
Mar 7, 2016, 11:15:22 AM3/7/16
to Ruby on Rails: Talk
On 7 March 2016 at 15:40, Hitesh Sancheti <li...@ruby-forum.com> wrote:
> http://damp-dusk-43146.herokuapp.com/
> From the top nav bar, the 'Show Post' link takes me back to the home
> page. But I want it to take me to
> https://github.com/hitesh99/ror_blog/blob/master/app/views/posts/allposts.html.erb
>
> Is there anything that needs to be defined in routes?
> https://github.com/hitesh99/ror_blog/blob/master/config/routes.rb

What do you see in the log file when you click the link? Having
studied that if you do not understand it then copy/paste a few lines
of code around the Show Post link so we can see what you are doing,
and also post the relevant section of the log.

If you had something missing from the routes you would get a routing error.

Colin

Hitesh Sancheti

unread,
Mar 7, 2016, 8:04:35 PM3/7/16
to rubyonra...@googlegroups.com
This is what I see when I click on Show Posts link

2016-03-08T01:02:38.242828+00:00 heroku[router]: at=info method=GET
path="/posts" host=damp-dusk-43146.herokuapp.com
request_id=b362a590-741a-4b32-8ba6-ad2c8d916413 fwd="67.244.90.19"
dyno=web.1 connect=0ms service=21ms status=200 bytes=2749
2016-03-08T01:02:38.227479+00:00 app[web.1]: Started GET "/posts" for
67.244.90.19 at 2016-03-08 01:02:38 +0000
2016-03-08T01:02:38.232514+00:00 app[web.1]: Processing by
PostsController#index as HTML
2016-03-08T01:02:38.236506+00:00 app[web.1]: Rendered
posts/index.html.erb within layouts/application (2.1ms)
2016-03-08T01:02:38.240308+00:00 app[web.1]: Rendered
posts/_navigation.html.erb (1.1ms)
2016-03-08T01:02:38.240592+00:00 app[web.1]: Completed 200 OK in 8ms
(Views: 7.1ms | ActiveRecord: 0.0ms)

Colin Law

unread,
Mar 8, 2016, 3:27:56 AM3/8/16
to Ruby on Rails: Talk
Please quote the previous message when replying. This is a mailling
list not a forum, though you may be accessing it via a forum like
interface, so it is important to quote so we know what you are
replying to. See further comments below.

On 8 March 2016 at 01:03, Hitesh Sancheti <li...@ruby-forum.com> wrote:
> This is what I see when I click on Show Posts link
>
> 2016-03-08T01:02:38.242828+00:00 heroku[router]: at=info method=GET
> path="/posts" host=damp-dusk-43146.herokuapp.com
> request_id=b362a590-741a-4b32-8ba6-ad2c8d916413 fwd="67.244.90.19"
> dyno=web.1 connect=0ms service=21ms status=200 bytes=2749
> 2016-03-08T01:02:38.227479+00:00 app[web.1]: Started GET "/posts" for
> 67.244.90.19 at 2016-03-08 01:02:38 +0000
> 2016-03-08T01:02:38.232514+00:00 app[web.1]: Processing by
> PostsController#index as HTML
> 2016-03-08T01:02:38.236506+00:00 app[web.1]: Rendered
> posts/index.html.erb within layouts/application (2.1ms)

That tells us that the server is running GET /posts and is rendering
PostsController#index. That does not sound like the Home Page that
you said it was rendering. Which controller action did you want it to
run?

Unfortunately you have not provided the other information I asked for,
which was to show us a few lines round the Show Posts link in your
code. Please provide that, unless posts#index is the action you want
it to run.

Also have a look at the html that the code is generating (View Page
source in the browser, or something similar depending on which browser
you are using). Copy/paste the relevant section here. But again,
please look at it and see if it helps you to work out what the problem
is. Again if posts#index is the action you want it to run then no
need as that bit is working.

> 2016-03-08T01:02:38.240308+00:00 app[web.1]: Rendered
> posts/_navigation.html.erb (1.1ms)
> 2016-03-08T01:02:38.240592+00:00 app[web.1]: Completed 200 OK in 8ms
> (Views: 7.1ms | ActiveRecord: 0.0ms)

That bit looks a bit odd as it only seems to have rendered what I
assume is your nav bar. Perhaps this is the issue. If posts#index is
the action you expected to be called but it is not rendering the view
you expect then have a look at that code and see if you can see the
issue. Otherwise paste the index action code here.

But don't just post lots of stuff. Try and understand it first and
any bits you don't understand then ask us for help.

Colin
Reply all
Reply to author
Forward
0 new messages