Press Button and add to title - ruby-on-rails

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?

Related

How to replace "Update" button by "Save" and "Cancel" buttons in a Ruby on Rails application with "form_with" form helper?

I am using a form in a rails application in order to update a record.
<%= form_with model: #article do |form| %>
<%= form.text_field :title %>
<%= form.text_area :body %>
<%= form.submit %>
<% end %>
But I don't want only one "Update" button, I would like 2 buttons (Save and Cancel). How can I do that ?
For a Cancel button, you could use a link_to to redirect to a different page.
<%= link_to 'Cancel', preferred_redirect_path %>
To change the text for the submit you can use the following.
<%= f.submit "Custom Text" %>

Ruby on Rails 5 - Duplicate "Delete" links

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.

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.

How to pass html for a link_to block?

I'm trying to pass a link_to block with html but can't get it. I tried some other ways with no luck so I will use my original code:
<% link_to survey_path(survey), :class => "button" do %>
<span>add questions to <%= survey.name %></span>
<% end %>
This doesn't show the :class though.
What needs to be corrected?
Try to add = to make it <%= %>
<%= link_to survey_path(survey), :class => "button" do %>
<span>add questions to <%= survey.name %></span>
<% end %>
In the view code in Rails 3 applications it’s sometimes necessary to
use <%= instead of <% at the beginning of blocks that output content,
such as form_for.
Since it's just a span, why don't you just do
<%= link_to "add questions to #{survey.name}", survey_path(survey), :class => "button" %>

Resources