在控制器更新操作上处理 collection_select

发布于 2024-12-28 11:53:12 字数 1575 浏览 0 评论 0原文

我有 2 个模型

第一个模型 category.rb

class Category
  include Mongoid::Document

 # Relationships
  has_many :boards, :dependent => :destroy , :autosave => true
  accepts_nested_attributes_for :boards

  #fields
  field :name

  #attr
  attr_accessible :name, :boards_attributes
end

第二个模型是 board.rb

class Board
 include Mongoid::Document

#Relationships
 belongs_to :category

  #fields
  field :name
  field :description

  #attr
  attr_accessible :name, :description

end

一个形式:

<%= form_for [@board], :url => user_board_path do |f| %>
 <%= f.text_field :name %>
 <%= f.text_area :description, :cols =>72, :rows => 5, %>
 <%= f.collection_select :category_id, Category.all, :id, :name%>
<% end %>

我在 编辑板视图 中有下 来自boards_controller.rb的更新操作有下一个:

def update
  @board = Board.find(params[:id])
  @category = Category.find(params[:category_id])
  @board.category_id = @category

 respond_to do |format|
   if @board.update_attributes(params[:board])
     format.html { redirect_to user_board_path(@board.user, @board), notice: 'Board was successfully updated.' }
     format.json { head :ok }
   else
     format.html { render action: "edit" }
     format.json { render json: @board.errors, status: :unprocessable_entity }
   end
 end
end

为什么我得到@board.category_id nil?我想用我在 select 中选择的值更新 @board.category_id

I have 2 models

The first model category.rb

class Category
  include Mongoid::Document

 # Relationships
  has_many :boards, :dependent => :destroy , :autosave => true
  accepts_nested_attributes_for :boards

  #fields
  field :name

  #attr
  attr_accessible :name, :boards_attributes
end

The second model its board.rb

class Board
 include Mongoid::Document

#Relationships
 belongs_to :category

  #fields
  field :name
  field :description

  #attr
  attr_accessible :name, :description

end

I have in edit board view the next form:

<%= form_for [@board], :url => user_board_path do |f| %>
 <%= f.text_field :name %>
 <%= f.text_area :description, :cols =>72, :rows => 5, %>
 <%= f.collection_select :category_id, Category.all, :id, :name%>
<% end %>

and I have in update action from boards_controller.rb the next:

def update
  @board = Board.find(params[:id])
  @category = Category.find(params[:category_id])
  @board.category_id = @category

 respond_to do |format|
   if @board.update_attributes(params[:board])
     format.html { redirect_to user_board_path(@board.user, @board), notice: 'Board was successfully updated.' }
     format.json { head :ok }
   else
     format.html { render action: "edit" }
     format.json { render json: @board.errors, status: :unprocessable_entity }
   end
 end
end

Why I get the @board.category_id nil? I want update the @board.category_id with the value that I choose in select

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

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

发布评论

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

评论(1

好听的两个字的网名 2025-01-04 11:53:12

问题是 @board.category_id = @category (您将对象设置为 id 字段)。应该是

@board = Board.find(params[:id])
@category = Category.find(params[:category_id])
@board.category = @category

,或者如果 @category 未在控制器或视图中使用,您可以编写

@board = Board.find(params[:id])
@board.category_id = params[:category_id]

第二个解决方案删除类别上的“SELECT”请求

The problem is @board.category_id = @category (you set an Object to an id field). It should be

@board = Board.find(params[:id])
@category = Category.find(params[:category_id])
@board.category = @category

or if @category is not used in the controller nor in the view, you could write

@board = Board.find(params[:id])
@board.category_id = params[:category_id]

The second solution removes a "SELECT" request on the Categories

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