0

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
3
Contributors
3
Replies
17
Views
1 Year
Discussion Span
Last Post by Taywin
0

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.

0

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.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.