关于多对多关系的“创建新条目”页面(The “create new entry” page on a many

编程入门 行业动态 更新时间:2024-10-18 12:26:07
关于多对多关系的“创建新条目”页面(The “create new entry” page on a many-to-many-relationship)

我是一个新的rails用户。 这可能是一个非常基本的问题,但我需要帮助

我正在创建一个具有Bands表,节日表和多对多关系的Participations表的应用程序。 我的模型如下:

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

当我使用rails server执行我的应用程序时,我可以正常访问localhost:3000 / band和/ festivals,并且新的乐队页面可以正常运行。 当我访问localhost:3000 /参与时,它正常加载(并显示一个包含ID和年份的空表)但点击新参与会显示以下错误:

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

我是一个新的rails用户,我不确定如何更改参与表的视图控制器。 谢谢!

PD:我还想知道如何在参与创建页面中添加一个下拉菜单,其中包含已经创建的节日和乐队。

编辑:上一期已经解决。 但是,我仍然在寻找一种方法,在参与创建页面中创建一个下拉菜单,其中包含已经创建的节日和乐队,如果它可以与节日名称而不是id更好。 另一个问题:当我访问创建新参与页面时,我填写了节日数据和我已经创建的乐队的字段(两个字段的ID 1,我在我的数据库中使用了SELECT语句并确保了这些条目存在),我得到以下错误:

没有身份证无法找到节日。

我很确定错误在这里:(在参与控制器)

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

结束

你能帮我一次吗?

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?

最满意答案

该字段的名称必须是单数:

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

(你有<%= f.number_field :bands_id %> )

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 %>)

更多推荐

本文发布于:2023-08-04 12:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416002.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条目   页面   关系   page   create

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!