comment_question goes to comment_questions - ruby-on-rails

I have three models: posts, comments and questions. I have a link from my posts show page that's supposed to go to the questions show view. Instead it goes to the questions index view.
Here are the rake routes:
comment_question GET /comments/:comment_id/questions/:id(.:format) questions#show
comment_questions GET /comments/:comment_id/questions(.:format) questions#index
and here is the posts view:
<%= 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 %>
Thanks for the help.

Try using question instead of #question in your each block:
<%= link_to question.title, comment_question_path(comment, question) %>

If the helpers give you trouble you can always override them with manual implementation
<%= #question.title %>

Related

Redirect to a specific element

I'm learning RoR building social network. So in my views I have a index view which is rendering a mix of all the posts from my groups.
So now, I would like to build a link to redirect toward my original post (into his group). Redirect into the group is not really a problem, but I don't know how to redirect to my post into this group.
With a code example it will be more clear :
Index view :
<%= #posts.each do |p| %>
<%= p.title %>
Publish in <%= link_to p.group.name, group_path(p.group, ROUTE TO MY POST) %>
<% end %>
Show view(group):
<%= #group.posts.each do |post|%>
<div id='post_iter<%=post.id%>'
<%= p.title%>
</div>
<% end %>
So I would like to redirect the user toward his iteration into the group Show.
Something like
<%= link_to p.group.name, group_path(p.group, #post_iter#{p.id}) %>
This should be good :
<%= link_to p.group.name, group_path(p.group, anchor: "post_iter(#{p.id})") %>

link to last post - rails

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.

Linking to acts_as_taggable_on tags within a Post

I've got a very simple setup of posts with associated tags. When I 'show' a post I want to be able to link to each one of those tags BUT it seems to only link to the tag :id that shares the :id of the post I'm showing.
My code:
<% #post.tag_list.each do |tag| %>
<%= link_to tag, tag_path() %>
<% end %>
Let's say I'm looking at post number 2, the above will only link me to /tags/2 , no matter which tag I click on. I'm sure the answer is embarrassingly simple but it's driving me crazy. Thanks so much.
Pass tag to your route helper:
<% #post.tag_list.each do |tag| %>
<%= link_to tag, tag_path(tag) %>
<% end %>
Update:
Change tag_list to tags:
<% #post.tags.each do |tag| %>
<%= link_to tag, tag_path(tag) %>
<% end %>

Ruby on rails cannot display form for comments before displaying comments

I have comments on my post and I can display my comments first and form for writing comments after, but cannot display my form first and comments after. I'm pretty certain that the reason begind that is the .build:
<%= form_for([#question, #question.replies.build]) do |f| %>
So inside this form you just enter the comment body and click submit.
The displayed data is: commenter (user who commented), body of the comment and created_at.
The error I get is:
undefined method `first_name' for nil:NilClass
and the extracted source is:
<%= render #question.replies%>
<% #question.replies.each do |reply| %>
<div class="reply">
<p><%= link_to reply.user.first_name, user_profile_path(reply.user) %> says:</p>
<p><%= reply.body %></p>
<p>Answered <%= time_ago_in_words(reply.created_at) %> ago</p>
<% if current_user==reply.user %>
you should have some sort of #current_user available to you right?
So try doing something like this
<%= form_for([#question, #question.replies.build.tap{|a| a.user = #current_user}]) do |f| %>
The issue is that the comment that you are building with the form_for has a nil user value.
The solution to this is that you have to check if the reply is nil before displaying anything.
<% #question.replies.each do |reply| %>
<% unless reply.user.nil? %>

multiple forms for different instances of the same object class on the same page in Rails

I have a page with many posts, and each post has a list of comments. At the end of the list is a form for a user to add a comment. Only one comment can be submitted at a time.
Can I get away with something like:
form for #comment
...
form for #comment
or do I need to specifically make sure each form is for a separate object? ie
form for #comment1
...
for for #comment2
If it's the latter, how can I make the main page's controller create one comment object for every post on the page?
You need something like this on your view
<% #posts.each do |post| %>
...
<%= form_for post.comments.build do |f| %>
<%= f.hidden_field :post_id %>
...
<% end %>
<% end %>
or, if you use nested resources in you routes
<% #posts.each do |post| %>
...
<%= form_for [post, Comment.new] do |f| %>
...
<% end %>
<% end %>
You can use Nested model form for this purpose.

Resources