使用嵌套表单和 has_many :through 在 Ruby on Rails 中创建类别下拉字段

发布于 2024-10-22 05:32:33 字数 2168 浏览 1 评论 0原文

编辑:看起来在任务的创建方法中再次定义@categories确实解决了错误。当我提交任务时,仍在努力实际更新类别,因为现在它忽略它。有什么想法吗?

大家好,

我正在尝试在我的第一个 Rails 项目中创建一个下拉列表,以便从任务的类别列表中进行选择。我使用分类模型将任务模型与类别链接起来。

经过一番努力,我已经让下拉菜单正确显示在新任务表单上,但是当我点击提交时,表单出现以下错误:

NoMethodError in Tasks#create

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map

非常感谢您提供的任何帮助。这是我正在使用的代码(为简洁而摘录)...

我的新任务表单 (HAML) 的相关部分:

= f.fields_for :categorization do |sub|
    = sub.label :name, 'Category'
    = sub.collection_select(:category_id, @categories, :id, :name, :include_blank => 'Select a Category')

tasks_controller.rb:

def new
    @task = Task.new
    @categories = Category.all
end

def create
    @task = current_user.tasks.build(params[:task])
    if @task.save
        flash[:success] = "Task created!"
        redirect_to root_path
    else
        render 'new'
    end
end

分类.rb:

class Categorization < ActiveRecord::Base
    belongs_to :task
    belongs_to :category

    validates :task_id, :presence => true
    validates :category_id, :presence => true
end

category.rb:

class Category < ActiveRecord::Base
    has_many :categorizations, :dependent => :destroy
    has_many :tasks, :through => :categorizations

    validates :name, :presence => true
end

task.rb

class Task < ActiveRecord::Base
    attr_accessible :title, :body

    has_many :categorizations, :dependent => :destroy
    has_many :categories, :through => :categorizations

    accepts_nested_attributes_for :categorizations

    validates :title, :presence => true
    validates :body, :presence => true
end

routes.rb:

resources :tasks

resources :categories do
    member do
        get :tasks
    end
end

有什么想法吗?非常感谢您的浏览,如果您需要其他帮助,请告诉我。

海顿

Edit: It looks like defining @categories again in the task's create method did the trick of clearing up the error. Still working on actually having the category update when I submit the task, because right now it's ignoring it. Any ideas?

Hi all,

I'm trying to create a drop down list in my first Rails project in order to select from a list of categories for a task. I've used a Categorization model to link the Task model with a Category.

After some effort, I've gotten the drop down to show up properly on the new task form, but when I hit submit, the form gives the following error:

NoMethodError in Tasks#create

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map

Any help you could provide is really appreciated. Here is the code I'm using (excerpted for brevity)...

The relevant part of my new task form (HAML):

= f.fields_for :categorization do |sub|
    = sub.label :name, 'Category'
    = sub.collection_select(:category_id, @categories, :id, :name, :include_blank => 'Select a Category')

tasks_controller.rb:

def new
    @task = Task.new
    @categories = Category.all
end

def create
    @task = current_user.tasks.build(params[:task])
    if @task.save
        flash[:success] = "Task created!"
        redirect_to root_path
    else
        render 'new'
    end
end

categorization.rb:

class Categorization < ActiveRecord::Base
    belongs_to :task
    belongs_to :category

    validates :task_id, :presence => true
    validates :category_id, :presence => true
end

category.rb:

class Category < ActiveRecord::Base
    has_many :categorizations, :dependent => :destroy
    has_many :tasks, :through => :categorizations

    validates :name, :presence => true
end

task.rb

class Task < ActiveRecord::Base
    attr_accessible :title, :body

    has_many :categorizations, :dependent => :destroy
    has_many :categories, :through => :categorizations

    accepts_nested_attributes_for :categorizations

    validates :title, :presence => true
    validates :body, :presence => true
end

routes.rb:

resources :tasks

resources :categories do
    member do
        get :tasks
    end
end

Any thoughts? Thank you so much for taking a look, and let me know if you need anything else to help.

Haidn

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守不住的情 2024-10-29 05:32:33

我认为您需要将以下行添加到您的 categorization.rb 中:

attr_accessible :category_id

如果没有该行,您将无法在tasks_controller.rb 中使用的构建方法中设置category_id。

请告诉我这是否可以解决您的问题!

I think you're going to need to add the following line to your categorization.rb:

attr_accessible :category_id

Without that line, you cannot set the category_id in the build method you are using in tasks_controller.rb

Let me know if that fixes it for you!

风铃鹿 2024-10-29 05:32:33

好的,我将尝试自行找出正确实现保存的方法,但我发现了原始问题的解决方案,即在我的任务创建方法中再次声明@categories。希望这对某人有帮助!

OK, I'm going to try figuring out correctly implementing the save on my own, but I discovered the solution for my original problem, which was declaring @categories again in my Task create method. Hopefully that helps somebody!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文