I am looking at booksindex for an example of nested routes. I see where the nesting is declared in the routes file. But the path names don't seem to reflect nesting. (Though they obviously work.)
As close as I understand, without any special configuration, the route parameter on the for index.html.erb in the index_entries view folder, would be, for example, <td><%= link_to 'Show', book_index_entry_url(@book, index_entry) %></td>. With @book providing the book ID, and index_entry providing the index_entry id.
Is that correct?
I know there are shortcuts to reduce the name parameter, but in order to understand it, I am trying to figure out what the actual syntax should be for route parameters.
Is it possible to see a version with routes fully flushed out, in a non-shortcut way, just to get the flavor of the correct syntax?
X appears to be an Array but I am having difficulty navigating X. What little bit that I can see does not include units. Second, I would really like to see the sql code used to get whatever is in X. Setup.rb from a previous assignment (I assume for use without Rails; just ActiveRecord with ruby) does not provide me sufficient clues to see the sql. Given the number of hours I have spent on this, I am ready for some hints. Thanks Brian
Incidentally, X should not be capitalized. If you do that, it's a constant.
As well, you probably want to put in a variable id for where you have the literal "6" -- this is a big, big "no no." Review section 17.5 of AWDR and take a look at the ? syntax for conditions.
did the rake db:migrate running the ruby script/server browsing to http://localhost:3000/ using Firefox v 2.0.0.18
Get the login page with Login or Register above the text box type John in the text box press enter
logs in... indicates logged in as john in upper right menu bar shows default google news bookmark
click on logout flash[:notfiy] places 'Logged out' above the text entry box (and the label of Login or Register)
This time I type in 'test' logs in... indicates logged in as john in upper right menu bar shows default google news bookmark
I would have expected two things based upon a read of the code:
1.) a flash[:notice] showing 'User created.' 2.) a logged in as test in the upper right corner of the menu bar
When I try to debug it, I see that !@user always evaluates to false. So, I throw a flash[:notice] = @user in after the find, and I see that @user is being set to '#' so it is not nil
Don't know why I see john as the user... confusing me
I haven't looked at the code yet, but I did notice, with just a little playing with the application, that whatever name I type in to the login field, the next page always tells me I'm logged in as "John"
But this is happening on my own machine, with no previous session, so session handling seems unlikely. Digging around, I see that there's only one row in the "users" table. Using script/dbconsole, I update that row so that the login column has the value 'herbert'. Closing my browser and restarting it, I attempt to login with the name "Morris", and the system tells me I'm "Logged in as herbert". [I am not herbert].
I suspect there's something wrong with your find, which is finding the first row in the database table, regardless of the string entered in the form.
I think I've found the problem. I added some debugging code to UsersController#login, just after the call to User.find:
if @user puts "found user #{params.login}" puts "User's name is #{@user.login}" end
to see what this code is getting from the database. Logging in again, I see, amidst lots of other output,
found user User's name is herbert
Note that there's nothing after "user" in "found user". For some reason, params[:login] is blank. Looking at view/users/login.html.erb, we see <%= form.text_field :login so there ought to be a :login entry in the hash.
but viewing the source of the HTML as seen by the browser, we see the corresponding HTML form field has the attributes
id="user_login" name="user[login]" because this is a "<% form_for :user"
In the original version (last year) of LinkWizz, the e-mail address was always used (rather than a username / login name). Somewhere in the refactoring I switched the :conditions => specification, and, obviously, did it wrongly.
When I get home I will check to make sure that this issue hasn't propagated elsewhere (e.g., to CCC).
I actually dumbed down the user controller in LinkWizz and CCC in order to make the switch (will I will be showing) to acts_as_authenticated more dramatic. But as you can see, I dumbed it down too much.
# If there wasn't a user, create one if !@user @user = User.new(params[:user]) if @user.save flash[:notice] = 'User created.' else flash[:notice] = 'Couldn\'t register user.' # We return at this point, so that the POST will # complete, and views/users/login.rhtml will be # used, which has all of the validation info. return end end
should be executed. But I do not see any value being populated in the params[:login]. The site always logs in as 'John'. What am I missing?
I had created that ZIP at work -- the rake "package" task uses the user's login name, and at work I'm jnorman while at home I'm jgn -- and I had just changed the version number in the file name.
Add New Comment
Viewing 33 Comments
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
I am looking at booksindex for an example of nested routes. I see where the nesting is declared in the routes file. But the path names don't seem to reflect nesting. (Though they obviously work.)
As close as I understand, without any special configuration, the route parameter on the for index.html.erb in the index_entries view folder, would be, for example, <td><%= link_to 'Show', book_index_entry_url(@book, index_entry) %></td>. With @book providing the book ID, and index_entry providing the index_entry id.
Is that correct?
I know there are shortcuts to reduce the name parameter, but in order to understand it, I am trying to figure out what the actual syntax should be for route parameters.
Is it possible to see a version with routes fully flushed out, in a non-shortcut way, just to get the flavor of the correct syntax?
Do you already have an account? Log in and claim this comment.
-- Let me see the book associated with this index entry:
<%= link_to 'Book for this index entry', book_path(@index_entry.book) %> |
(put in views/index_entries/show.html.erb
-- Or, let me see the index entries for this book:
<%= link_to 'Index entries for this book', book_index_entries_url(@book) %> |
(put in views/books/show.html.erb
Do you already have an account? Log in and claim this comment.
I am having trouble writing a sql select to get units.id given observation_kinds.id. Here is the ActiveRecord code that I am using.
>> X = Measurement.find(:all, :conditions => "observation_kinds.id = 6", :include => [:observation_kinds, :units])
X appears to be an Array but I am having difficulty navigating X. What little bit that I can see does not include units.
Second, I would really like to see the sql code used to get whatever is in X.
Setup.rb from a previous assignment (I assume for use without Rails; just ActiveRecord with ruby) does not provide me sufficient clues to see the sql.
Given the number of hours I have spent on this, I am ready for some hints.
Thanks
Brian
Do you already have an account? Log in and claim this comment.
x.each { |item| ... }
Incidentally, X should not be capitalized. If you do that, it's a constant.
As well, you probably want to put in a variable id for where you have the literal "6" -- this is a big, big "no no." Review section 17.5 of AWDR and take a look at the ? syntax for conditions.
Do you already have an account? Log in and claim this comment.
I was using the equivalent of x[0].units.id rather than x[0].units.first.id .
Having to explain the problem helps clarify the issues in my own head.
I am still curious about the sql code view though.
Do you already have an account? Log in and claim this comment.
did the rake db:migrate
running the ruby script/server
browsing to http://localhost:3000/ using Firefox v 2.0.0.18
Get the login page with Login or Register above the text box
type John in the text box
press enter
logs in... indicates logged in as john in upper right menu bar
shows default google news bookmark
click on logout
flash[:notfiy] places 'Logged out' above the text entry box (and the label of Login or Register)
This time I type in 'test'
logs in... indicates logged in as john in upper right menu bar
shows default google news bookmark
I would have expected two things based upon a read of the code:
1.) a flash[:notice] showing 'User created.'
2.) a logged in as test in the upper right corner of the menu bar
When I try to debug it, I see that !@user always evaluates to false.
So, I throw a flash[:notice] = @user in after the find, and I see that @user is being set to '#' so it is not nil
Don't know why I see john as the user... confusing me
Mike
Do you already have an account? Log in and claim this comment.
click on logout
flash[:notfiy] places 'Logged out' above the text entry box (and the label of Login or Register)
This time I type in 'test'
logs in... indicates logged in as john in upper right menu bar
shows default google news bookmark
-----
Notice that UserController#logout says: reset_session - that is supposed to kill the session, so that, e.g., session[:login] should not have a value.
Let me get back to you on that, with an explanation or a fix.
Do you already have an account? Log in and claim this comment.
@user = User.find(:first, :conditions => params[:login])
flash[:notice] = @user.login # outputs 'john'
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
I will provide an explanation or a fix tonight: It's possible that something subtle has changed between Rails 1.x and 2.1 regarding session handling.
Do you already have an account? Log in and claim this comment.
Digging around, I see that there's only one row in the "users" table. Using script/dbconsole, I update that row so that the login column has the value 'herbert'. Closing my browser and restarting it, I attempt to login with the name "Morris", and the system tells me I'm "Logged in as herbert". [I am not herbert].
I suspect there's something wrong with your find, which is finding the first row in the database table, regardless of the string entered in the form.
Do you already have an account? Log in and claim this comment.
if @user
puts "found user #{params.login}"
puts "User's name is #{@user.login}"
end
to see what this code is getting from the database.
Logging in again, I see, amidst lots of other output,
found user
User's name is herbert
Note that there's nothing after "user" in "found user". For some reason, params[:login] is blank.
Looking at view/users/login.html.erb, we see
<%= form.text_field :login
so there ought to be a :login entry in the hash.
but viewing the source of the HTML as seen by the browser, we see the corresponding HTML form field has the attributes
id="user_login" name="user[login]" because this is a "<% form_for :user"
Do you already have an account? Log in and claim this comment.
User.find(:first, :conditions => params[:user])
where params[:user] is a hash, containing the key :login
since there's no params[:login], the existing call
User.find(:first, :conditions => params[:login])
calls User.find with no conditions, and always returns the first row in the users table.
Do you already have an account? Log in and claim this comment.
I now see where this bug crept in.
In the original version (last year) of LinkWizz, the e-mail address was always used (rather than a username / login name). Somewhere in the refactoring I switched the :conditions => specification, and, obviously, did it wrongly.
When I get home I will check to make sure that this issue hasn't propagated elsewhere (e.g., to CCC).
I actually dumbed down the user controller in LinkWizz and CCC in order to make the switch (will I will be showing) to acts_as_authenticated more dramatic. But as you can see, I dumbed it down too much.
Do you already have an account? Log in and claim this comment.
Mike
Do you already have an account? Log in and claim this comment.
I'm going through the code and playing around with it. When I enter a new user, it looks like the user_controller code:
@user = User.find(:first, :conditions => params[:login])
# If there wasn't a user, create one
if !@user
@user = User.new(params[:user])
if @user.save
flash[:notice] = 'User created.'
else
flash[:notice] = 'Couldn\'t register user.'
# We return at this point, so that the POST will
# complete, and views/users/login.rhtml will be
# used, which has all of the validation info.
return
end
end
should be executed. But I do not see any value being populated in the params[:login]. The site always logs in as 'John'. What am I missing?
Mike
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
So then we effectively say . . .
if !nil
(!nil evaluates to true)
i.e., if there is no user by that name, create one.
OK?
Do you already have an account? Log in and claim this comment.
Could you provide some steps to reproduce:
1. Start not logged-in
2. Enter John as the user
etc.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
I had created that ZIP at work -- the rake "package" task uses the user's login name, and at work I'm jnorman while at home I'm jgn -- and I had just changed the version number in the file name.