Setup & Authentication

© 2012 - 2017 thoughtbot, inc. Upcase, the design of a robot, and thoughtbot are registered trademarks of thoughtbot, inc.


This is a companion discussion topic for the original entry at https://thoughtbot.com/upcase/videos/intermediate-rails-1

I copied everything but still get an error for the strong params in UsersController. Users#New calls user_params which requires :user, but params[:user] is not present until create. Any help is appreciated. Will the tutorial code be available? Thanks!

ActionController::ParameterMissing in UsersController#new
param is missing or the value is empty: user

I am getting this problem too. Any solutions?

I don’t understand why its happening either, why is my user/new needing params?

I got the same error and I fixed it by over-riding new in the user controller (which had been referencing our over-written user_params method).

not sure why the one in Clearance was not like this, maybe I am limiting some special functionality with this change.

class UsersController < Clearance::UsersController

  def new
    @user = User.new
    render template: "users/new"
  end

  private

  def user_params
    params.require(:user).permit(:username, :email, :password)
  end
end

Actually this is addressed/corrected in the 4th video

Author did a trick, and deleted a key-value pair in one go in Vim.

He had the following code: resources :users, controller: "clearance/users", only: [:create], and he deleted controller: "clearance/users", in one go. I’m interested in how he did that?

I’m getting ActiveModel::ForbiddenAttributesError when I add user_params to the UsersController, with following source:

  def sanitize_for_mass_assignment(attributes)
    if attributes.respond_to?(:permitted?)
      raise ActiveModel::ForbiddenAttributesError if !attributes.permitted?
      attributes.to_h
    else
      attributes

Tried to find the author’s code on Github but nothing…I’ve followed exactly step by step, but stymied now. My Rails is a bit rusty, but this is rather annoying–any chance you can post sample code somewhere as a reference?

Okay, think I figured it out while re-watching. He did it very quickly in the video, missed it the first time: in the routes.rb file, he modified the original line:
resources :users, controller: "clearance/users", only: [:create] do

and removed the controller reference, so it reads like:

resources :users, only: [:create] do

Once I fixed that it worked fine.

But still: can you post example code? Thanks

my finish repo

I think it would be very helpful if you guys used a plugin to show the keys / shortcuts you are using! Otherwise, I have to pause and go search it myself, which isn’t a big deal, but it slows down the process going through the video. I know you guys have videos on vim, which I’m also going through.

Also, I was wondering how you would implement a constraint in the routes on your own outside of a gem used for authentication? I agree that using before_action isn’t as pretty as using a constraint in the routes.

You guys have great articles over here rails - Giant Robots Smashing Into Other Giant Robots. I think linking to articles you guys have created that cover these topics more in depth would be helpful to the user if they want to learn more through the resources you guys have already created.

Can you post your code, or reference it? It’s difficult to help without seeing your code.

Post or reference your code, I may be able to help! :stuck_out_tongue:

Hi @ rodandrew95 , after adding the session controller as below
`class SessionsController < Clearance::SessionsController
private

def authenticate(_)
super(session_params)
end

def session_params
{ session: session_params_with_email }
end

def session_params_with_email
params.require(:session).permit(:password).merge(email: user.email)
end

def user
User.where(email: email_or_username).or(User.where(username: email_or_username)).first || Guest.new
end

def email_or_username
params[:session][email_or_username]
end

end`
my server crashes and i am unable to proceed with sign in. kindly advice

Typing mistake it is:
def email_or_username
params[:session][email_or_username]
end
should be:
def email_or_username
params[:session][:email_or_username]
end

A repo of this would be really helpful, we could review the commits to see the code changes together.

All the keys he presses in VIM are there in the lower right corner. He typed the gesture “delete-to-find-comma” which is “df,” - so he deleted everything up to a next comma including it too.

In order to create a new user you need a “create method” which is misssing
try this:

class UsersController < Clearance::UsersController

def create
    @user = User.new(users_params)
    if @user.save
        redirect_to '/'
    end
end

private

def users_params
    params.require(:user).permit(:username, :email, :password)
end

end

Then you can check that the user has been created.
rails c
irb(main):001:0> User.all

In later versions of clearance (2.6.0), the session params passed to authenticate needs to be an ActionController::Parameters instance, rather than a plain Hash.

In the sessions controller, I needed to refactor the session_params method as follows:

  def session_params
    ActionController::Parameters.new(session: session_params_with_email)
  end
1 Like