【问题标题】:"One-to-Many" Rails Form Creation“一对多” Rails 表单创建
【发布时间】:2014-08-30 14:31:35
【问题描述】:

我有一个“一间教室有很多学生”的协会。我想创建一个表单,我可以在其中创建一个学生并为他分配一个教室。我在创建表单时遇到问题。

model/classroom.rb

class Classroom < ActiveRecord::Base
  has_many :students
end

模型/学生.rb

class Student < ActiveRecord::Base
  belongs_to :classroom
end

我想创建一个新学生并将其分配到某个教室。

<%= form_for(@student) do |f|%>


<%= f.label :name %>
<%= f.text_field :name %>

<br />
<br />

<%= f.label :student.classroom.number %>       #Is this correct? 
<%= f.text_field :student.classroom.number %>  #Is this correct?

<%= f.submit %>

<%end%>

每个模型的属性是

1.9.3-p448 :026 > Classroom
 => Classroom(id: integer, number: string, created_at: datetime, updated_at: datetime) 
1.9.3-p448 :027 > Student
 => Student(id: integer, name: string, created_at: datetime, updated_at: datetime, classroom_id: string) 

students_controller

class StudentsController < ApplicationController
  def index
    @students = Student.all
  end

  def show
    @student = Student.find(params[:id])
  end

  def new
    @student = Student.new
  end

  def create
    @student = Student.new(article_params)
    respond_to do |format|
    if @student.save
      format.html {redirect_to(@student, notice: 'Student was successfully created.')}
    else
      format.html {render action: "new"}
    end
end
  end

  private
    def article_params
      params.require(:student).permit(:name, :classroom_id)
    end
  end

教室控制器

class ClassroomsController < ApplicationController
  def index
    @classrooms = Classroom.all
  end

  def show
    @classroom = Classroom.find(params[:id])
  end

  def new
    @classroom = Classroom.new
  end

  def create
    @classroom = Classroom.new(article_params)
    respond_to do |format|
    if @classroom.save
      format.html {redirect_to(@classroom, notice: 'Classroom was successfully created.')}
    else
      format.html {render action: "new"}
    end  
end
  end

  private
    def article_params
      params.require(:classroom).permit(:number)
    end
  end

【问题讨论】:

  • 您要将学生分配到现有教室还是新教室?
  • 现有教室。 :)
  • @RailsNewbie 不,这是不正确的。使用隐藏字段发送您的教室编号或使用选择,用户可以从教室列表中选择,或者您可以在控制器方法本身中分配班级。根据您的应用设计决定您想做什么

标签: ruby-on-rails


【解决方案1】:

如果您已经知道要将他添加到哪个教室,您可以设置一个隐藏字段,将其设置为教室本身:

<%= f.hidden_field, :classroom_id, value: here_you_put_the_classroom_id $>

别忘了在控制器的允许参数中添加 :classroom_id。

如果您想要选择让学生所在的教室的选项,您可以采取另一种方法,您可以创建一个选择字段来传递所有教室。

<% classroom_array = Classroom.all.map { |classroom| [classroom.name, classroom.id] } %>
<%= options_for_select(classroom_array) %>

不要忘记再次添加允许的参数。 希望对您有所帮助。

***更新***

options_for_select 应该放在 de select 标签内,像这样:

<% classroom_array = Classroom.all.map { |classroom| [classroom.number, classroom.id] } %> 
<%= f.label :classroom %> 
<%= f.select(:classroom_id, options_for_select(classroom_array)) %>

***更新 2***

Pluck 也可以是一种选择,只要您将教室 id 作为参数传递。所以,代码可以重构为:

<%= f.label :classroom %>
<%= f.select :classroom_id, Classroom.all.pluck(:name, :id) %>

【讨论】:

  • 更改为 但我没有看到任何选择下拉菜单。
  • 它的作品。这是工作代码:
  • 我的代码是关于城市的,所以我错过了。对不起。
  • 杰西,如果你有空,你能给我解释一下代码吗?为什么你的有效,但 bdx 无效?不太明白。另外,我怎样才能重构代码,以便我可以使用 students_controller 来代替?
  • 其实pluck和map返回的是一个数组数组。如果每个数组只有一个选项,则所选选项的值与选项中显示的值相同,因此 尝试设置教室里的学生,id 为classic.number,而不是classic.id。要解决这个问题,您需要传递数组 [name, id]。名称是将显示的内容,如果选择,id 将是值。像这样,您将学生设置为具有 id 教室.id 的教室
【解决方案2】:

假设您要将Student 分配给现有的Classroom

首先,确保 Student 模型在 attr_accessible 中有以下内容:

attr_accessible :classroom_id

在您的表单中,您应该可以执行以下操作,而不是第二个标签/文本字段:

<%= f.label :classroom %>
<%= f.select(:classroom_id, Classroom.all.pluck(:number)) %>

请注意,对于 f.select 方法,您必须传递您正在设置的属性,而不是关联名称(即 classroom_id 而不是 classroom

另请注意,最佳实践是将与从模型(即Classroom.all.pluck(:number))收集信息相关的逻辑移动到控制器中的实例变量中,

例如@classrooms = Classroom.all.pluck(:number)

并在您的视图中使用 @classrooms 实例变量。

除了以上内容,您还应该阅读更多关于符号的内容。您在:student.classroom.number 那里尝试过的东西不会像您想象的那样起作用。这里有一个很好的问题:When to use symbols instead of strings in Ruby?

【讨论】:

  • 使用 Rails 控制台: .... 使用你的方法: ---- "classroom_id" 被持久化为 "1E1" 而不是 "1",它是实际的classroom_id
  • 并且“attr_accessible”创建了 NoMethod 错误。相反,我使用了“params.require(:student).permit(:name, :classroom_id)”
  • 当我更改为“”时,Student 将被保留为classroom_id:“1”。我很困惑,请帮助:(
  • 关于 attr_accessible 错误,我假设您使用的是 Rails 3,因为您的控制台输出显示您使用的是 Ruby 1.9.3。我的错。很高兴您在上面得到了另一个解决方案。
  • 至少现在我知道 attr_accessible 在 rails4 中是无关紧要的。谢谢bdx :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多