Hartl Rails Tutorial Chapter 9 Exercise 6

Updating, showing and deleting users, exercises

Is there a way to create an RSpec test for user controller actions, for example, “create” and “new”?

I do not quite understand the difference between the two actions: “create” and “new”; can someone please be so kind as to clarify?

After creating the test, how can I implement the redirect_to root_path implementation? I think I should include the “new” and “create” actions in the before_filter signed_in section, but this does not automatically redirect to the root.

I tried to pass the tests by modifying the users_controller.rb file as follows:

def create if signed_in? redirect_to root_path else @user = User.new(params[:user]) if @user.save sign_in @user flash[:success] = "Welcome to the Sample App!" redirect_to @user else render 'new' end end end 
+7
source share
3 answers
  • Yes; and it partially started around 7.16 and elsewhere.
  • Actually the user is created ( create ). One for the page to create a new user ( new ).
  • Not sure if I understand the question.
+1
source

I made a filter before this, it seems to work well, for the test I did this:

at authentication_pages_spec.rb

 describe "signin" do describe "authorization" do describe "for signed in users" do let(:user) { FactoryGirl.create(:user) } let(:new_user) { FactoryGirl.attributes_for(:user) } before { sign_in user } describe "using a 'new' action" do before { get new_user_path } specify { response.should redirect_to(root_path) } end describe "using a 'create' action" do before { post users_path new_user } specify { response.should redirect_to(root_path) } end end end end 

Like @WillJones, some people can add no_capybara: true to the previous block

and on my user controller:

 before_filter :signed_in_user_filter, only: [:new, :create] def signed_in_user_filter redirect_to root_path, notice: "Already logged in" if signed_in? end 

For the difference between new and created actions, this is due to the REST architectural style, but basically new is an action from a user controller that responds to a GET request, and one that is responsible for returning the view it responds to (in this case, a new user form ) create , on the other hand, is an action that responds to a POST request, it does not display anything (it can respond with javascript, but this is an extended topic), and it is responsible for creating new users, for example, the name of the action implies.

+10
source

I also made a filter before this, but my filter is different, and I don’t understand why it works.

My user controller has the following entries

  class UsersController < ApplicationController . . before_filter :logged_in_user, only: [:new, :create] . . def logged_in_user redirect_to(root_path) if !current_user?(@user) end 

It works fine, as in the rkrdo example, and the corresponding tests pass. But does this mean that current_user does not match the user when the user logs in and back? In my opinion, they should be equal in the first case and should not be second.

0
source

All Articles