I have created an app with simple login authentication, it is actually a twitter clone. The user logs in and access the pages, etc.

But when the user posts something from there profile. It gives an error

NoMethodError in RibbitsController#create
undefined method `userid='

The error is around line 5:

class RibbitsController < ApplicationController

    def create
      @ribbit = Ribbit.create(user_ribbits)
      @ribbit.userid = current_user.id

      if @ribbit.save
          redirect_to current_user 
      else
          flash[:error] = "Problem!"
          redirect_to current_user
      end
    end

    private

    def user_ribbits
      params.require(:ribbit).permit(:content, :userid)
    end
end

The request given to the app:

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"dwVmjDNO4GOowphGFgChMDBxBfvka+M/xSUHvJMECzwxtv4NF6OuWtiaX74NLz91OwQJ9T9+wm7yMiPQ0BLpGA==",
 "ribbit"=>{"content"=>"hi. test.\r\n"},
 "commit"=>"Ribbit!"}

The sessions controller:

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by_username(params[:username])
    if user && user.authenticate(params[:password])
        session[:userid] = user.id
        redirect_to rooturl, notice: "Logged in!"
    else
        flash[:error] = "Wrong Username or Password."
        redirect_to root_url
    end
  end

  def destroy
    session[:userid] = nil
    redirect_to root_url, notice: "Logged out."
  end

end

The users controller:

class UsersController < ApplicationController

    def new
        @user = User.new
    end

    def create
      @user = User.create(user_params)

      if @user.save
        session[:user_id] = @user.id
        redirect_to @user, notice: "Thank you for signing up!"
      else
        render 'new'
      end
    end

    def show
        @user = User.find(params[:id])
        @ribbit = Ribbit.new
    end

    private

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

end

Dani AI

Generated

The error means ActiveRecord has no writer method called userid= for Ribbit. As noted, that happens when the model/table doesn't define that attribute. Two related issues visible in the thread make this more likely: a convention/name mismatch for the foreign key, and inconsistent session handling (which flagged). The safest fixes are to (a) follow Rails conventions for the FK and model associations, and (b) set the owner server-side rather than trusting params.

Concrete checklist and quick fixes:

  • Confirm whether the ribbits table has a user_id column (Rails convention). In the Rails console check column names or existence. For example:

    Rails console
    Ribbit.column_names
    ActiveRecord::Base.connection.column_exists?(:ribbits, :user_id)
  • If user_id is missing, add it with a migration (add an indexed integer user_id), then migrate.

  • Use associations instead of manually assigning an unfamiliar attribute name. Ensure Ribbit belongs_to :user and create a new ribbit through the user association so ActiveRecord sets the FK correctly. Example pattern:

    @ribbit = current_user.ribbits.build(ribbit_params)
    @ribbit.save
  • Do not permit a client-supplied user id in strong parameters. Only permit content, and assign the owner on the server.

  • Define a reliable current_user helper in ApplicationController (load from the single, consistent session[:user_id] key) and keep the session key consistent across login/logout and user creation.

Troubleshooting tips:

  • Use the Rails console to inspect a newly created Ribbit and confirm user_id is set.
  • Check server logs for Strong Parameters filtering to ensure user_id is not being dropped or maliciously provided.
  • After these changes, the original NoMethodError should disappear because ActiveRecord will expose user_id= (or you won't be calling a nonexistent userid= at all).

These steps tie back to ’s diagnosis (missing attribute) and ’s point (load the user from session consistently); applying both will fix the error and avoid future authorization/assignment issues.

Recommended Answers

All 3 Replies

Come on guys, please. Anyone..?

It looks like the Ribbit class does not have a userid= method - presumably because you did not define one and the corresponding table does not have a userid column either.

This is an old post but I just want to answer just in case anyone else has the same issue...

In is application, you expect current_user as a global variable which is somewhat OK. However, you didn't tell Rails that you want the object to be loaded from a session everytime a request is made. In other words, even though a session is created, the variable must be stored somewhere and can be retrieved at anytime. You would need either a module or implement something in your application_controller in order to reload your user object from session (stored the user id). Not sure how you implement this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.