Rails Console Is Adding Nil Instead of Values

Rails console is adding nil instead of values

Your problem is using attr_accessor in your model. This should only be used where you want to define getter/setter methods for an object which you do not want to store in the database. See here for more information.

Remove the entirety of the line that starts attr_accessor and your code will work.

Also remove the assignment of the id and the line that validates its presence. This is handled automatically by the database.

Why does '= nil' appear in the output when I use this website to code but not when I use Sublime Text?

nil in this case is just the return value of your method call. It didn't return anything. The repl prints it out for you, but when you run a Ruby file, you won't see it.

e.g. in pry, irb, repl.it, etc.

puts 5

prints

5
=> nil

The puts command prints 5, then returns nil. The repl prints that for you, so you know what the return value was. You can try it for yourself

def test
puts 'test'
return 5
end
test

prints

test
=> 5

If you want it to appear when running a file, you can print the return of the function. e.g.

puts "=> #{test.inspect}"

result

test
=> 5
=> nil # this line only if running in repl

Rails - Seeding the database with nil instead of values

brace-types matter...

use the following instead

User.create!( username: "test", password_digest: "test" )

This tells rails to create a single user, using the hash that contains username "test" and password "test"

what you have is telling rails to create a user with an array [] that contains a hash {}... but create is not expecting an array of hashes - it's expecting just a single hash.

EDIT: re: using []

That's because when you don't use () but do use [], then ruby has to guess what you mean...
are you passing an array to a method or are you calling the array-indexing method on a variable? (or hash-indexing if it's not an integer in the brackets)?

If you put the [] right next to the word create! (eg User.create![]) ruby will probably interpret that as the latter... eg it's looking for an variable on the User class called create!... and then tries to look for a key of that hash called {username: "test", password_digest: "test"}, {username: "test2", password_digest: "test2"} and then it gets confused because when you use the array-indexing method [] you should pass it only one key to find, not two ({username: "test", password_digest: "test"} is the first and {username: "test2", password_digest: "test2"} is the second)... thus giving you the error that you're trying ot pass 2 arguments instead of one... but also just using the wrong interpretation to begin with.

neither of which is what you want...

If you leave a space between the [] and create! then ruby is more likely to interpret the [] as a parameter being passed to a method... and then it triggers a different error.

To be unambiguous - always use a space... or use parens () when passing an argument that could be ambiguous in this way.

Rails: When adding column :username to devise, username value still shows as nil in console when I create a new user

Your form submission is being sent to the Devise::RegistrationsController, which - by default, will only permit the following parameters: :email, :password and :password_confirmation. If you view the rails log while submitting this form, you should see an error like:

unpermitted attribute: :username

There are a few possible solutions outlined in the Devise README; the simplest solution in your case being:

class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
end
end

You can read more about strong parameters here in the rails docs.

On a separate note, I would suggest adding the following validation rule to your model:

class User < ApplicationRecord
validates :username, presence: true
end

You could also consider doing something like "if no username is supplied, set it to equal the email address".

Why am I getting a nil value from a Ruby object's member, when it is not nil?

I've solved the problem: chart.ScenarioID is the way I'd do it in C++ and it doesn't work here; it returns nil.

In Ruby, chart[:ScenarioID] works, and correctly returns 160.

allow_nil: false does not work from rails console

The problem is you have to set allow_blank: false instead of allow_nil: false.

In Ruby an empty string is not nil.

"".nil?
#=> false

"".blank?
#=> true

Update your model like this

class Idea < ActiveRecord::Base
mount_uploader :picture, PictureUploader
belongs_to :project
validates :name, presence: true, allow_blank: false
end

If you want know the differences between nil and blank,see this SO post.

Refer these Guides for allow_blank

nil value instead of form value

try this in your ratings controller create action:

@rating = Rating.new(stars: params[:rating][:star], comments: params[:rating][:comments], ratable_id: params[:beer_recipe_id])

if @rating.save
puts "huzzah!"
end

Let me know if that works.

Rails: how do i change values in table from nil to string inside the rails console?

In your case:

@user = User.username = ant

You're saying, @user is equal to User.username, what at the same time is equal to ant, but what's ant? Rails will try to work with it as a local variable in your current scenario, that's why the message:

NameError: undefined local variable or method `ant' for main:Object

You need to use update, specify each attribute as a "key" and the corresponding new value as the key value, like:

user = User.find(1)
user.update(username: 'new username', first_name: 'new first_name', last_name: 'new last_name')


Related Topics



Leave a reply



Submit