Hi, I'm trying to make a rails app and although I understand the logic
of what I'm trying to do, the syntax used in ruby really confuses me as
it seems to do a lot of the work automatically.
I have 3 tables, user, forum and post. They are related as follows:
User
has_many :posts
end
Forum
has_many :posts
end
Post
belongs_to :forum
belongs_to :user
end
When a user registers or logs in, the user is stored in a session. The
user can then add a post to a form by using the default form generated
by the scaffold. However, atm, the user can type in any user ID into
the form. I would like to delete this part of the form and use the
current [:user][:user_id] in it's place but I don't know how to combine
this with the already existing params in the controller.
Any advice would be appreciated, thanks in advance.
ok, this makes sense. most people store only the id in the session. in
your case it seems to store the whole user object. but that's ok.
after finding user with name/pwd @user=User.find(...)
session[:user] = @user
this function simply returns this object (in Ruby you don't need an
explicit return statement, whatever is in the last line will be
returned, session[:user] in that case)
def current_user
session[:user]
end
current_user will give you the whole user and you can get the id:
current_user.id
Hi again. Sorry to be a pain! I can access and display the values
fine, but how can I combine the value with the values obtained by the
form?
For example, if I have a variable
@currentid = current_user.id
How can I add it to the params in the create method of the controller?
I apologise for being slow, but rails confuses me as it seems to do a
lot of the work for you, which is great if you understand it but bizzare
when you are trying to analyze things.