Jump to content

The ultimate community for Ruby on Rails developers.


Photo

How to conceive of the relationship of these objects?

has_many_through form helper submit

  • Please log in to reply
5 replies to this topic

#1 E Moloney

E Moloney

    Passenger

  • Members
  • 8 posts

Posted 01 September 2014 - 04:00 PM

Hi all,

 

I apologize for the vagueness of my first post, which happened about four or five days ago. I have since gotten over the problem that was facing me then, but am having trouble with another, thornier problem. It goes thusly:

 

I have three classes, Deck, Card and Hero. Deck should belong to hero and have many cards, while hero should have many decks and have many cards through decks. Card belongs to Deck. My idea was this: that whenever someone wants to add a new card to a Deck, he would supply the name (and possibly further details) of a particular card, and then a new Card model would be created, associated with the particular Deck that it was created upon. I had thought of creating a new form, apart from the normal forms created by Rails' scaffolding, to take in these details, create a new Card, and then "attach" it to the relevant Deck. However, I'm simply stumped as to how to do this. Would I need to use something other than "Submit" in my form?

 

One possibility that suggested itself to me was to have a button on the "Show a particular deck" view, which when clicked would invoke the create method of Card. However, attempts to make this work have only resulted in the app misinterpreting my commands and trying to go to cards/index instead. Turning cards/create into a Module was also suggested, but that seems like it would suffer from scope problems, since that method requires card_params as an input, which is another method within Card. In addition, card_params requires access to params, which probably wouldn't work if accessed from Deck, since Deck does not have the same parameters as Card. Does anyone have any idea of how such a problem should be addressed? I will provide code on request.



#2 Ohm

Ohm

    Driver

  • Moderators
  • 371 posts
  • LocationCopenhagen

Posted 01 September 2014 - 07:06 PM

If you want to stick with the scaffolding, you should look into nested resources (http://guides.rubyon...ested-resources)

 

Doing this, you should add a "Add card" button to your deck's show action. This should then point to the nested card resource's new action.


  • E Moloney likes this
Blog: http://ohm.sh | Twitter: @madsohm

#3 E Moloney

E Moloney

    Passenger

  • Members
  • 8 posts

Posted 03 September 2014 - 11:51 AM

Thanks Ohm, I will definetely look into nested resources. The problem, though, is that I have since run into another error, and while I don't want to turn this into a general-purpose-help-me thread, my post on StackExchange about it seems to have lost everyone's interest and is almost certainly dead, since people don't seem to be notified of edits to posts. Basically, my Card model isn't working correctly, as I get the following error:

 

SystemStackError in CardsController#index
stack level too deep

Rails.root: C:/Users/laptop user/Desktop/Rails Projects/HSCreate #(HSCreate is the root, the application folder)
actionpack (4.1.4) lib/action_dispatch/middleware/reloader.rb:79

 

Whenever I try to load localhost:3000/cards. What exactly does this mean? I've taken it to mean that I have some form of circular reference in my code, but I can't see any such circular reference. This is especially confusing to me given that Deck, which is identical to Card in all but name, works perfectly fine. Does anyone have any idea what might be causing this?



#4 Ohm

Ohm

    Driver

  • Moderators
  • 371 posts
  • LocationCopenhagen

Posted 03 September 2014 - 12:43 PM

stack level too deep means exactly what it says. The application got too deep on the stack.

 

Every time you call a method, you add the current place in the code to the stack, so that once the application is done with the method, it knows where it were before calling it.

 

If you go too deep, it normally means that you've done something wrong, e.g. infinite recursion or similar. Do you have method that calls itself or that calls another method that calls back?


Blog: http://ohm.sh | Twitter: @madsohm

#5 E Moloney

E Moloney

    Passenger

  • Members
  • 8 posts

Posted 03 September 2014 - 03:28 PM

I don't think so, though I haven't picked through the code with a fine-toothed comb. If there were such code, however, is there any particular place that it would be likely to be? That is, is self-referential code usually in the controller, or in the views, or somewhere else? I ask because, as I say above, I am fairly certain that cards_controller is only trivially different from decks_controller, which works absolutely fine. I'll look, but there's a fair amount of code to go through and I'd like to know if there's a particular place I should start.



#6 Ohm

Ohm

    Driver

  • Moderators
  • 371 posts
  • LocationCopenhagen

Posted 07 September 2014 - 08:19 PM

Did you ever find out about this?

 

Your error should tell you where in the code the stack overflow occurred. 


Blog: http://ohm.sh | Twitter: @madsohm




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users