Select_Tag无法在rails中工作?

时间:2014-03-07 18:17:51

标签: ruby-on-rails ruby-on-rails-4 html-select

以下是select_tag

的代码
<%= select_tag :tp, options_for_select([["movable",1],["fixed",2]]), :prompt => "select type of asset" %>

如果我替换为:

<%= f.text_area :tp %>

如果这还不够,我会根据您的意愿提供更多代码: 这是我使用select_tag

后的日志
Started POST "/assets" for 127.0.0.1 at 2014-03-07 23:40:34 +0530
Processing by AssetsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Gib6rSODjXgXLZ9+5sDcq0ZatkA7144hU+Em2X7KONU=", "asset"=>{"name"=>"", "location"=>"", "cost"=>""}, "tp"=>"1","commit"=>"Save"}
(1.0ms)  BEGIN
(1.0ms)  ROLLBACK
Redirected to http://localhost:3000new
Completed 302 Found in 5ms (ActiveRecord: 2.0ms)
[2014-03-07 23:40:34] ERROR URI::InvalidURIError: the scheme http does not accept registry part: localhost:3000new (or bad hostname?)
    F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/uri/generic.rb:1203:in `rescue in merge'
     F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/uri/generic.rb:1200:in `merge'
     F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpresponse.rb:275:in `setup_header'
    F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpresponse.rb:205:in `send_response'
    F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:110:in'run'
    F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'


Started GET "/assets/logo.png" for 127.0.0.1 at 2014-03-07 23:40:36 +0530

更改为“real_assets_path

后出错
Started GET "/real_assets/new" for 127.0.0.1 at 2014-03-09 13:56:47 +0530
ActiveRecord::SchemaMigrationLoad(1.0ms)SELECT"schema_migrations".* FROM"schema_migrations"
Processing by AssetsController#new as HTML
Rendered assets/new.html.erb within layouts/application (30.0ms)
Completed 500 Internal Server Error in 98ms

ActionView::Template::Error (undefined method `assets_path' for #<# <Class:0x545bb78>:0x545aeb8>):
1: <div class = "container">
2: <div id = "assetnew">
3:  <%= form_for(@asset) do |f| %>
4:  <center><h1>Add Asset</h1></center>
5:   <div class = "well">
6:     <a class ="red">
  app/views/assets/new.html.erb:3:in`_app_views_assets_new_html_erb___457532114_11608392'


Rendered     

F:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
 Rendered  

F:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
  Rendered  

F:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (18.0ms)

1 个答案:

答案 0 :(得分:1)

<强>资产

这是Rails返回的错误:

ERROR URI::InvalidURIError: the scheme http does not accept registry part: localhost:3000new (or bad hostname?)

在没有看到您的表单的情况下,您似乎遇到了调用资源/assets的问题(如评论中所述)

基本上,Rails将资产管道视为base目录 - localhost:3000/your_asset_file.png - 看起来这就是这里发生的事情

由于您的应用引用了/assets,我猜测它会将其与资产目录混淆。试着这样做:

#config/routes.rb
resources :assets, as: "real_assets", path: "real_assets" #-> real_assets_path - /real_assets

选择

选择问题可能是由assets路径问题

引起的

select_tag是一个独立的帮助方法(不需要任何FormBuilder来帮助它)。这意味着您应该能够在.html.erb文件中使用它而不会出现任何问题

选择的语法看起来没问题,所以我建议如果你修复了你的资产路径,你应该可以使用它