Skip to content

Instantly share code, notes, and snippets.

@eri-b
Last active September 17, 2019 23:17
Show Gist options
  • Save eri-b/32a92f51ddbdd268cd82c910666096a4 to your computer and use it in GitHub Desktop.
Save eri-b/32a92f51ddbdd268cd82c910666096a4 to your computer and use it in GitHub Desktop.
class PostsController < ApplicationController
before_action :editable?, only: [:create]
def create
@site = Site.find_by(name: params[:post][:site])
@post = @site.posts.build(post_params)
if @post.save
redirect_to main_path(@post.site.name)
else
render 'sites/show'
end
end
private
def post_params
params.require(:post).permit(:body)
end
# make sure site is editable before creating post (either public or private + unlocked)
def editable?
@slug = params[:post][:site]
@site = Site.find_by(name: @slug)
if private?
redirect_to main_path(@site.name), notice: 'Site is locked.'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment