Number limited redirects

The following is simply for testing and educational purposes:

  def new     if request.get?       @album = Album.new(:artist_id => params[:artist])     else       @album = Album.new(params[:album])       if @album.save         flash[:notice] = "Album successfully added. Add a Song"         redirect_to :controller => 'song_admin', :action => 'new', :album => @album       else         flash[:notice] = "Failed to add Album"         flash[:error_field] = :album       end     end   end

Once the album is saved, it redirects to allow the user to add a song to the album. On the album form, I have a field: "no_songs" as an integer. I want the redirect to happen as many times as specified in the "no_songs" column.

I'm not sure what syntax to use here.

Do you mean that you only want to redirect if the number of songs in the album is less than no_songs? If so then just test album.songs.size against params[:no_songs].

Max Williams wrote:

Do you mean that you only want to redirect if the number of songs in the album is less than no_songs? If so then just test album.songs.size against params[:no_songs].

Thanks for your reply but what I meant was, I want the redirect to occur as many times as is entered in the 'no_songs' column.

For example: I add an album and set 'no_songs' to 7. Upon update, it redirects me to add a song to the album using parameters. I only want this redirect to occur 7 times as I've told the system that there are only 7 songs in the album.

Pale Horse wrote:

Max Williams wrote:

Do you mean that you only want to redirect if the number of songs in the album is less than no_songs? If so then just test album.songs.size against params[:no_songs].

Thanks for your reply but what I meant was, I want the redirect to occur as many times as is entered in the 'no_songs' column.

For example: I add an album and set 'no_songs' to 7. Upon update, it redirects me to add a song to the album using parameters. I only want this redirect to occur 7 times as I've told the system that there are only 7 songs in the album.

Well, you could add a countup value to a hidden field and then in the controller set an instance variable from it and test if it's > 7. If it is then that's all the attempts used up, if not then increment it and re-render the page, setting the field from the variable.

This is kind of a horrible way of doing things, why not just put space for seven (or whatever) songs on the form in the first place? I know you said it's just an example, i just had to mention that...

This is kind of a horrible way of doing things, why not just put space for seven (or whatever) songs on the form in the first place? I know you said it's just an example, i just had to mention that...

I know what you mean and I agree with you but I'm doing it this way to teach myself associations so I have separate song, album and artist tables. I'm picking it up easy enough, I guess.

Pale Horse wrote:

This is kind of a horrible way of doing things, why not just put space for seven (or whatever) songs on the form in the first place? I know you said it's just an example, i just had to mention that...

I know what you mean and I agree with you but I'm doing it this way to teach myself associations so I have separate song, album and artist tables. I'm picking it up easy enough, I guess.

This doesn't really teach you about associations though, it just teaches you how to do a really horrible application design :slight_smile:

A more useful way to use the associations is to look at the extra methods given to you by the associations, which allow you to add seven songs to an album at once for example.

This doesn't really teach you about associations though, it just teaches you how to do a really horrible application design :slight_smile:

A more useful way to use the associations is to look at the extra methods given to you by the associations, which allow you to add seven songs to an album at once for example.

Well, making tables that are associated together surely does teach me about associations and use of primary keys...

I know what you mean about using more appropriate methods to do this, I was just querying that particular method. Thanks :slight_smile: