-
-
Save hanachin/b9bceb6f74ac3cb897b0 to your computer and use it in GitHub Desktop.
action resource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # app/models/posts/action.rb | |
| class Action | |
| include ActiveModel::Model | |
| attr_reader :post_id, :name | |
| def save | |
| return false if invalid? | |
| @post = Post.find(post_id) | |
| if name == 'publish' | |
| @post.update state: :publish, published_at: Time.zone.now | |
| elsif name == 'xxx' | |
| # do something | |
| end | |
| # 適当にif分岐にしてるけど、本当はもう少しクラス設計を直して、if文がなくなるようにした方が良い | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # app/controllers/posts/actions_controller.rb | |
| module Posts | |
| class ActionsController < ApplicationController | |
| # POST /posts/:post_id/actions.json | |
| def create | |
| @action = Posts::Action.new(post_id: params[:post_id], name: params[:name]) | |
| respond_to do |format| | |
| if @action.save | |
| format.json { render json: @action, status: :created } | |
| else | |
| format.json { render json: @action.errors, status: :unprocessable_entity } | |
| else | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment