Ruby on Rails 5 - Duplicate "Delete" links - ruby-on-rails

This is the picture of my problem ---> Duplicate Delete Links Picture
I have a comment with two delete links.
This is the order of how I'm rendering the comment form and the comments from partials:
<h2>Comments</h2>
<%= render 'comments/form' %>
<%= render #recipe.comments %>
This is the code for _form.html.erb
<%= form_for([#recipe, #recipe.comments.build]) do |f| %>
<p><%= f.label 'Comment' %> <br> <%= f.text_area :comment %></p>
<p><%= f.submit 'Post' %></p>
<% end %>
This is the code for _comment.html.erb
<div class="show-text-formatting">
<p><strong><%= comment.name %></strong></p>
<p><%= comment.comment %></p>
<%= link_to 'Delete', [comment.recipe, comment], method: :delete,
data: { confirm: 'Are you sure want to delete this comment?' } %>
</div>
The first delete link have the extension: /recipes/10/comments/1 (1 is the comment id #)
Also, when I hover over the second delete link has the extension: /recipes/10/comment
The difference between is that the second delete link's extension doesn't have a comment id
I know that by switching the order of
<h2>Comments</h2>
<%= render 'comments/form' %>
<%= render #recipe.comments %>
to
<h2>Comments</h2>
<%= render #recipe.comments %>
<%= render 'comments/form' %>
would solve my problem, but I want the form to come before the comments.
EDIT:
The second delete link isn't actually a "second" delete link. It's a delete link for a null object. I still don't know how to get rid of it though.

Try this. Set an order to your comments. Like this:
<h2>Comments</h2>
<%= render 'comments/form' %>
<%= render #recipe.comments.order(created_at: :DESC) %>
That should fix your problem. I've had the same issue and it worked for me.

If you know for sure it's nil object, why don't you add a trigger. I mean we can check the object whether it's nil or not before rendering.

Related

show action not working in nested show page

I have a weird issue that I can't seem to figure out nor know how to google.
Let me explain what the error is before going into the code stuff.
I have an album, which has many photos (photos belong to album).
Album works fine. Photo works fine.
When I try to click on show link of photo ... that's where things seem to go sideways, ie, none of it works after the first one.
Any ideas? Please let me know if further information is needed.
The problem seems to be coming from my Photos controller of show action, ie this:
# app/controllers/photos_controller.rb
def show
#album = Album.find(params[:album_id])
#photo = #album.photos.find(params[:id])
end
The error that is being generated is this:
ActiveRecord::RecordNotFound in PhotosController#show
Couldn't find Album with 'id'=4
The weird part is that the first photo of ablum works. It's all other subsequent photos that don't work.
This this the app/views/photos/show.html.erb:
<h1>Album Details</h1>
<b><p>Title</p></b>
<p><%= #album.title %></p>
<b><p>Descriptions</p></b>
<p><%= #album.description %></p>
<%= link_to "All Albums", albums_path %>
<h3>Photos</h3>
<% #album.photos.each do |photo| %>
<p>
<b>Title</b>
<%= photo.title %>
<%= link_to 'Show', album_photo_path(photo) %>
</p>
<% end %>
<h3>Add More Photos</h3>
<%= form_for([#album, #album.photos.build]) do |f| %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
It seems to me that you link_to for showing the photo is missing the id for the album.
Instead of
<%= link_to 'Show', album_photo_path(photo) %>
Should it be
<%= link_to 'Show', album_photo_path(#album, photo) %>
Since it looks like :album_id is expected as a separate parameter from the photos :id.

Partial path error because of nil

I am working on a project and I just introduced polymorphism for posting text Posts and photo Posts.
When I try to post an image I get this error: "'nil' is not an ActiveModel-compatible object that returns a valid partial path."
The first show file (app/views/dashboards/show.html.erb) with code:
<%= form_for #text_post do |form| %>
<%= form.text_field :body, placeholder: 'Post content here' %>
<%= form.submit 'Post Title!' %>
<%end%>
<%= form_for #photo_post do |form| %>
<%= form.file_field :image %>
<%= form.submit 'Post Image!' %>
<%end%>
<%= render #posts %>
which leads to this at app/views/photo_posts/_photo_post.html.erb with the following code:
<%= image_tag photo_post.image.url(:post) %>
The app/views/posts/_post.html.erb which causes problem is:
<%= div_for post do %>
<%= link_to post.user.username, post.user %>
posted
<%= render post.content %>
<%= link_to time_ago_in_words(post.created_at), post %>
<% end %>
Sorry if the details I provide is insufficient, I am a new ROR developer. If you want, you can fork or check the project from here, (I pushed so you can check for the error).
Any help/guidance is greatly appreciated!
Please try
<%= render post.content if post.content %>
instead of
<%= render post.content %>
as per the error logs, this should fix the issue.

Make the post's comments toggleable in Rails using Jquery

Scenarioļ¼šA post has many comments. In the index page of posts, when user clicks the show link( link_to post), its comments will show below the post.
Here I use the append() to add the comments:
$('#edit_post_<%= #post.id %>').append('<%= j render "comments/comments" %>')So when user click the show link, comments would be load and show up.
But how can I hide these comments again (i.e to make comments toggeable) ?
posts/index
<h1>Posts</h1>
<% #posts.each do |post| %>
<%= form_for post, remote: true do |f| %>
<%= post.content %>
<div id="post_<%= post.id %>">
<%= link_to "show", post, remote: true %>
</div>
<% end %>
<% end %>
posts/show.js.erb
$('#edit_post_<%= #post.id %>').append('<%= j render "comments/comments" %>')
comments/_comments.html.erb
<% #comments.each do |comment| %>
<%= comment.content %>
<% end %>
I suppose you dont have the event bindings for new comments.
you should use something like
$('.container-of-your-comments').on('click', '.the-toggle-button', function (e) {
// toggle
});
so toggling works for all (also future) elements.
EDIT:
given the changes in the question: if toggle means just hiding the whole comment list,
you have to do it manually.
i suggest you have separate show / hide buttons (you can show and hide the buttons upon changing state as well), because if not, it gets more complex.
so for example, in posts/index:
<h1>Posts</h1>
<% #posts.each do |post| %>
<%= form_for post, remote: true do |f| %>
<%= post.content %>
<div id="post_<%= post.id %>">
<%= link_to "show", post, remote: true %>
</div>
<div class="post-comments" id="post_comments_<%= post.id %>">
</div>
<% end %>
<% end %>
in comments/_comments.html.erb
<% #comments.each do |comment| %>
<%= comment.content %>
<% end %>
<%= link_to "hide", '#', class: "hide-comments" %>
and in posts/show.js.erb
$('#post_comments<%= #post.id %>').html('<%= j render "comments/comments" %>')
and in assets/javascripts/posts.js (be sure you include this in your application.js)
$(document).ready(function() {
$('.hide-comments').click(function(e) {
e.preventDefault();
$(this).parent().hide();
});
});
backbone.js is a great tool, if you'll be doing a lot of things with those comments, like CRUD etc. and you need to stay on one page - use backbone.js. if not, stick to jQuery.

Press Button and add to title

I have a post and it has a title. I want the user to be able to press a button and then after the title the word completed is placed in but I'm not sure how I would do this.
my post view looks like
<% div_for post do %>
<strong><%= link_to_unless_current h(post.title), post %></strong> - <%= link_to post.user.name, post.user %>
<%= simple_format h(post.body) %>
<% end %>
I feel like I may need an if statement saying if clicked original code for the view plus "completed" else original code but I'm not sure how I would use this logic in a button and how to describe this logic. I'm still a noob so I'm sorry if this question is overly simple.
What I want to do is have a button labeled completed that adds the work complete onto the end of the title.
this is my show view
<%= render :partial => #post %>
<% if current_user?(#post.user) %>
<p>
<%= link_to 'Edit', edit_post_path(#post)%>
<%= link_to 'Delete', #post, :method => :delete, :confirm => "Are you sure?" %>
</p>
<% else %>
<% end %>
<h2>Comments</h2>
<div id="comments">
<%= render :partial => #post.comments %>
</div>
<%= form_for [#post, Comment.new] do |f| %>
<p>
<%= f.label :body, "New Comment" %><br />
<%= f.text_area :body %>
</p>
<p><%= f.submit "Add Comment" %></p>
<% end %>
the view in shown in my main question is the view for each individual post so basically I want to add completed in front of <%= link_to_unless_current h(post.title), post %> that block of code when I press the button and then it should display the word completed and the original title
Im not getting your idea if what exactly shoul happen when clicking on the title, but if u want to change stuff on the fly in our html site, u need JS or ajax to handle this...
Could u please send more example code or specify what should happen or where the title should be placed?

(rails) hidden form elements not giving values to controller

I have the following form:
<% form_for(:tag, :url => {:action => "post_tag", :id => #photoID}) do |form| %>
<%= error_messages_for(:tag) %>
<% if #errors then %>
<%= #errors[0] %>
<% end %>
<p><%= form.select(:user_id, #userHash) %></p>
<p><%= form.hidden_field(:xpos) %></p>
<p><%= form.hidden_field(:ypos) %></p>
<p><%= form.hidden_field(:width) %></p>
<p><%= form.hidden_field(:height) %></p>
<%= submit_tag "Submit Tag" %>
<% end %>
But none of the values are filled in the controller. I know the values are all full in the view because I can see they have the correct values in Firebug. In the controller, I am trying to access them like params[:xpos] for the :xpos hidden_field. Is this correct???
Do logger.debug params.inspect. I have a sneaking suspicion you will see params[:tag][:xpos] there :)
You have passed a name to form tag there (form_for(:tag, ...) do |form|), it will wrap all fields constructed as form.field(...) in a hash identified by the passed name ("tag", in this case).

Resources