simple form - Reddit-like site - ruby-on-rails

I completed a search and found this topic that is close, How do I make simple form to use, I'm too new to fully understand
what I was looking at.
The answer was as follows:
Step 1
rails generate simple_form:install --bootstrap (which I did after adding the Gem and running Bundle Install)
Step 2
Told me to add wrappers (not sure if I need to complete this step)
Step 3
modify the simple_form and has this example:
<%= simple_form_for(#contact, url: supplier_contacts_path, html: { class:
'form-horizontal' }) do |f| %>
<div class="form-inputs">
<%= f.input :c_regular,wrapper: :horizontal_input_group do %>
<span class="input-group-addon">$</span>
<%= f.input_field :c_regular, :label => "Regular",class: "form-control" %>
<% end %>
</div>
<% end %>
My code is as follows(views/comments/show.html.erb):
<div id="comments">
<%= render :partial => #link.comments %>
</div>
<%= simple_form_for(#link, html {class: 'form-horizontal' }) do |f| %>
<div class="field">
<%= f.text_area :body, size: "60x12" %>
</div>
<%= f.submit "Add Comment", class: 'btn btn-primary" %>
<% end %>
Here's my partial(views/comments/_comments.html.erb):
<%= div_for(comment) do %>
<div class="comments_wrapper clearfix">
<div class="pull-left">
<p class="lead"><%= comment.body %></p>
<p><small>Submitted <strong><%= time_ago_in_words(comment.created_at) %>
ago</strong> by
<%= comment.user.email %></small></p>
</div>
<div class="btn-group pull-right">
<% if comment.user == current_user %>
<%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you
sure?' }, class: "btn btn-sm btn-default" %>
<% end %>
<% end %>
</div>
<% end %>

Related

Issues with Trix editor in Rails

I'm having this issue with the Trix editor in rails 5.2.4 with webpacker.
The only thing I can see is a little box at the left of the 'Save' button.
My Gemfile has this gem installed:
gem'actiontext',github:'kobaltz/actiontext',branch:'archive',require:'action_text'
And installed the trix editor with:
rails action_text:install
And finally, this is the html code:
<div class="row">
<div class="col-md-3">
<%= link_to 'Back', posts_path, class:'btn btn-secondary mb-4' %>
<div class='card'>
<div class='card-body'>
<h4 class='card-title'>Editing post</h4>
<%= render 'form', post: #post %>
</div>
</div>
</div>
<div class="col-md-9">
<div class="mb-3">
<%= form_with(model: [#post, #paragraph]) do |form| %>
<%= form.hidden_field :element_type, value: 'paragragh' %>
<%= form.submit 'Paragraph', class: "btn btn-primary" %>
<% end %>
</div>
<% #post.elements.each do |element| %>
<% if element.persisted? %>
<%= form_with(model: [#post, element]) do |form| %>
<%= form.rich_text_area :content %>
<%= form.submit 'Save', class: "btn btn-primary" %>
<% end %>
<% end %>
<% end %>
</div>

syntax error, unexpected keyword_ensure, expecting keyword_end in my app

Please i need help validating this code.
Using Ruby 2.3.3 & Rails 5.2
i think all ending anchors are given though
<% if current_user.id != user.id %>
<div class="panel panel-default">
<div class="pane-body">
<center>
<% if !current_user.following?(user) %>
<%= form_for(current_user.active_relationships.build) do |f| %>
<div><%= hidden_field_tag :followed_id, user.id %></div>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>
<% else %>
<%= form_for(current_user.active_relationships.find_by(followed_id: user.id),
html: {method: :delete}) do |f| %>
<%= f.submit "Unfollow", class: "btn btn-default" %>
<% end %>
</center>
</div>
</div>
<% end %>
Here
<% else %>
<%= form_for(current_user.active_relationships.find_by(followed_id: user.id), html: {method: :delete}) do |f| %>
<%= f.submit "Unfollow", class: "btn btn-default" %>
<% end %>
</center>
You have the end to close the block for form_for but not for ending the if-else
<% else %>
<%= form_for(current_user.active_relationships.find_by(followed_id: user.id), html: {method: :delete}) do |f| %>
<%= f.submit "Unfollow", class: "btn btn-default" %>
<% end %>
<% end %>
</center>
Properly indenting helps a lot:
<% if current_user.id != user.id %>
<div class="panel panel-default">
<div class="pane-body">
<center>
<% if !current_user.following?(user) %>
<%= form_for(current_user.active_relationships.build) do |f| %>
<div><%= hidden_field_tag :followed_id, user.id %></div>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>
<% else %>
<%= form_for(current_user.active_relationships.find_by(followed_id: user.id), html: {method: :delete}) do |f| %>
<%= f.submit "Unfollow", class: "btn btn-default" %>
<% end %>
<% end %>
</center>
</div>
</div>
<% end %>
Your end on line 13 closed off the form_for do |f| block, meaning you needed one more to close off the if \ else do block

Comments and Rating section no longer showing up;

I am working on creating a test shoe store website for one of my classes. I had previously added a comment section and rating system for new shoes. For some reason the comment and rating section is no longer viewable.
Here is my product show page
<p id="notice"><%= notice %></p>
<div class="show-buttons" id="show1">
<%= link_to 'Edit', edit_product_path(#product), class: "btn btn- success btn-xs" %> |
<%= link_to 'Back', products_path, class: "btn btn-primary btn-xs" %>
</div>
<div class="pay button">
<%= form_tag "/payments/create" do %>
<%= render partial: "shared/stripe_checkout_button" %>
<%= hidden_field_tag(:product_id, #product.id) %>
</div>
<p>
<strong>Name:</strong>
<%= #product.name %>
</p>
<p>
<strong>Description:</strong>
<%= #product.description %>
</p>
<p>
<strong>Image url:</strong>
<%= #product.image_url %>
</p>
<p>Average Rating:
<div class="rated" data-score="<%= #product.average_rating %>"></div>
</p>
<%= render 'new_comment' %>
<%= render 'comment' %>
<%= will_paginate #comments %>
</div>
<% end %>
Here is my _comment.html.erb
<div class="container">
<div class="row">
<div class="product-reviews">
<% #comments.each do |comment| %>
<div class="row">
<hr>
<p><%= comment.user.first_name %> <small><em><%= "# {time_ago_in_words(comment.created_at)} ago" %></em></small></p>
<% if signed_in? && current_user.admin? %>
<%= link_to ('<span class="glyphicon glyphicon-remove"> </span>').html_safe, product_comment_path(#product, comment), method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
<div class="rated" data-score="<%= comment.rating %>"></div>
<p><%= "Rating: #{comment.rating}/5" %></p>
<p><%= comment.body %></p>
</div>
<% end %>
</div>
</div>
My _new_comment.html.erb
<% if signed_in? %>
<h4>Add a review:</h4>
<%= form_for([#product, #product.comments.build]) do |f| %>
<p>
<%= f.label :body, "Comments" %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.label :rating %><br>
<div class= "rating"></div>
</p>
<%= f.submit "Submit", class: "btn btn-default" %>
</p>
<% end %>
<% end %>
And finally my product.html.erb
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="productbox">
<div class="imgthumb img-responsive">
<%= image_tag(product.image_url, :class => 'list_image') %>
</div>
<div class="caption">
<h3><%= product.name %></h3>
</div>
</div><!--end productbox-->
</div><!--end columns-->
<div class="col-md-8" id="descriptionp">
<p><%= product.description %><br><br>
<%= product.color %><br><br>
€<%= product.price %><br><br>
<%= link_to 'More Info', product %><br><br>
<% if signed_in? && current_user.admin? %>
<%= link_to 'Edit', edit_product_path(product) %> |
<%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
</p>
</div>
</div><!--/row-->
</div><!--/container -->
<hr>
Any assistance on why I can no longer view my comments or rating page would be greatly appreciated.

undefined method `merge' for 35:Fixnum

Found it
Don't use <%= f.hidden_field :field, number %> , use <%= f.hidden_field :field, value: number %>
Issue below
An ActionView::Template::Error occurred in bookings#new:
undefined method `merge' for 35:Fixnum
app/views/bookings/_form.html.erb:31:in `block in _app_views_bookings__form_html_erb__2731573742378725623_70113682151640'
Getting this horrible general error from our production website, and it's not clear why. It's not happening on our local hosts. Here's the line referenced above:
<%= current_employer.locations.first.name_or_address_1 %>
where name_or_address_1 is:
return "from #{name}" if name.present?
"from #{address_1}"
I've gone into the console and run "name_or_address_1" for every location in our database, which works fine, and "locations.first.name_or_address_1" for every employer in our database. Again, works fine. So surely it can't be this line?
Edit: I just commented out the line, deployed to production, and the error still occurs. What's going on? Why is the wrong line being referenced?
Here's the partial:
<%= form_for #employer, url: bookings_path, method: :post, html: { class: "main-form", id: "create-booking" } do |f| -%>
<% #employer.errors.full_messages.each do |msg| %>
<p><%= msg %></p>
<% end %>
<div id="bookings">
<ol class="numbers">
<li>
<legend>Location, Pay, & Vehicle</legend>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Type of job</label><br>
<%= f.select(:job_type_id, options_from_collection_for_select(JobType.all, :id, :name_with_delivery), {}, { id: 'job-type', class: 'form-control' }) %>
</div>
<div class="col-sm-6">
<label>Vehicle needed</label><br>
<%= f.select(:vehicle_id, options_from_collection_for_select(Vehicle.all, :id, :name), {}, { id: 'vehicle-type', class: 'form-control' }) %>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Location</label>
<% if current_employer.locations.size > 1 %>
<%= f.select :location_id, options_from_collection_for_select(current_employer.locations.all, :id, :name_or_address_1), {}, { class: 'form-control' } %>
<% elsif current_employer.locations.size == 1 %>
<p><strong>Location: </strong><%#= current_employer.locations.first.name_or_address_1 %></p>
<%= f.hidden_field :location_id, current_employer.locations.first.id %>
<% end %>
<%= link_to "or add new location", new_employer_location_path(current_employer, Location.new) %>
</div>
<div class="col-sm-6">
<%= f.label :pay %>
<span id="length-message" class="pull-right" style="color: #a94442"></span>
<br>
<%= f.text_field :pay, class: 'form-control', id: 'pay', maxlength: '18' %>
</div>
</div>
</div>
</li>
<legend>Shifts</legend>
<%= render 'booking', booking: Booking.new %>
</ol>
</div>
<%= link_to "Add another shift", "javascript:;", class: 'add-shift', style: 'margin-left: 40px;' %>
<script type="text/javascript">
$(".add-shift").click(function(){
$("ol.numbers").append("<%= j render 'booking', booking: Booking.new %>")
});
</script>
<%= f.submit "Post Shifts", class: 'btn green-button pull-right' %>
<br>
<br>
<br>
<span class="error-message bg-danger pull-right">
</span>
<% end %>
Don't use
<%= f.hidden_field :field, number %>
use
<%= f.hidden_field :field, value: number %>

Rails - Unable to visit link

I tried to follow a tutorial by Mackenziechild.me.
Below is the code in github https://github.com/ikanyu/raddit
Demo app: https://whispering-shelf-9164.herokuapp.com/links/1
Can anyone point out why when I inspect element on the link I have created(for example Facebook link), I can clearly see facebook. However, when I click on the link, it gives me error.
ActiveRecord::RecordNotFound in LinksController#show
Couldn't find Link with 'id'=facebook
def set_link
#link = Link.find(params[:id])
end
def authorized_user
...
Thanks!!
<div class="page-header">
<h1><%= #link.title %><br> <small>Submitted by <%= #link.user.name %></small></h1>
</div>
<div class="btn-group">
<%= link_to 'Visit URL', #link.url, class: "btn btn-primary" %>
</div>
<div class="btn-group pull-right">
<%= link_to like_link_path(#link), method: :put, class: "btn btn-default btn-sm" do %>
<span class="glyphicon glyphicon-chevron-up"></span>
Upvote
<%= #link.get_upvotes.size %>
<% end %>
<%= link_to dislike_link_path(#link), method: :put, class: "btn btn-default btn-sm" do %>
<span class="glyphicon glyphicon-chevron-down">
Downvote
<%= #link.get_downvotes.size %>
<% end %>
</div>
<% if #link.user == current_user -%>
<div class="btn-group">
<%= link_to 'Edit', edit_link_path(#link), class: "btn btn-default" %>
<%= link_to 'Destroy', #link, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-default" %>
</div>
<% end %>
<h3 class="comments_title">
<%= #link.comments.count %> Comments
</h3>
<div id="comments">
<%= render :partial => #link.comments %>
</div>
<%= simple_form_for [#link, Comment.new] do |f| %>
<div class="field">
<%= f.text_area :body, class: "form-control" %>
</div>
<br>
<%= f.submit "Add Comment", class: "btn btn-primary" %>
<% end %>
If you're using the following html to create a link in rails
Facebook.com
Rails will link you to YourApp.com/facebook.com (which is an invalid URL).
Try re-writing your link like this:
Facebook.com
You'll need to change the code in your view to this:
<div class="page-header">
<h1><%= #link.title %><br> <small>Submitted by <%= #link.user.name %></small></h1>
</div>
Alternatively, you can edit your #link.url to include the string "http://www."

Resources