Skip to content

Instantly share code, notes, and snippets.

@jparrish62
Created August 2, 2016 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jparrish62/fb8ff3811e8584bd078e4996e2eb0d1a to your computer and use it in GitHub Desktop.
Save jparrish62/fb8ff3811e8584bd078e4996e2eb0d1a to your computer and use it in GitHub Desktop.
First the follow will not render I have replaced the code with text and it works so I know rails is finding the file just not rendering the form second If you look at the _follow_form where I'm calling @post.user_id I' getting a error undefined method user_id for nil nil class
<%= form_for(current_user.active_relationships.build, remote: true) do |f| %>
<div><%= hidden_field_tag :followed_id, user.id %></div>
<%= f.submit 'Follow', class: 'btn btn-primary'%>
<% end %>
<div id="follow_form">
<% if current_user.following?(@post.user_id) %>
<%= render 'follow' %>
<% else %>
<%= render 'unfollow' %>
<% end %>
</div>
<div class="container">
<% @posts.each do |post| %>
<div class="col-sm-6 col-md-4">
<div class="thumbnail thumb-box">
<%= link_to image_tag post.image.url(:medium) %>
<h5 style = "text-align:center;"><%= link_to post.user_name, post_path(post) %> | <%= post.title %></h5>
<% if current_user %>
<div class="btn btn-default" >
<%= link_to image_tag("glyphicons-402-skateboard.png"),like_post_path(post), method: :put, data: { remote: true } %>Like
<span id="post_<%= post.id %>"><%= post.get_upvotes.size %></span>
</div>
<%= link_to "Edit", edit_post_path(post) %> | <%= link_to "Delete", "#"%>
<%= render 'follow_form'if current_user != nil %>
<% end %>
</div>
</div>
<% end %>
</div>
class PostsController < ApplicationController
def new
@post = current_user.posts.build
end
def show
@post = Post.find(params[:id])
@post_comment = @post.comments.build
@post_comments = @post.comments
end
def index
@posts = Post.order("created_at DESC")
end
def edit
@post = Post.find(params[:id])
end
def upvote
@post = Post.find(params[:id])
@post.upvote_by current_user
respond_to do |format|
format.js { render 'vote' }
end
end
def create
@post = current_user.posts.build(post_params)
@post.user.id = current_user.id
if @post.save
redirect_to posts_path
else
render 'new'
end
end
def post_params
params.require(:post).permit(:user_name, :title, :body, :image, :video )
end
end
@AlexanderBelonogov
Copy link

You do not have @post in the index action, you have @posts. So you should pass post in this line as locals like <%= render 'follow_form', post: post %> and here change to <% if current_user.following?(post.user_id) %>. For show and edit actions you should also pass variable to form when render like <%= render 'follow_form', post: @post %>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment