Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blog app - Add forms #8

Merged
merged 24 commits into from
Jan 25, 2023
Merged

Blog app - Add forms #8

merged 24 commits into from
Jan 25, 2023

Conversation

Baayeh
Copy link
Owner

@Baayeh Baayeh commented Jan 25, 2023

Adding forms to accept user inputs

In this PR, I completed the following tasks:

  • Created a method called current_userin the ApplicationController to make current user data available to all controllers.
  • Created a form to create a post on behalf of current_user.
  • Created a form to create a comment on behalf of current_user.
  • Allowed user to like a post.

Copy link

@t-yanick t-yanick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Baayeh ,

Good job so far!

To Highlight! 👏 🟢

✔️ Good commit messages
✔️ Descriptive Readme file

There are some issues that you still need to work on to go to the next project but you are almost there!

Required Changes ♻️

Check the comments under the review.

Optional suggestions

Every comment with the [OPTIONAL] prefix is not crucial enough to stop the approval of this PR. However, I strongly recommend you to take them into account as they can make your code better.

You can also consider:

  • N/A

Cheers and Happy coding!👏👏👏

Feel free to leave any questions or comments in the PR thread if something is not 100% clear.
Please, remember to tag me in your question so I can receive the notification.

Please, do not open a new Pull Request for re-reviews. You should use the same Pull Request submitted for the first review, either valid or invalid unless it is requested otherwise.


As described in the Code reviews limits policy you have a limited number of reviews per project (check the exact number in your Dashboard). If you think that the code review was not fair, you can request a second opinion using this form.

def create
@post = Post.new(post_params)
@post.author = current_user
if @post.save

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Great job with the project. I think you have to make use of the rails method respond_to so that you define how requests for html are responded to. You should something similar to the code snippet below:
 respond_to do |format|
        if @post.save
          format.html { redirect_to @post }
        else
          format.html { render :new }
        end
      end

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank for the suggestion. I will implement it.

@comment = Comment.new(comment_params)
@comment.author = current_user
@comment.post = @post

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Your comment controller is missing the new action. Kindly add it.
def new
 ...
end

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task requires that we create a form to handle the creation of comments. It did not specifically ask that we create a view for it. So I put the form in the show view of the post controller. It will make it easy for the user to comment on a post easily without having to navigate to another page.

end
end

def comment_params

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The comment_params method is suppose to be a private method

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might have escaped me 😃 . Thanks for the reminder. I will make it private.

@comment = Comment.new(comment_params)
@comment.author = current_user
@comment.post = @post

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Take a look at the create method below and see how to improve your method
def create
    @comment = @current_user.comments.new(params)
    @comment.post_id = params[:post_id]

    if @comment.save
      redirect_to post_path(@comment.post)
    else
      @article = Post.find(params[:post_id])
      redirect_to post_path(@post)
    end
  end

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I will refactor that part.

resources :posts, only: [:index, :show]
resources :users, only: [:index, :show] do
resources :posts, only: [:index, :new, :create, :show] do
resources :comments, only: [:create]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • You should have a route for new comments. Youre probabbly missing it because you did not have the new action in your comments controller. Kindly check and add. When I run rails routes, I cannot find it among the routes just to be very sure.
  • The route should be a GET
    -GET /posts/:post_id/comments/new(.:format) comments#new

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task requires that we create a form to handle the creation of comments. It did not specifically ask that we create a view for it. So I put the form in the show view of the post controller. It will make it easy for the user to comment on a post easily without having to navigate to another page.

<div class="form-submit">
<%= form.submit "Add Comment" %>
</div>
<% end %>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • There should be a form in the views/comments/new.html.erb file which accepts input for creating comments. This is missing in your folder structure. I am sure this is because you did not have the new action in your comments controller.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task requires that we create a form to handle the creation of comments. It did not specifically ask that we create a view for it. So I put the form in the show view of the post controller. It will make it easy for the user to comment on a post easily without having to navigate to another page.

Comment on lines 9 to 14
if @like.save
redirect_to user_post_path(@user, @post), notice: 'You have liked this post'
else
flash[:error] = @like.errors.full_messages
redirect_to user_post_path(@user, @post)
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I think you should make use of the rails respond_to method for the same reason I indicated in my comment in your posts controller.
respond_to do |format|
        if @like.save
          format.html { redirect_to @post }
        else
          format.html { redirect_to @post }
        end
      end

Copy link

@TedLivist TedLivist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

STATUS: APPROVED ✔️ ✔️

Hi @Baayeh,

✔️ Posts, comments and likes can be well created

Your project is complete! There is nothing else to say other than... it's time to merge it :shipit:
Congratulations! 🎉

Cheers and Happy coding!👏👏👏

Feel free to leave any questions or comments in the PR thread if something is not 100% clear. And please, remember to tag me in your question so I can receive the notification.


As described in the Code reviews limits policy you have a limited number of reviews per project (check the exact number in your Dashboard). If you think that the code review was not fair, you can request a second opinion using this form.

@Baayeh Baayeh merged commit 5a171a9 into dev Jan 25, 2023
@Baayeh Baayeh deleted the forms branch January 25, 2023 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants