Beginner question about attributes

S

Scott Peterson

I don't understand why my model (attached) isn't working.

I generated a scaffold for my 'player' data. If I uncomment the
:validates_presence_of check in my model, the save on the 'new' page
compalins about those fields, and re-displays them without the data I've
typed in (along with the red surrounding and error messages, of course).

If I run the model as is, the save goes through, but none of the fields
except hashed_password get saved. Or, another way to put it is, the
*only* field that gets saved is the one I set manually in the
before_create handler.

I don't understand what I did. I haven't changed the generated view for
new.html.erb, the column names haven't changed in the database. I don't
see what's missing to reconnect those elements.

Attachments:
http://www.ruby-forum.com/attachment/3041/player.rb
 
Y

Yossef Mendelssohn

If I run the model as is, the save goes through, but none of the fields
except hashed_password get saved. =A0Or, another way to put it is, the
*only* field that gets saved is the one I set manually in the
before_create handler.

1. This is a Rails question, not a Ruby question.

2. This has probably much more to do with your controller than your
model.
 
S

Scott Peterson

Thanks for the response. I guess there's a lot I don't understand.
1. This is a Rails question, not a Ruby question.

I thought that Ruby was the language in which all the controllers,
models, and views were written. If I've generated a scaffold and only
changed the model file, it seems like that's where I've introduced the
bug(?)
2. This has probably much more to do with your controller than your
model.

Something in the generated create, where it initializes as such:

@player = Player.new(params[:player]). I'll keep poking around to see
if I can figure out why those params aren't what I think they should be.
 
T

Tim Hunter

Scott said:
Thanks for the response. I guess there's a lot I don't understand.


I thought that Ruby was the language in which all the controllers,
models, and views were written. If I've generated a scaffold and only
changed the model file, it seems like that's where I've introduced the
bug(?)

You're correct. However, Ruby is a general-purpose programming language
used for many things besides Rails. This list is generally frequented by
Ruby users of all stripes and, while there may be a few Rails users
around, you will have more luck getting an answer to your question in
the Rails forum.

See http://www.rubyonrails.com/community

Good luck!
 
S

Scott Peterson

Tim said:
You're correct. However, Ruby is a general-purpose programming language
used for many things besides Rails. This list is generally frequented by
Ruby users of all stripes and, while there may be a few Rails users
around, you will have more luck getting an answer to your question in
the Rails forum.

See http://www.rubyonrails.com/community

Good luck!

Ah, thanks for the clarification. Yossef got me thinking down a
different path, which led me to making those attributes accessible,
seems to be working (well enough for me--as I learn more, I'll probably
figure out that needs to be changed).

Anyway, I appreciate the feedback.
 
T

Tim Hunter

Scott said:
Ah, thanks for the clarification. Yossef got me thinking down a
different path, which led me to making those attributes accessible,
seems to be working (well enough for me--as I learn more, I'll probably
figure out that needs to be changed).

Anyway, I appreciate the feedback.

Don't feel alone. We get one or two new Rails users a week, sometimes
more. After you master Rails maybe you'll want to use Ruby for something
else. If so, come on back. We'll still be here.
 
M

Marc Heiler

After you master Rails maybe you'll want to use Ruby for something

Like RMagick :)

Or for complete world dominance.

I may have some script for that .... world_control.rb

It is unfinished though.
 
P

Phlip

Scott said:
I thought that Ruby was the language in which all the controllers,
models, and views were written. If I've generated a scaffold and only
changed the model file, it seems like that's where I've introduced the
bug(?)

A technical question always gets the best answer on the narrowest possible
forum. To help the Rails forum succeed, we who know Rails try not to answer
questions about it here; that dilutes both groups.
@player = Player.new(params[:player]). I'll keep poking around to see
if I can figure out why those params aren't what I think they should be.

How are your unit tests?
 
Y

Yossef Mendelssohn

Ah, thanks for the clarification. =A0Yossef got me thinking down a
different path, which led me to making those attributes accessible,
seems to be working (well enough for me--as I learn more, I'll probably
figure out that needs to be changed).

To be honest, I didn't really look closely at the model because I had
already categorized this as a Rails question by that point.

There was no way to be absolutely positive given the information
provided, but it could be reasonable to assume you were using a
generated controller with standard actions. Really, the problem was in
the model with the use of attr_accessible, which keeps other
attributes from being mass-assigned.

Of course, the question was still in the wrong place, much like
attr_accessible and attr_protected are in the wrong part of the MVC
trio. But that's a discussion for another day.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top