在控制器更新操作上处理 collection_select
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
@board.category_id = @category
(您将对象设置为 id 字段)。应该是,或者如果
@category
未在控制器或视图中使用,您可以编写第二个解决方案删除类别上的“SELECT”请求
The problem is
@board.category_id = @category
(you set an Object to an id field). It should beor if
@category
is not used in the controller nor in the view, you could writeThe second solution removes a "SELECT" request on the Categories