me and my friend are amateur programmers and we have hit another roadblock in our code.
Our app has a user system where the head/coaches of sports clubs can upload the scores of its associated teams to a single page for all its members to view.
There is a model for the scoreboard which is as follows:
class CreateScoreboards < ActiveRecord::Migration
def change
create_table :scoreboards do |t|
t.string :name_of_scoreboard
t.string :name_of_organization
t.string :name_of_activity
t.references :user, index: true
t.timestamps null: false
end
add_foreign_key :scoreboards, :users
add_index :scoreboards, [:user_id, :created_at]
end
end
The scoreboard model has_many :teams which can be associated with it. And we also declared the belongs_to :scoreboards in the teams model.
The teams model is as follows:
class CreateTeams < ActiveRecord::Migration
def change
create_table :teams do |t|
t.string :name
t.integer :win
t.integer :loss
t.integer :tie
t.references :scoreboard, index:true
t.timestamps null: false
end
add_foreign_key :teams, :scoreboards
end
end
Now this is our problem, inside a div on the scoreboard model show page(show.html.erb), we want to have a form of input where a user can create an instance of the teams model and associate it that that instance of the scoreboard. In another div on the same page, we want to return an array of all the teams that is associated with that instance of the scoreboard model.
Our form looks like this:
<%= form_for(@team) do |f| %>
<%= f.label :name %>
<%= f.text_field :name, class: "form-control" %>
<%= f.label :win %>
<%= f.text_field :win, class: "form-control" %>
<%= f.label :loss %>
<%= f.text_field :loss, class: "form-control" %>
<%= f.label :tie %>
<%= f.text_field :tie, class: "form-control" %>
<%= f.submit "Create", class: "btn btn-primary" %>
<% end %>
We are stuck because we do not know what to do in our controllers, that upon submission, that submitted team is associated to the @scoreboard instance whose show page the form is on. Is a form the only way to submit the data while tying it to another model? We tried @team = scoreboard.teams.build(team_params) in the controller and don't know where we went wrong.
As for displaying the array of associated teams( when we finally make it work), @scoreboard.teams should return that right? Sorry for any unclear description as me and my friend don't even know if we're right sometimes. Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire