mirror of
https://github.com/astuto/astuto.git
synced 2025-12-15 11:17:49 +01:00
Refactor controllers and uncomment an authorization check
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
class PostsController < ApplicationController
|
class PostsController < ApplicationController
|
||||||
# before_action :authenticate_user!
|
before_action :authenticate_user!, only: [:create]
|
||||||
|
|
||||||
def index_by_board_id
|
def index_by_board_id
|
||||||
board_id = params[:board_id] || 1
|
board_id = params[:board_id] || 1
|
||||||
@@ -10,17 +10,18 @@ class PostsController < ApplicationController
|
|||||||
.select('posts.title, posts.description, post_statuses.name as post_status_name, post_statuses.color as post_status_color')
|
.select('posts.title, posts.description, post_statuses.name as post_status_name, post_statuses.color as post_status_color')
|
||||||
.where(filter_params)
|
.where(filter_params)
|
||||||
|
|
||||||
render json: posts
|
render json: posts
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
post = Post.new(post_params)
|
post = Post.new(post_params)
|
||||||
post.user_id = current_user.id
|
|
||||||
|
|
||||||
if post.save
|
if post.save
|
||||||
render json: { status: 'success' }
|
render json: post, status: :no_content
|
||||||
else
|
else
|
||||||
render json: { status: 'error', message: post.errors.full_messages }
|
render json: {
|
||||||
|
error: I18n.t('errors.post.create', message: post.errors.full_messages)
|
||||||
|
}, status: :unprocessable_entity
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -31,7 +32,10 @@ class PostsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def post_params
|
def post_params
|
||||||
params.require(:post).permit(:title, :description, :board_id)
|
params
|
||||||
|
.require(:post)
|
||||||
|
.permit(:title, :description, :board_id)
|
||||||
|
.merge(user_id: current_user.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,17 +7,6 @@ class StaticPagesController < ApplicationController
|
|||||||
|
|
||||||
@posts = Post
|
@posts = Post
|
||||||
.select(:id, :title, :board_id, :post_status_id, :user_id, :created_at)
|
.select(:id, :title, :board_id, :post_status_id, :user_id, :created_at)
|
||||||
.where(post_status_id: get_array_of_ids(@post_statuses))
|
.where(post_status_id: @post_statuses.pluck(:id))
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def get_array_of_ids(resources)
|
|
||||||
array_of_ids = []
|
|
||||||
resources.each do |resource|
|
|
||||||
array_of_ids.push resource.id
|
|
||||||
end
|
|
||||||
|
|
||||||
array_of_ids
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
@@ -102,10 +102,11 @@ class NewPost extends React.Component<Props, State> {
|
|||||||
});
|
});
|
||||||
this.setState({isLoading: false});
|
this.setState({isLoading: false});
|
||||||
|
|
||||||
let data = await res.json();
|
if (res.status === 204) this.setState({success: 'Your post has been published!'});
|
||||||
|
else {
|
||||||
if (data.status === 'success') this.setState({success: 'Your post has been published!'});
|
let data = await res.json();
|
||||||
else this.setState({error: data.message});
|
this.setState({error: data.message});
|
||||||
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.setState({
|
this.setState({
|
||||||
@@ -130,7 +131,6 @@ class NewPost extends React.Component<Props, State> {
|
|||||||
<div className="box sidebar-box newBoardContainer">
|
<div className="box sidebar-box newBoardContainer">
|
||||||
<span className="boardName">{board.name}</span>
|
<span className="boardName">{board.name}</span>
|
||||||
<span className="boardDescription">{board.description}</span>
|
<span className="boardDescription">{board.description}</span>
|
||||||
{/* <span>{this.props.authenticityToken}</span> */}
|
|
||||||
{
|
{
|
||||||
isLoggedIn ?
|
isLoggedIn ?
|
||||||
<button
|
<button
|
||||||
|
|||||||
@@ -1,33 +1,4 @@
|
|||||||
# Files in the config/locales directory are used for internationalization
|
|
||||||
# and are automatically loaded by Rails. If you want to use locales other
|
|
||||||
# than English, add the necessary files in this directory.
|
|
||||||
#
|
|
||||||
# To use the locales, use `I18n.t`:
|
|
||||||
#
|
|
||||||
# I18n.t 'hello'
|
|
||||||
#
|
|
||||||
# In views, this is aliased to just `t`:
|
|
||||||
#
|
|
||||||
# <%= t('hello') %>
|
|
||||||
#
|
|
||||||
# To use a different locale, set it with `I18n.locale`:
|
|
||||||
#
|
|
||||||
# I18n.locale = :es
|
|
||||||
#
|
|
||||||
# This would use the information in config/locales/es.yml.
|
|
||||||
#
|
|
||||||
# The following keys must be escaped otherwise they will not be retrieved by
|
|
||||||
# the default I18n backend:
|
|
||||||
#
|
|
||||||
# true, false, on, off, yes, no
|
|
||||||
#
|
|
||||||
# Instead, surround them with single quotes.
|
|
||||||
#
|
|
||||||
# en:
|
|
||||||
# 'true': 'foo'
|
|
||||||
#
|
|
||||||
# To learn more, please read the Rails Internationalization guide
|
|
||||||
# available at https://guides.rubyonrails.org/i18n.html.
|
|
||||||
|
|
||||||
en:
|
en:
|
||||||
hello: "Hello world"
|
errors:
|
||||||
|
post:
|
||||||
|
create: 'Post create error: %{message}'
|
||||||
Reference in New Issue
Block a user