I have 3 models: posts, comments and questions. Comments belong to posts and questions belong to comments. I'm trying to link from my posts show page to my questions show page. The posts show page is calling a partial _comments which the link is in. The problem is that the link goes to the questions index instead of the questions show because the question.id is nil. The URL looks like this:
/comments/19/questions/
The routes:
comment_question GET /comments/:comment_id/questions/:id(.:format) questions#show
comment_questions GET /comments/:comment_id/questions(.:format) questions#index
The link in the _comments partial:
<%= div_for(comment) do %>
<% comment.questions.select(:title).order('created_at desc').limit(3).each do |question| %>
<%= link_to question.title, comment_question_path(comment, question) %>
<% end %>
<% end %>
The partial is called in the posts show page with this:
<%= render :partial => #post.comments %>
I changed the link to this:
<%= url_for :controller => 'questions', :action => 'show', :comment_id => comment.id, :id => question.id %>
but got this error:
No route matches {:action=>"show", :controller=>"questions", :id=>nil, :comment_id=>20}
Thanks for the help!
Your question isn't nil, the question object just has no ID attached to it.
<% comment.questions.select(:title).order('created_at desc').limit(3).each do |question| %>
That line will build a query similar to the following (ignoring joins with comments):
SELECT title FROM questions
When you pass that to comment_question_path(comment, question) it just reads the attribute, it doesn't try to fetch it from the database. The ID in that question object will be nil because you haven't queried for it, hence why it's building a link with a nil question ID.
If you really want to use select, use comment.questions.select([:id, :title]) instead.
If you don't care about using select, just use comment.questions instead.
More info on select: http://guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields
Related
Im trying to link_to the show action for a post.
In my controller I have:
#post = Post.all
In my view I'm trying to link it to the last post like this:
<%= link_to #post.last do %>
<%= #post.last.title %>
<% end %>
But it does not take me to the show page for that post?
Post.all loads all posts, but does not guarantee an order. You might want to use the id or the created_at value to order your list of posts:
# in your controller
#posts = Post.order(:id)
# in the view:
<%= link_to #posts.last.title, #posts.last %>
Or - if you don't need the other posts in the view - just load the lastest post:
# in the controller:
#latest_post = Post.order(:id).last
# in the view:
<%= link_to #latest_post.title, #latest_post %>
Try with below code,
<%= link_to post_path(#post.last) do %>
<%= #post.last.title %>
<% end %>
If this code not work then please find route with fire rake routes in your terminal and replace post_path with your routes
Hope this will work.
On my index page, I'm listing all the posts from all the blogs. How would I link_to from a post that has a blog_id to that actual blog.
I could easily in a controller do #blog = Blog.find(#posts.blog_id) if it were just one blog, but since it's not and I already have the blog_id for the post blog, I feel there has to be a way to do something like:
<% #posts.each do |f| %>
<%= f.title %>
#<%= link_to "Blog", go to the Blog using f.blog_id somehow? %>
<% end %>
The answer was given in a comment from "BroiStatse" and I thought I'd give the answer to it since he answered it in the comments section.
Basically had to do this simple line:
link_to 'Blog', blog_path(f.blog_id)
I am new to rails and I am creating a basic blog application.
When I have created a post it has an author. If you locate the author it displays a list of posts that have been created by that author, however when you select the post it is not linking to the post and is instead giving me the following error:
ActiveRecord::RecordNotFound in PagesController#show
Couldn't find Page with ID=2
app/controllers/pages_controller.rb:8:in `show'
{"id"=>"2"}
My code in the Pages Controller is as follows:
def show #Show action
#page = Page.find(params[:id])
It looks as though when I select the link from the author menu it is not locating the correct page ID as it is routing to 2, 3, 4 etc, when the URL of the post is more like 28, 29, 30. If I locate the post in directly from the index menu the link to the post works fine.
Any help will be appreciated.
<% #author.pages.each do |p| %>
<%= link_to p.title, [[p.url]] %>
<% end %>
replace "[[p.url]]" with how you have your urls set up maybe
{:controller => 'pages', :action => 'show', :id => p.id}
Then format for your liking.
I suspect you are passing a different object to the page_path parameter of the link to method on your authors page, below is an example of some erb for displaying a list of pages for an author and linking to them:
<ul>
<% #author.pages.each do |page| %>
<li><%= link_to page.title, page_path(page) %></li>
<% end %>
</ul>
I have the following rails 3 nested models:
resources :books do
resources :authors
end
I now have a view here: /books/131/authors/
And I want to each record to link to something like: /books/131/authors/3333
<% #authors.each do |author| %>
<%= link_to 'author.name', book_author_path(#book, #author) %>
<% end %>
but that error's with: No route matches {:action=>"destroy", :controller=>"authors"}
I also tried:
<%= link_to 'author.name', [#book, author] %>
Problem is the code keeps linking to /authors/3333, not /books/131/authors/3333
Ideas? thanks!
book needs to be defined in the author controller for def index
<%= link_to "title", book_author_path(#book, author) %>
I'm new to rails and thought I had finally figured out some of this routing stuff, but have been going in circles with this bit all day.
I was following a tutorial about building a twitter like service, and I've got the basics working from the tutorial, but with Mongo instead of mySql.
I've got 3 types of pages.
The home page which is showing all the posts ordered by date
The user page which is showing the posts from a specific user
The posts page which is showing posts from a users friends.
So for each page, I've done the following
1) created a method in the corresponding controller to get the correct posts
2) created a _posts.html.erb page with the display parameters, which are slightly different on each page
3) referenced the partial in the index.html.erb page for each view.
The controller entries look like this
def index
#posts = Post.all(:order => 'created_at DESC')
end
or
def posts
#posts = Post.all(:conditions => {'user_id' => params[:id]}, :order => 'created_at DESC')
end
and the partials are
<%= render :partial => #posts %>
In each view is a _posts.html.erb file, and each is slightly different
home/_posts.html.erb looks like this
<%= div_for post do %>
Posted <%= time_ago_in_words(post.created_at) %> ago
Posted By <%= post.user_id %>
<%= post.text %>
<% end %>
while posts/_post.html.erb looks like this
<%= div_for post do %>
Posted By <%= post.user_id %>
<%= post.text %>
<% if post.created_at > 52.hours.since %>
<%= distance_of_time_in_words_to_now(post.created_at) %>
<% else %>
<%= post.created_at.strftime("%c") %>
<% end %>
<% end %>
Now the strange part is that on all the pages index.html.erb, users/show.html.erb, posts/index.html.erb, the partial that is being displayed is the posts/_post.html.erb. The others are being completely ignored.
my understanding was that render :partial would take the #posts and render _posts.html.erb from the current view. But this isn't happening, as only the posts/_post.html.erb is being rendered from all views.
I've looked in the routes.rb file, but don't have anything in there that would cause this problem.
Can anybody tell me why I am not displaying the proper partials?
-----------Edited --------------------------------
The directory structure for views is as follows
views
- home
-_post.html.erb
-index.htlm.erb
- layouts
- posts
-_post.html.erb
-index.html.erb
-posts.html.erb
- sessions
- users
-_post.html.erb
-new.html.erb
-show.html.erb
I hope that helps.
"post", :collection => #posts%>
maybe rails automatically defines path to the partial when you pass only collection
You're passing the collection as the argument that rails is expecting to be the name of the partial. Your call to render should look like this
<%= render partial: "post", collection: #posts %>
This will render app/views/posts/_post.html.erb, passing the local variable post to the partial.
Additionally, (is sometimes handy) there's an iteration object that is made available to this view, partial_name_iteration, that has information about the total size of the #posts collection, and the index of the current object.