No route matches [GET] "/links/www.twitter.com.au" - ruby-on-rails

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>

Related

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

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

Ajax form submition not working

I'm attempting to create a "Save Changes" button for a form that would send data via ajax to the update method in the controller. The aim is to allow form users to save their work without the form reloading or redirecting. However I'm running into a bit of a problem; I'm getting the following error
undefined method `update_incorporation_path'
To be clear, incorporation is the controller that we're working with. Below is the code I added to accomplish this.
To my view, I added:
<%= button_to "", update_incorporation_path(#incorporation), :remote => true, :method => :post %>
To my routes, I added:
resources :incorporations do
member do
post 'update'
end
end
The update method looks like this:
def update
if #incorporation.update(incorporation_params)
if admin_signed_in?
#incorporations = Incorporation.all.order("created_at DESC")
else
#incorporations = current_user.incorporations("created_at DESC")
end
render action: "index"
else
render 'edit'
end
end
The complete view is below:
edit.html.erb
<%= render 'form' %>
<br/>
<%= link_to "Back", root_path, class: "btn btn-default" %>
_form.html.erb (the buttons are at the bottom)
<div id="wrapper" class="active main-content">
<%= simple_form_for #incorporation do |f| %>
<!-- Sidebar -->
<!-- Sidebar -->
<div id="sidebar-wrapper">
<ul id="sidebar_menu" class="sidebar-nav">
<li class="sidebar-brand"><a id="menu-toggle" href="#">Menu<span id="main_icon" class="glyphicon glyphicon-align-justify"></span></a></li>
</ul>
<% #sections=[["basic_info", "Basic Info"],["address", "Address"],["equity", "Equity"],["officers","Officers"],["directors", "Directors"],["contractor","Contractors"],["ip","IP"],["shareholders", "Shareholders"]] %>
<ul class="sidebar-nav" id="sidebar">
<% #sections.each do |section| %>
<li><span class="sub_icon glyphicon glyphicon-link"></span><%= section[1] %></li>
<% end %>
</ul>
<div id="save">Save</div>
</div>
<div class="panel-body">
<div id="basic_info" class="form_section">
<div class="form-left"><h2>Basic Info</h2></div>
<div class="form-right">
<%= f.simple_fields_for :company do |company| %>
<div class="padded-fields">
<%= render 'basic_fields', company:company %>
</div>
<% end =%>
<div class="padded-fields">
<div class="form_subsection">
<%= f.input :trademark_search, as: :radio_buttons, label: 'Would you like us to do a trademark search and provide advice regarding any issues we identify in relation to the name you have selected?', input_html: { class: 'form-control' } %>
</div>
</div>
</div>
</div>
<%= f.simple_fields_for :company do |company| %>
<div id="address" class="form_section">
<%= render 'address_fields' , company:company %>
</div>
<div id="equity" class="form_section">
<%= render 'equity_fields' , company:company %>
</div>
<div id="officers" class="form_section">
<div class="form-left"><h2>Officers</h2><br/><p>Please list the officers of the company.</p></div>
<div class="form-right">
<div>
<%= company.simple_fields_for :officers do |officer|%>
<%= render 'officer_fields', f: officer %>
<% end =%>
<%= link_to_add_association 'Add Officer', company, :officers, class: "btn btn-default add-button" %>
</div>
</div>
</div>
<div id="directors" class="form_section">
<div class="form-left"><h2>Directors</h2><br/><p>Please list the initial directors of the company. We recommend an odd number to avoid a deadlocked board.</p></div>
<div class="form-right">
<div>
<%= company.simple_fields_for :people do |person|%>
<%= render 'person_fields', f: person %>
<% end =%>
<%= link_to_add_association 'Add Director', company, :people, class: "btn btn-default add-button" %>
</div>
</div>
</div>
<div id="contractor" class="form_section">
<div class="form-left"><h2>Employees Contractors</h2></br><p>Please list all employees, independent contractors and any other individual or entity who will be providing services to the company at the time of incorporation. Each of these persons should have written agreements with the company. Please check the box next to each name for whom you would like us to prepare agreements</p></div>
<div class="form-right">
<div>
<%= company.simple_fields_for :contractor_people do |contractor| %>
<%= render 'contractor_person_fields', f:contractor %>
<% end =%>
<%= link_to_add_association 'Add Person', company, :contractor_people, class: "btn btn-default add-button" %>
</div>
<div class="form_subsection">
<div>
<%= company.simple_fields_for :contractor_orgs do |contractor| %>
<%= render 'contractor_org_fields', f:contractor %>
<% end =%>
<%= link_to_add_association 'Add Company', company, :contractor_orgs, class: "btn btn-default add-button" %>
</div>
</div>
</div>
</div>
<div id="ip" class="form_section">
<div class="form-left">
<h2>Intellectual Property</h2><br/><p>Please list existing intellectual property (including business plans, software, artwork, inventions, trade secrets and the like) that has been created for use in the company and the name of the person or people who created it.</p>
</div>
<div class="form-right">
<div>
<%= company.simple_fields_for :ips do |ip| %>
<%= render 'ip_fields', f: ip %>
<% end =%>
<div class="add-field"><%= link_to_add_association 'Add IP', company, :ips, class: "btn btn-default add-button" %></div>
</div>
</div>
</div>
<div id="shareholders" class="form_section">
<div class="form-left"><h2>Shareholders</h2><br/><p>Please list all individuals to hold equity in this company.</p></div>
<div class="form-right">
<div>
<%= company.simple_fields_for :shareholders do |shareholder|%>
<%= render 'shareholder_fields', f: shareholder %>
<% end =%>
<%= link_to_add_association 'Add Shareholder', company, :shareholders, class: "btn btn-default add-button" %>
</div>
</div>
</div>
<% end =%>
</div>
<%= f.button :submit, id:"incorporation_submit", class: "btn btn-primary" %>
<%= button_to "Update", incorporation_path(#incorporation), method: :post, remote: true %>
<% end =%>
</div>
I figure I must be forgetting something. Any thoughts are much appreciated.
Routes for update method was by default added when you wrote resources :incorporations, so change your routes to
resources :incorporations
And your path should be incorporation_path, also method in button_to is by default post, you don't need to write it,
change your button_to to
<%= button_to "Update", incorporation_path(#incorporation), :remote => true %>
But, if you are submitting a form, it should have a submit button instead of button_to, your form should look like this
<%= form_for #incorporation, remote: true do |f| %>
# form content
<%= f.submit "Submit" %>
<% end %>
Hope this helps!
Instead of :
<%= button_to "", update_incorporation_path(#incorporation),
:remote => true, :method => :post %>
Try :
<%= button_to "Update", incorporation_path(#incorporation),
method: :post, remote: true %>
In your route:
resources :incorporations
The resources ships with default actions index,new, create,edit, update, destroy. You don't need to declare it manually.
You can verify the routes from your console.
rake routes | grep 'incorporations'
You will get output like :
From here you can construct your path for the update action.
Hope it helps :)

How can I add a comment list in my posts' index.html page?

How can I add a comment list in my posts' index.html page?
It is my PostsController:
def index
#posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
#comments = #post.comments.all
#comment = #post.comments.build
end
and its my posts show view:
<p id="notice"><%= notice %></p>
<p>
<h3><%= #post.name %></h3>
</p>
<p>
<%= (#post.descriptopm).html_safe %>
</p>
<%= link_to 'Edit', edit_post_path(#post), :class => "btn btn-info btn-xs" %>
<%= link_to 'Back', posts_path, :class => "btn btn-info btn-xs" %>
<h3>Comments</h3>
<% #comments.each do |comment| %>
<div>
<strong><%= comment.user_name %></strong>
<br />
<p><%= (comment.body).html_safe %></p>
</div>
<% end %>
<%= render 'comments/form' %>
and its my posts index view:
<h1>Listing posts</h1>
<%= link_to 'Create a New Post', new_post_path, :class => "btn btn-success btn-sm" %>
<% #posts.each do |post| %>
<div class="post thumbnail">
<h3><%= post.name %></h3>
<div><%= (post.descriptopm).html_safe %></div>
<div class="bottom-bottoms">
<%= link_to 'Display', post, :class => "btn btn-info btn-xs" %>
<%= link_to 'Edit', edit_post_path(post), :class => "btn btn-info btn-xs" %>
<%= link_to 'Delete', post, method: :delete, data: { confirm: 'Are you sure?' }, :class => "btn btn-info btn-xs" %>
</div>
<h3>Comments</h3>
<% #post.comments.each do |comment| %>
<div>
<strong><%= comment.user_name %></strong>
<br />
<p><%= (comment.body).html_safe %></p>
</div>
<% end %>
<%= render 'comments/form' %>
</div>
<% end %>
the post.rb :
class Post < ActiveRecord::Base
has_many :comments
end
the comment.rb :
class Comment < ActiveRecord::Base
belongs_to :post
end
the show page show the comments well but the index cannot...
it shows the error : undefined method `comments' for #
please help me >"<
I am a newly Ruby on Rails writer
Associations
If you're using ActiveRecord association to "link" your Post and Comment model, you'll be able to call .comments on each post you have in your index
Something to note (and this lies at the core of your error) is that you will have to call the .comments method on each instance of the Post. You're currently trying to call it on an #instance variable which doesn't exist:
#app/views/posts/index.html.erb
<% #posts.each do |post| %>
<% post.comments.each do |comment| %>
<%= comment.body %>
<% end %>
<% end %>
This, of course, is only possible by using the setup you have already (has_many / belongs_to):
#app/models/post.rb
Class Post < ActiveRecord::Base
has_many :comments #-> post.comments
end
#app/models/comment.rb
Class Comment < ActiveRecord::Base
belongs_to :post #-> comment.post
end
instead of using #post in comment use post on index page. You have typo error.
<%= link_to 'Create a New Post', new_post_path, :class => "btn btn-success btn-sm" %>
<% #posts.each do |post| %>
<div class="post thumbnail">
<h3><%= post.name %></h3>
<div><%= (post.descriptopm).html_safe %></div>
<div class="bottom-bottoms">
<%= link_to 'Display', post, :class => "btn btn-info btn-xs" %>
<%= link_to 'Edit', edit_post_path(post), :class => "btn btn-info btn-xs" %>
<%= link_to 'Delete', post, method: :delete, data: { confirm: 'Are you sure?' }, :class => "btn btn-info btn-xs" %>
</div>
<h3>Comments</h3>
<% post.comments.each do |comment| %>
<div>
<strong><%= comment.user_name %></strong>
<br />
<p><%= (comment.body).html_safe %></p>
</div>
<% end %>
<%= render 'comments/form' %>
</div>
<% end %>

Resources