diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 3faf4e1b..fc73909d 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,5 +1,5 @@ class PostsController < ApplicationController - # before_action :authenticate_user! + before_action :authenticate_user!, only: [:create] def index_by_board_id 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') .where(filter_params) - render json: posts + render json: posts end def create post = Post.new(post_params) - post.user_id = current_user.id if post.save - render json: { status: 'success' } + render json: post, status: :no_content 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 @@ -31,7 +32,10 @@ class PostsController < ApplicationController end 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 diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index 730f3009..f01654a0 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -7,17 +7,6 @@ class StaticPagesController < ApplicationController @posts = Post .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 - - 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 \ No newline at end of file diff --git a/app/javascript/components/Board/NewPost.tsx b/app/javascript/components/Board/NewPost.tsx index 4b721fcb..3d3cb929 100644 --- a/app/javascript/components/Board/NewPost.tsx +++ b/app/javascript/components/Board/NewPost.tsx @@ -102,10 +102,11 @@ class NewPost extends React.Component { }); this.setState({isLoading: false}); - let data = await res.json(); - - if (data.status === 'success') this.setState({success: 'Your post has been published!'}); - else this.setState({error: data.message}); + if (res.status === 204) this.setState({success: 'Your post has been published!'}); + else { + let data = await res.json(); + this.setState({error: data.message}); + } } catch (e) { this.setState({ @@ -130,7 +131,6 @@ class NewPost extends React.Component {
{board.name} {board.description} - {/* {this.props.authenticityToken} */} { isLoggedIn ?