Kushal Singh Kushal Singh - 6 months ago 11
Ruby Question

Error in submittion of signup form

When testing for submittion of signup form I keep getting the errors
Failure/Error: @user = User.new(params[:user])
ActiveModel::ForbiddenAttrubutesError:


Error Image


users_controller.rb

class UsersController < ApplicationController

def show
@user = User.find(params[:id])

end

def new
@user = User.new
end

def create
@user = User.new(params[:user])
if @user.save
redirect_to @user
else
render 'new'
end
end

private

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



schema.rb

ActiveRecord::Schema.define(version: 20160510092655) do

create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true

end



user_pages_spec.rb

require 'rails_helper'

RSpec.describe "UserPages", type: :request do

subject {page}

describe "profile page" do
let (:user) { FactoryGirl.create(:user)}
before { visit user_path(user)}

it { should have_selector('h1', text: user.name) }
it { should has_selector?('title', text: user.name) }
end

describe "signup page" do
before {visit signup_path}

it {should has_selector?('h1', text: 'Signup')}
it {should has_selector?('title', text: full_title('Sign Up'))}
end

describe "singup" do
before { visit signup_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)

end
end

describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user@example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end

it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
end
end

Answer

In your controller, try this for the create method:

def create
    @user = User.new(user_params)
    if @user.save
        redirect_to @user
    else
        render 'new'
    end
end