Skip to content

Instantly share code, notes, and snippets.

@mstarkman
Created December 7, 2012 20:29
Show Gist options
  • Save mstarkman/4236267 to your computer and use it in GitHub Desktop.
Save mstarkman/4236267 to your computer and use it in GitHub Desktop.
Nate Help
###### Original code
class RepliesController < ApplicationController
before_filter :signed_in_user, only: [:create, :edit]
#before_filter :correct_user, only: [:edit]
def new
@reply = conversation.replies.build
end
def create
@reply = conversation.replies.build(params[:reply], user_id: current_user.id)
if @reply.save
flash[:success] = "Post created!"
redirect_to root_url
else
redirect_to root_url
end
end
private
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
def correct_user
@reply = current_user.replies.find_by_id(params[:id])
redirect_to root_url if @conversation.nil?
end
end
####### Updated Code
class RepliesController < ApplicationController
before_filter :signed_in_user, only: [:create, :edit]
#before_filter :correct_user, only: [:edit]
def new
@reply = conversation.replies.build
end
def create
@reply = conversation.replies.build(params[:reply], user_id: current_user.id)
if @reply.save
flash[:success] = "Post created!"
redirect_to root_url
else
redirect_to root_url
end
end
private
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
def correct_user
@reply = current_user.replies.find_by_id(params[:id])
redirect_to root_url if @conversation.nil?
end
def conversation
@conversation ||= Conversation.find(params[:conversation_id])
end
end
<!-- Original -->
<% provide(:title, @conversation.subject) %>
<%= render 'conversation_init' %>
<%= form_for(@reply) do |f| %>
<%= render 'shared/reply_form', f: f %>
<%= f.submit "Reply", class: "btn btn-large btn-primary" %>
<% end %>
<!-- Updated -->
<% provide(:title, @conversation.subject) %>
<%= render 'conversation_init' %>
<%= form_for([@conversation, @reply]) do |f| %>
<%= render 'shared/reply_form', f: f %>
<%= f.submit "Reply", class: "btn btn-large btn-primary" %>
<% end %>
@sephethus
Copy link

Oh I forgot to put this code back in before the reply form:

<% if @reply_items.any? %>
<%= render 'reply_item', collection: @reply_items %>
<% end %>

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