The “create new entry” page on a many-to-many-relationship

岁酱吖の 提交于 2020-01-14 06:22:13

问题


I am a new rails user. This is probably a very basic question, but I need help

I am making an app that has a Bands table, a Festivals table, and a Participations table in a many-to many relationship. My models are as follows:

class Band < ActiveRecord::Base
  attr_accessible :year, :genere, :name, :country
  has_many :participations
  has_many :festivals, :though => :participations
end

class Festival < ActiveRecord::Base
  attr_accessible :city, :name, :country
  has_many :participations
  has_many :bands, :through => :participations
end

class Participation < ActiveRecord::Base
  attr_accessible :year
  belongs_to :band
  belongs_to :festival
end

When I excecute my app with rails server I can access localhost:3000/bands and /festivals normally, and the new band page works perfectly. When I access localhost:3000/participations it loads normally (and shows an empty table with both ids and the year) but clicking on new participation shows me the following error:

undefined method `bands_id' for #<Participation:0xba1a5760>

Extracted source (around line #16):

13: 
14:   <div class="field">
15:     <%= f.label :bands_id %><br />
16:     <%= f.number_field :bands_id %>
17:   </div>
18:   <div class="field">
19:     <%= f.label :festivals_id %><br />

Trace of template inclusion: app/views/participations/new.html.erb

Rails.root: /home/User/apps/festnum

I am a new rails user and am unsure how to change the view controller of the participations table. Thanks!

PD: I would also like to know how to add a drop down menu in the participations creation page with the festivals and bands that have already been created.

EDIT: The previous issue has been resolved. However, I am still looking for a way to make a drop down menu in the participations creation page with the festivals and bands that have already been created, and if it could be with the festival name and not the id even better. Another issue: When I access the create new participation page, I fill in the fields with data of a festival and a band that I already created (ID 1 for both fields, I used a SELECT statement in my database and made sure those entries already exist) and I get the following error:

Couldn't find festival without an ID.

Im pretty sure the error is here: (At the participations controller)

def create
@participation = Band.new(params[:band])
@participation.festivals << Festival.find(params[:festival_id])

respond_to do |format|
  if @participation.save
    format.html { redirect_to @participation, notice: 'Participation was $
    format.json { render json: @participation, status: :created, location:$
  else
    format.html { render action: "new" }
    format.json { render json: @participation.errors, status: :unprocessab$
  end
end

end

Could you please help me again?


回答1:


The name of the field has to be singular:

14:   <div class="field">
15:     <%= f.label :band_id %><br />
16:     <%= f.number_field :band_id %>
17:   </div>

(you have <%= f.number_field :bands_id %>)



来源:https://stackoverflow.com/questions/20022693/the-create-new-entry-page-on-a-many-to-many-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!