Rails - Unable to visit link - ruby-on-rails

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."

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

simple form - Reddit-like site

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 %>

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.

No route matches [GET] "/links/www.twitter.com.au"

Hello building a reddit clone.
When i click on this link found here http://postimg.org/delete/saq41zozs/ it should go another website. In this case it's www.twitter.com.au
Error found here http://postimg.org/delete/uhlsxso1e/
Im guessing because i don't have a twitter.com route. I don't know how to set it up so it routes to twitter.com
Here's my routes.rb.
Rails.application.routes.draw do
resources :comments
devise_for :users
resources :links do
member do
put "like", to: "links#upvote"
put "dislike", to: "links#downvote"
end
resources :comments
end
root "links#index"
Here is my show.html.erb
<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 %>
As you are linking to an external site, as opposed to a relative url, you need to prepend your link with 'http'. Just change the link in question to:
<div class="page-header">
<h1><%= link_to #link.title, "http://#{#link.url}" %><br> <small>Submitted by <%= #link.user.name %></small></h1>
</div>

Resources