简体   繁体   English

如何让 Rails select() 工作?

[英]How do I get Rails select() to work?

I'm running Rails 2.3.5 on a hosted server, and cannot upgrade to 3.我在托管服务器上运行 Rails 2.3.5,无法升级到 3。

I have one database, and it has a list of cartoons with id , date , and title .我有一个数据库,它有一个带有iddatetitle的卡通列表。 I can navigate through them using text links, and I also want to be able to navigate using a dropdown select , but I can't get the select tag working.我可以使用文本链接浏览它们,并且我还希望能够使用下拉菜单select进行导航,但我无法使select标签正常工作。 This isn't a form to create a new element.这不是创建新元素的表单。 Whereas the form shows up (being empty), the select tag does not show up, not even an empty select tag.尽管form显示(为空),但select标签不显示,甚至没有空的select标签。

Here's what I have:这是我所拥有的:

*comics_controller.rb* *comics_controller.rb*

class ComicsController < ApplicationController
  before_filter :get_all_comics

  def get_all_comics
    @comics=Comic.find(:all, :order => "date DESC")
  end

  def view
    if params[:id].nil?
    @comic=@comics.first
    @prev=@comics[1]
    else
        @comic=Comic.find(params[:id])
        @prev=Comic.find(:first, :conditions => ["date < ?", @comic.date],
                             :order => "date DESC")
        @next=Comic.find(:first, :conditions => ["date > ?", @comic.date],
                                 :order => "date ASC")
    end
  end

end

comics/view.html.erb漫画/view.html.erb

<% form_for :comics, :url => { :action => "view" } do |f|
    f.select(:id,Comic.all,:prompt => true)
end %>
<img src="directory/<%= @comic.filename %>" />
<p class="title"><%= @comic.title %></p>
<p class="date"><%= @comic.date %></p>
<% unless @prev.nil? %>
    <a href="<%= @prev.id %>">Previous</a>
<% end
   unless @next.nil? %>
    <a href="<%= @next.id %>">Next</a>
<% end %>

The basic problem here is that the block tags you are using.这里的基本问题是您正在使用的块标签。

Rails uses two types of template tags: the <%= ones are used for anything which should be evaluated and returned to the client, whereas the <% would be used for control statements which do not return anything to the client. Rails 使用两种类型的模板标签: <%=用于任何需要评估并返回给客户端的内容,而<%用于不向客户端返回任何内容的控制语句。

Note also that anything in Rails 2 which is output with <%= is not safely escaped, and you should probably be using <%= h @comic.title %> (h for html escaping).另请注意,Rails 2 中带有<%=的 output 的任何内容都没有安全转义,您可能应该使用<%= h @comic.title %> (h 用于 html 转义)。 However, if you use the plain <%= syntax in Rails 3, it will handle the escaping for you.但是,如果您在 Rails 3 中使用普通的<%=语法,它将为您处理 escaping。

You would probably do better to move to Rails 3 if you are learning the framework from scratch.如果您从头开始学习该框架,那么迁移到 Rails 3 可能会更好。

<% form_for ... do |f| %>
<%= f.select(...) %>
<% end %>

Edit: I suggest you also add <%= submit_tag %> somewhere编辑:我建议你也添加<%= submit_tag %>某处

I think the collection you are using needs a bit of formatting我认为您正在使用的集合需要一些格式

f.select :id, Comic.all, :prompt => true

try尝试

f.select :id, Comic.all.map { |c| [c.title, c.id] }, :prompt => true

# collection should resemble this format
[
  ["Batman", 1],
  ["Superman", 2],
  ...
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM