Reference user in Rails Fixtures - ruby

Is there any way to reference the ID of a user fixture in another fixture. I have a messages.yml fixture:
basic:
body: "Hey Barney, wanna go bowling tonight?"
sender: fred
recipient_ids: [users(:barney)]
type: Message
users.yml
barney:
first_name: Barney
last_name: Rubble

Yes, use ERB syntax:
recipient_id: <%= User.find_by_first_name('Barney').id %>
http://guides.rubyonrails.org/v5.0/testing.html#the-low-down-on-fixtures

This is a big reason why fixtures are not commonly used anymore. I'd recommend using a seeds file. Possibly with seed_fu. This will allow you to create fixtures/seeds in Ruby and do just about anything.

Related

Ruby: Error in minitest to destroy user

I am a newbie to ruby and am just creating my first test suite.
When writing a minitest to destroy a user I get the following error:
ERROR["test_should_destroy_when_logged_in_as_a_admin", UsersControllerTest]
test_should_destroy_when_logged_in_as_a_admin#UsersControllerTest ActionController::UrlGenerationError:
No route matches {:action=>"/users/608331937", :controller=>"users"}
The test reads the following:
def setup
#user_destroy = users(:destroyme)
#user_admin = users(:admin)
end
test "should destroy when logged in as a admin" do
log_in_as(#user_admin)
assert #user_admin.admin?, "not admin"
assert_difference 'User.count', -1 do
delete user_path(#user_destroy)
end
end
and fixture:
admin:
name: Matthias Havenaar
email: my#mail.com
password_digest: <%= User.digest('password') %>
admin: true
destroyme:
name: Destroy Me
email: destroy#me.com
password_digest: <%= User.digest('password') %>
admin: true
It seems like something goes wrong with the user ID or user_path. Any idea what I am doing wrong here?
Try this, I hope this will work.
Replace
delete user_path(#user_destroy)
With
delete :destroy, id: #user_destroy

Ruby class instance fields hidden

This is a very simple question, but I cannot figure out what is happening here. Here's my simple model:
class User
attr_accessor :first_name, :last_name
end
I can write the first_name and last_name fields for a new User, and when I call that user through the variable I assigned it to, I get this:
> u
=> #<User>
It doesn't show the fields I wrote. But when I do this (for last & first name):
> u.first_name
=> "John"
Why isn't the full object showing up when I call u? Such as => #<User first_name: 'John' last_name: 'Smith'>. What do I have to add to my model to get that output? Thanks in advance.
The method that is used for converting an object to a String is to_s, the method that is used for human-readable debugging output is inspect. Some environments also use other methods, e.g. pry (or some plugin) uses pretty_inspect for "pretty" human-readable debugging output. You need to implement those methods.
Example:
class User
def inspect
"#<#{self.class} first_name: '#{first_name}', last_name: '#{last_name}'>"
end
end
What ruby are you using? For me it works:
class Testtt
attr_accessor :a
end
test = Testtt.new
test.a = 1
> test
=> #<Testtt:0x007fdbca785b60 #a=1>

Sinatra App Not Using Erb in Links

The problem I'm having is that Sinatra apps don't always seem to show my ERB when it is used in some specific context of hyperlinks. I have no idea how much is relevant to show for this question, so I'll do the best I can .Basically, I have a file in my Sinatra app called book_show.erb. It has the following line:
<p>Edit Book</p>
However, when that link is rendered in the browser, it links like this:
http://localhost:9292/books/#{#book.id}/edit
The #{#book.id} was not replaced with the actual ID value. There is a #book object and I do use it in other contexts in that very same file. For example:
<h1><%= #book.series %></h2>
<h2><%= #book.title %></h2>
<h3>Timeframe:</h3>
<p><%= #book.timeframe %></p>
I don't even know if my routes would make a difference here for diagnosing this but all of the relevant routes for my books functionality are:
get '/books' do
#title = 'Book Database'
#books = Book.all
erb :books
end
get '/books/new' do
#book = Book.new
erb :book_add
end
get '/books/:id' do
#book = Book.get(params[:id])
erb :book_show
end
post '/books' do
book = Book.create(params[:book])
redirect to("/books/#{book.id}")
end
I don't know what else to show to help diagnose this problem. I'm hoping someone sees something terribly obvious that I'm missing.
Adding a book to the database works just fine; it's only that edit link that I can't get to work correctly -- and that would seem to be pure HTML/ERB. In order to test it, I also added this line to the page:
<p>Testing: <%= "/books/#{#book.id}/edit" %>
That came back and returned this text:
Testing: /books/4/edit
So I know the ID is getting stored. It has to be something to do with the hyperlink but I can find nothing useful on Sinatra that helps at all with this.
ERB template does not behave like a ruby string - you need to explicitly tell it you exit the 'template' part into the 'logic' part. It looks very odd when it comes to attributes:
<p>Edit Book</p>
You could use link_to helpers to make it look better:
link_to('Edit Book', controller: 'books', action: 'edit', id: #book.id)

undefined method update_attributes in Rails 4

i'm now using Rails 4
now i want to update my user record
i'm trying to use this command
#user=User.update_attributes(:name=> params[:name], :user=> params[:username], :pass=> params[:password])
OR
#user=User.update_attributes(name: params[:name], user: params[:username], pass: params[:password])
but always got the error
undefined method `update_attributes'
so how i can update my user
also i want to ask is it will update all the users in my DB ??
i think i must add some condition such as where id=#user.id but i don't know how i can do that in rails !!!
update_attributes is an instance method not a class method, so first thing you need to call it on an instance of User class.
Get the user you want to update :
e.g. Say you want to update a User with id 1
#user = User.find_by(id: 1)
now if you want to update the user's name and password, you can do
either
#user.update(name: "ABC", pass: "12345678")
or
#user.update_attributes(name: "ABC", pass: "12345678")
Use it accordingly in your case.
For more reference you can refer to Ruby on Rails Guides.
You can use update_all method for updating all records.
It is a class method so you can call it as
Following code will update name of all User records and set it to "ABC"
User.update_all(name: "ABC")
It seems that the "update_attributes" method is not working anymore!
You must use the "update" method like this:
#user=User.update(:name=> params[:name], :user=> params[:username], :pass=> params[:password])

MongoMapper can't save a document with simple example

I'll admit I'm still new to Ruby and now mongoDB so i'm guessing i'm doing something dumb.
For a test I have this code called tester.rb:
require 'Mongo_Mapper'
MongoMapper.database = "myTestDB"
class Person
include MongoMapper::Document
key :first_name, String
key :last_name, String
end
person = Person.new(:first_name => "FirstNameHere", :last_name => "LastNameHere")
person.save
I'll run that code with no errors.
I jump over to mongoDB....my myTestDB has been created, yeah! But if i do "db.myTestDB.find()" I see nothing....
I tried "Person.create()" as well, nada...nothing stored.
I have no clue what I'm doing wrong....
ideas?
Thanks
I think you're calling your find() method wrong in your mongodb command line.
You can see what collections are in your db by running:
show collections
You should see something like:
system.indexes
people
If you see the "people" collection, you can then run:
db.people.find()
to see all of the records that are in that collection.
Hope this helps!

Resources