I have a model called "cube," which represents a collection of trading cards:
app/models/cube.rb
class Cube < ApplicationRecord
has_many :cubecards
validates :name, length: { in: 3..30 }
end
As you can see, this model has a has_many relationship with cube_cards:
app/models/cube_card.rb
class CubeCard < ApplicationRecord
belongs_to :cube
end
While following this rails guide for help creating this relationship, the code references this form style:
<%= form_with(model: [ @article, @article.comments.build ], local: true) do |form| %>
For testing purposes, I just want people to input a number that corresponds with the card ID, so I have the following form:
app/views/cubes/show.html.erb
<%= form_with(model: [ @cube, @cube.cubecards.build ], local: true) do |form| %>
<p>
<%= number_field(:card, :id, in: 1.0..8000.0, step: 1) %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
The problem is, @cube.cubecards.build
is giving me trouble: rails throws an uninitialized constant error for Cube::CubeCard
.
In other threads, I noticed that plurality has been a common issue, but I can't find anything that would be a problem here.
I also noticed that this fellow had a similar issue, but as far as I can tell the accepted solution does not apply to my code and the highly-voted solutions are about the controller. Here is my cube cards controller:
app/controllers/cube_cards_controller.rb
class CubeCardsController < ApplicationController
def create
@cube = Cube.find(params[:cube_id])
@cubecard = @cube.cubecards.create(cubecard_params)
redirect_to cube_path(@cube)
end
private
def cubecard_params
params.require(:cubecard).permit(:card)
end
end
To clarify once more, a cube
can have many cube_cards
.
I have also tried changing the plurality of cubecard
to cubecards
, adding the underscore (cubecard
-> cube_card
and cube_cards
), and even the capitalization cubecard
-> CubeCard
and CubeCards
to no avail.
The tutorial I'm following seems to skip right on through to the form without a single issue, so I know that, being a newbie, I must be making some elementary mistake, but I've been working for about a day now with no apparent solution.
I'm running this locally on a Windows 7 machine, with plans to migrate it to a web host once complete.
If it's helpful, I've posted the code on github.
NameError in Cubes#show
Showing C:/Sites/pokecube/app/views/cubes/show.html.erb where line #25 raised:
uninitialized constant Cube::Cubecard
Extracted source (around line #25):
<p>
Add a Card:
<%= form_with(model: [ @cube, @cube.cubecards.build ], local: true) do |form| %>
<p>
<%= number_field(:card, :id, in: 1.0..8000.0, step: 1) %>
</p>
Also, my console spits out the following error:
Completed 500 Internal Server Error in 69ms (ActiveRecord: 1.0ms)
ActionView::Template::Error (uninitialized constant Cube::Cubecard):
22:
23: <p>
24: Add a Card:
25: <%= form_with(model: [ @cube, @cube.cubecards.build ], local: true) do |form| %>
26: <p>
27: <%= number_field(:card, :id, in: 1.0..8000.0, step: 1) %>
28: </p>
app/views/cubes/show.html.erb:25:in `_app_views_cubes_show_html_erb___540381856_44810532'
I found a solution to this issue. Unfortunately, I've fallen into a lot of the same trappings as people who have asked similar questions before me: the issue here stems from improper naming of my variables.
I was able to fix the code by changing
@cube.cubecards
to@cube.cube_cards
Additionally, in the params, I changed
:cubecard
to:cube_card
As it turns out, the only time I should have used cubecard as all one word is in the
Class
declarations at the beginning of each file. Every thing else references the tablecube_cards
, and as such should be spelled exactly in that manner.