parent
e57c97bf3b
commit
3c1e821fab
@ -1,2 +1,26 @@
|
|||||||
class ConfessionsController < ApplicationController
|
class ConfessionsController < ApplicationController
|
||||||
|
# skip_before_action :verify_authenticity_token
|
||||||
|
|
||||||
|
def create
|
||||||
|
transgression = Transgression.find(params[:transgression_id])
|
||||||
|
|
||||||
|
@confession = transgression.confessions.new(confession_params)
|
||||||
|
|
||||||
|
if @confession.save
|
||||||
|
|
||||||
|
else
|
||||||
|
render json: {
|
||||||
|
error: {
|
||||||
|
message: @confession.errors.full_messages.to_sentence
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def confession_params
|
||||||
|
return params.require(:confession)
|
||||||
|
.permit(:description, :occurred_at)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,52 +1,29 @@
|
|||||||
class TransgressionsController < ApplicationController
|
class TransgressionsController < ApplicationController
|
||||||
before_action :require_current_user
|
before_action :require_current_user
|
||||||
|
# skip_before_action :verify_authenticity_token, only: :create
|
||||||
|
|
||||||
def new
|
def index
|
||||||
@transgression = Transgression.new
|
@transgressions = current_user.transgressions.includes(:confessions)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@transgression = current_user.transgressions
|
@transgression = current_user.transgressions.new(transgression_params)
|
||||||
.new(transgression_params)
|
|
||||||
|
|
||||||
@confession = @transgression.confessions.new(confession_params)
|
if @transgression.save
|
||||||
|
|
||||||
if @transgression.save && @confession.save
|
|
||||||
redirect_to transgressions_path
|
|
||||||
else
|
else
|
||||||
flash[:message] = @transgression.errors.full_messages.to_sentence
|
render json: {
|
||||||
render :new
|
error: {
|
||||||
|
message: @transgression.errors.full_messages.to_sentence
|
||||||
|
}
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def edit
|
|
||||||
end
|
|
||||||
|
|
||||||
def update
|
|
||||||
# params[:id]
|
|
||||||
end
|
|
||||||
|
|
||||||
def show
|
|
||||||
end
|
|
||||||
|
|
||||||
def index
|
|
||||||
@transgressions = current_user.transgressions
|
|
||||||
.includes(:confessions)
|
|
||||||
.group_by { |x| x.sin_type }
|
|
||||||
end
|
|
||||||
|
|
||||||
def delete
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def transgression_params
|
def transgression_params
|
||||||
return params.require(:transgression)
|
return params.require(:transgression)
|
||||||
.permit(:sin_type, :description)
|
.permit(:sin_type, :description)
|
||||||
end
|
end
|
||||||
|
|
||||||
def confession_params
|
|
||||||
return params.require(:confession)
|
|
||||||
.permit(:description, :occurred_at)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@ -0,0 +1,7 @@
|
|||||||
|
json.transgression_id @confession.transgression_id
|
||||||
|
|
||||||
|
json.confession do
|
||||||
|
json.id @confession.id
|
||||||
|
json.description @confession.description
|
||||||
|
json.occurred_at time_ago_in_words(@confession.created_at) + " ago"
|
||||||
|
end
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
if current_user
|
||||||
|
json.current_user do
|
||||||
|
json.email current_user.email
|
||||||
|
end
|
||||||
|
else
|
||||||
|
json.current_user nil
|
||||||
|
end
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
json.transgression do
|
||||||
|
json.id @transgression.id
|
||||||
|
json.sin_type @transgression.sin_type
|
||||||
|
json.description @transgression.description
|
||||||
|
json.authored_at time_ago_in_words(@transgression.created_at) + " ago"
|
||||||
|
json.confessions []
|
||||||
|
end
|
||||||
@ -1,22 +0,0 @@
|
|||||||
|
|
||||||
<% @transgressions.each do |sin_type, trans| %>
|
|
||||||
<h4>
|
|
||||||
For the sin of <em><%= sin_type %></em>, your weaknesses are:
|
|
||||||
</h4>
|
|
||||||
<ul>
|
|
||||||
<% trans.each do |t| %>
|
|
||||||
<li>
|
|
||||||
<%= t.description %>, with <%= t.confessions.count %> confessions:
|
|
||||||
<% if t.confessions.count > 0 %>
|
|
||||||
<ul>
|
|
||||||
<% t.confessions.each do |c| %>
|
|
||||||
<li>
|
|
||||||
"<%= c.description %>" - <%= c.occurred_at %>
|
|
||||||
</li>
|
|
||||||
<% end %>
|
|
||||||
</ul>
|
|
||||||
<% end %>
|
|
||||||
</li>
|
|
||||||
<% end %>
|
|
||||||
</ul>
|
|
||||||
<% end %>
|
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
json.sinner current_user.email
|
||||||
|
|
||||||
|
json.transgressions(@transgressions) do |trans|
|
||||||
|
|
||||||
|
json.id trans.id
|
||||||
|
json.sin_type trans.sin_type
|
||||||
|
json.description trans.description
|
||||||
|
json.authored_at time_ago_in_words(trans.created_at) + " ago"
|
||||||
|
|
||||||
|
json.confessions(trans.confessions) do |conf|
|
||||||
|
json.id conf.id
|
||||||
|
json.description conf.description
|
||||||
|
json.occurred_at time_ago_in_words(conf.created_at) + " ago"
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -1,21 +0,0 @@
|
|||||||
<form action="<%= transgressions_path %>" method="POST"
|
|
||||||
class='transgression'>
|
|
||||||
<input type="hidden" name="authenticity_token"
|
|
||||||
value="<%= form_authenticity_token %>">
|
|
||||||
<input type="text" name="transgression[description]"
|
|
||||||
placeholder="Weaknesses are beautiful..."
|
|
||||||
value="<%= @transgression.description %>"><br/>
|
|
||||||
<select name="transgression[sin_type]">
|
|
||||||
<% Transgression::SIN_TYPES.each do |sin| %>
|
|
||||||
<option value="<%= sin %>"
|
|
||||||
<%= @transgression.sin_type == sin ? "selected" : "" %>>
|
|
||||||
<%= sin %>
|
|
||||||
</option>
|
|
||||||
<% end %>
|
|
||||||
</select>
|
|
||||||
<hr/>
|
|
||||||
<h5>Your first confession!</h5>
|
|
||||||
<input type="text" name="confession[description]"><br/>
|
|
||||||
<input type="date" name="confession[occurred_at]"><br/>
|
|
||||||
<input type="submit" value="Here I am">
|
|
||||||
</form>
|
|
||||||
Loading…
Reference in new issue