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 %>
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.
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.
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.