Finding the next item, in a group of items.

Please note I've also tried this: Tutorial.next_lesson(@tutorial.id,
@tutorial.category_id)

Which results in some compile errors: wrong number of arguments (2 for
0)

I guess this is because my model doesn't take any parameters, and I'm
giving it 2...

I'll be looking around for how I can make it accept 2 parameters (or
fix the previous error) but it would be nice if someone could help
out,

Thanks In Advance,

Joe

Would I be correct in saying it's because the model method doesn't
expect to be given any parameters, or is it a different problem? It
would seem that other people with errors of this kind are having
different problems.

Please Help,

Thanks In Advance,

Joe

Joe wrote:

Please note I've also tried this: Tutorial.next_lesson(@tutorial.id,
@tutorial.category_id)

Which results in some compile errors: wrong number of arguments (2 for
0)

I guess this is because my model doesn't take any parameters, and I'm
giving it 2...

I'll be looking around for how I can make it accept 2 parameters (or
fix the previous error) but it would be nice if someone could help
out,

Check your method definition. It probably doesn't expect 2 parameters.

If you're asking this sort of question, you really need to read
Programming Ruby (available free at http://www.ruby-doc.org ). You
can't do Rails development without an understanding of the Ruby
language, and you'll get that understanding much faster by reading than
by posting here.

Thanks In Advance,

Joe

Best,

Ah wow! That was pretty easy, just parameters like normal functions in
other languages.

Well I've got it to accept the parameters like this:

  def self.next_lesson(tutID, catID)
  find :all, :conditions => ["position > ? AND category_id = ?",
tutID, catID], :order => 'position', :limit => 1
  end

However I'm getting this error: undefined method `nil_class_path'

The show code is this:

<%= link_to 'Next', Tutorial.next_lesson(@tutorial.id,
@tutorial.category_id) %>

As far as I'm aware this is something to do with the link itself, but
I don't know why. Googling it seems to reveal it's a common problem
with many different solutions for different cases.
Does anyone know why this is happening?

EXTRA NOTE: By the way I plan to learn more ruby as it is really easy,
and is helping my RoR lots.

Please Help,

Thanks In Advance,

Joe

Marnen Laibow-Koser wrote:

<%= link_to 'Next', Tutorial.next_lesson(@tutorial.id,
@tutorial.category_id) %>

Does anyone know why this is happening?

I'm only gonna reply this time because you're being so dammed polite.
But no more from me on this thread...

You're putting the result of your class method into the link_to; the
link_to expects a single AR object, but the method is returning an
array of them.

You could test all this yourself in a console. Run your method and see
what happens. Then either change the :all to :first, or do something
else to get a single object (urm... I dunno... like maybe use a plugin
that does it all for you... I've heard acts_as_list is good :wink:

EXTRA NOTE: By the way I plan to learn more ruby as it is really easy,
and is helping my RoR lots.

Good for you. It will, given what the first 'R' stands for!

THANK YOU!

This was the missing piece of the puzzle, with changing it to :first
and some more editing I've now got it to work!

If I have any more problems I'll probably create a new topic (unless I
get them very quickly),

Thanks again,

Joe

Aah wait. One last issue, and it IS the last problem.

I can't get it to display the URL right; basically it goes to
tutorials/NextTutID and I want it to go to categories/catID/
tutorials/NextTutID

In routes.rb this is the line that makes most things go like this:

map.connect 'categories/:id/tutorials/:action', :controller =>
'categories'

To get it working from this I tried using this link for the link, but
it didn't seem to make a difference:

<% @NextLesson = Tutorial.next_lesson(@tutorial.position,
@tutorial.category_id) %>
<%= link_to 'Next lesson', @NextLesson, :controller =>
'categories', :id => (@category.id), :action => (@NextLesson) %>

When I've used this format before I've used lines like this, but this
technique is basically what I've done above, and it just isn't
changing..

<%= link_to tutorial.name, :controller => 'categories', :id =>
(@category), :id => (@category.id), :action => (tutorial.id) %>

I think that my routes.rb route URL system is currently bad as it
randomly uses the action as the tutorial id etc, but I didn't konw
another way to do it so it's setup this way now; Does anyone know how
I can change the URL it links to from tutorials/11 (for example) to
categories/1/tutorials/11 ?

This is my LAST query on this topic and when it's sorted I will have
finally finished a new system I'm working on!

Thanks for all your help so far guys,

Please Help,

Thanks In Advance,

Joe

I've just tried using this in routes.rb:
map.resources :tutorials, :path_prefix => '/categories/:category_id'

However the link isn't changing, it's still just going to /tutorials/
ID, the above line is just making it work when I type in a URL with
that format.
Now I'm beggining to think that it's possibly the link itself causing
the issues, and not so much the routes.rb file.

Please Help,

Thanks In Advance,

Joe

Bump?! This is this final step, please can anyoe help me?

Thanks In Advance,

Please Help,

Joe

Y'know I think this topic is too long anyway, so I'm probably going to
create a new topic on the issue tonight (unless someone wants to reply
between now and then).

Thanks For Your Help Guys!

Joe