Ruby on Rails 下拉值不保存
我是 RoR 新手,在尝试从多个下拉列表中保存时遇到问题。我有三个对象——书籍、流派和作者。图书对象具有与其关联的流派和作者,但问题是我只能设法将流派或作者保存到我的图书对象中,而不能同时保存两者。这就是我所在的位置:
class Author < ActiveRecord::Base
validates :name, :presence => true
validates :biography, :presence => true
has_many :books
end
class Genre < ActiveRecord::Base
validates :description, :presence => true
has_many :books
end
class Book < ActiveRecord::Base
belongs_to :genre
belongs_to :author
has_many :cartitems
validates :name, :presence => true
validates :price, :presence => true
validates :description, :presence => true
end
Controller:
def create
#@book = Book.new(params[:book])
@author = Author.find(params[:author].values[0])
@genre = Genre.find(params[:genre].values[0])
@book = @author.books.create(params[:book])
#one or the other saves, but not both
#@book = @genre.books.create(params[:book])
respond_to do |format|
if @book.save
format.html { redirect_to(@book, :notice => 'Book was successfully created.') }
format.xml { render :xml => @book, :status => :created, :location => @book }
else
format.html { render :action => "new" }
format.xml { render :xml => @book.errors, :status => :unprocessable_entity }
end
end
end
不确定这是否有帮助,但视图中的下拉菜单如下所示:
<div class="field">
<%= f.label :genre %><br />
<%= @items = Genre.find(:all)
select("genre", "description", @items.map {|u| [u.description,u.id]}, {:include_blank => true})%>
感谢对此的任何帮助。
编辑-这是我的完整表格。
<%= form_for(@book) do |f| %>
<% if @book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2>
<ul>
<% @book.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :genre %><br />
<%= @items = Genre.find(:all)
select("genre", "description", @items.map {|u| [u.description,u.id]}, {:include_blank => true})%>
</div>
<div class="field">
<%= f.label :author %><br />
<%=@items = Author.find(:all)
select("author", "name", @items.map {|u| [u.name,u.id]}, {:include_blank => true}) %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I'm new to RoR and having an issue when trying to save from multiple dropdowns. I have three objects - books, genres, and authors. A book object has a genre and author associated to it, but the issue is I can only manage to save either a genre or author to my book object, and not both. Here's where I'm at:
class Author < ActiveRecord::Base
validates :name, :presence => true
validates :biography, :presence => true
has_many :books
end
class Genre < ActiveRecord::Base
validates :description, :presence => true
has_many :books
end
class Book < ActiveRecord::Base
belongs_to :genre
belongs_to :author
has_many :cartitems
validates :name, :presence => true
validates :price, :presence => true
validates :description, :presence => true
end
Controller:
def create
#@book = Book.new(params[:book])
@author = Author.find(params[:author].values[0])
@genre = Genre.find(params[:genre].values[0])
@book = @author.books.create(params[:book])
#one or the other saves, but not both
#@book = @genre.books.create(params[:book])
respond_to do |format|
if @book.save
format.html { redirect_to(@book, :notice => 'Book was successfully created.') }
format.xml { render :xml => @book, :status => :created, :location => @book }
else
format.html { render :action => "new" }
format.xml { render :xml => @book.errors, :status => :unprocessable_entity }
end
end
end
Not sure if this will help out or not, but here's what the dropdowns look like in the View:
<div class="field">
<%= f.label :genre %><br />
<%= @items = Genre.find(:all)
select("genre", "description", @items.map {|u| [u.description,u.id]}, {:include_blank => true})%>
Appreciate any help with this.
EDIT - Here's my full form.
<%= form_for(@book) do |f| %>
<% if @book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2>
<ul>
<% @book.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :genre %><br />
<%= @items = Genre.find(:all)
select("genre", "description", @items.map {|u| [u.description,u.id]}, {:include_blank => true})%>
</div>
<div class="field">
<%= f.label :author %><br />
<%=@items = Author.find(:all)
select("author", "name", @items.map {|u| [u.name,u.id]}, {:include_blank => true}) %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新要定义的选择字段,如下所示:
并且您的控制器操作应如下所示:
另外,如果不需要 format.xml 调用,请删除它们,它们只会弄乱您的控制器操作。
Update your select fields to be defined like this:
And your controller action should be like this:
Also, remove the format.xml calls if you don't need them, they're just cluttering your controller action.
有很多不同的方法可以解决您的问题,这将有助于准确查看您的
params
哈希中的内容以及完整的表单是什么样子,但没关系。这是一种方法:这是另一种方法(本质上是同一件事):
您也可以单独实例化这本书:
我的猜测是您没有完全正确地构建表单,否则您的 params 哈希将类似于此
{:书=> {:作者=> 1、:流派=> 5}}
并且您将能够执行以下操作:您不会使用
params[:author]
查找作者,而是使用params[:book] [:author]
如果您确实需要这样做。There are lots of different ways to fix your problem, it would help to see exactly what's in your
params
hash and what your full form looks like, but no matter. Here is one way:Here is another (essentially the same thing):
You could also instantiate the book separately:
My guess is you didn't quite build your form correctly, otherwise your params hash would look similar to this
{:book => {:author => 1, :genre => 5}}
and you would be able to do this:And you would not look up the author using
params[:author]
, but would instead doparams[:book][:author]
if you needed to do it at all.