-
-
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) | |
| # name の内容で処理を分岐したり、コールバックを書いたり。 | |
| 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