app/controllers/stories_controller.rb
class StoriesController < ApplicationController
before_action :set_story, only: %i[ show edit update destroy ]
before_action :set_location, only: %i[ show new ]
# GET /stories or /stories.json
def index
@stories = Story.all
end
# GET /stories/1 or /stories/1.json
def show
@comments = @story.comments.order(created_at: :desc)
@comment = @story.comments.build
end
# GET /stories/new
def new
@story = Story.new
end
# GET /stories/1/edit
def edit
end
# POST /stories or /stories.json
def create
@story = Story.new(story_params)
respond_to do |format|
if @story.save
format.html { redirect_to location_story_path(@story.location, @story), notice: t(".created") }
format.json { render :show, status: :created }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @story.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /stories/1 or /stories/1.json
def update
update_record(@story, story_params, story_url(@story))
end
# DELETE /stories/1 or /stories/1.json
def destroy
@story.destroy!
respond_to do |format|
format.html { redirect_to stories_url, notice: t(".destroyed") }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_story
@story = Story.find(params[:id])
end
def set_location
@location = Location.find_by(id: params[:location_id])
end
# Only allow a list of trusted parameters through.
def story_params
params.require(:story).permit(:address, :body)
end
end