Rails 关于自己编写 RedMine 插件过程中,遇到的问题

joke1943 · 2014年11月24日 · 最后由 joke1943 回复于 2014年11月25日 · 3351 次阅读

最近自己学了学 ruby on rails,已经根据 rails 指南的教材编写了一个简单的小程序,现在想把这个东西做成 redmine 的插件。 但是提交表单的时间出了问题,苦恼了很久,网上也找不到相关的资料,求助! 以下是路由的代码:

get 'notes', :to => 'notes#index'
get 'notes/new(.:format)', :to => 'notes#new'
post '/notes(.:format)', :to => 'notes#create'

这里是页面代码:

<%= form_for :note, url: notes_path do |f| %>
  <p>
    <%= f.label :姓名 %><br>
    <%= f.text_field :name %>
  </p>
<p>
    <%= f.label :部门 %><br>
    <%= f.text_field :department %>
  </p>
<p>
    <%= f.label :岗位 %><br>
    <%= f.text_field :position %>
  </p>
<p>
    <%= f.label :请假时间 %><br>
    <%= f.text_field :time %>
  </p>
 <p>
    <%= f.label :请假事由 %><br>
    <%= f.text_area :text %>
  </p>
  <p>
    <%= f.label :主管意见 %><br>
    <%= f.text_field :opinion %>
  </p> 
  <p>
    <%= f.submit:提交 %>
  </p>
<% end %>`

在最后提交表单的时候出了问题

这里是 controller 代码:

class NotesController < ApplicationController 
    def index
        @notes = Note.all
    end
    def new
    end
    def create
        @note = Note.new(note_params)
        @note.save
         redirect_to :action => 'index'
    end
    def show
        @note= Note.find(params[:id])
    end

    private
    def note_params
        params.require(:note).permit(:name,:department,:position,:time,:text,:opinion)
    end
end  

最后是报错信息:

Started POST "/notes" for 127.0.0.1 at 2014-11-24 22:23:56 +0800
Processing by NotesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"u8Q0hqZOgEce4iOX8/vWrR8tWM9qwes7ffisYnXE1vI=", "note"=>{"name"=>"", "department"=>"", "position"=>"", "time"=>"", "text"=>"", "opinion"=>""}, "commit"=>"提交"}
  Current user: anonymous
Completed 500 Internal Server Error in 71.9ms

TypeError (no implicit conversion of Symbol into String):
  activesupport (3.2.19) lib/active_support/dependencies.rb:251:in `require'
  activesupport (3.2.19) lib/active_support/dependencies.rb:251:in `block in require'
  activesupport (3.2.19) lib/active_support/dependencies.rb:236:in `load_dependency'
  activesupport (3.2.19) lib/active_support/dependencies.rb:251:in `require'
  plugins/notes/app/controllers/notes_controller.rb:18:in `note_params'
  plugins/notes/app/controllers/notes_controller.rb:8:in `create'
  actionpack (3.2.19) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (3.2.19) lib/abstract_controller/base.rb:167:in `process_action'
  actionpack (3.2.19) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (3.2.19) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
  activesupport (3.2.19) lib/active_support/callbacks.rb:480:in `_run__542389636927640692__process_action__3294544606366668215__callbacks'
  activesupport (3.2.19) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.19) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
  activesupport (3.2.19) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.19) lib/abstract_controller/callbacks.rb:17:in `process_action'
  actionpack (3.2.19) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (3.2.19) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
  activesupport (3.2.19) lib/active_support/notifications.rb:123:in `block in instrument'
  activesupport (3.2.19) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (3.2.19) lib/active_support/notifications.rb:123:in `instrument'
  actionpack (3.2.19) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
  actionpack (3.2.19) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
  activerecord (3.2.19) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (3.2.19) lib/abstract_controller/base.rb:121:in `process'
  actionpack (3.2.19) lib/abstract_controller/rendering.rb:45:in `process'
  actionpack (3.2.19) lib/action_controller/metal.rb:203:in `dispatch'
  actionpack (3.2.19) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
  actionpack (3.2.19) lib/action_controller/metal.rb:246:in `block in action'
  actionpack (3.2.19) lib/action_dispatch/routing/route_set.rb:73:in `call'
  actionpack (3.2.19) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
  actionpack (3.2.19) lib/action_dispatch/routing/route_set.rb:36:in `call'
  journey (1.0.4) lib/journey/router.rb:68:in `block in call'
  journey (1.0.4) lib/journey/router.rb:56:in `each'
  journey (1.0.4) lib/journey/router.rb:56:in `call'
  actionpack (3.2.19) lib/action_dispatch/routing/route_set.rb:608:in `call'
  rack-openid (1.4.2) lib/rack/openid.rb:98:in `call'
  request_store (1.0.5) lib/request_store/middleware.rb:9:in `call'
  actionpack (3.2.19) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
  rack (1.4.5) lib/rack/etag.rb:23:in `call'
  rack (1.4.5) lib/rack/conditionalget.rb:35:in `call'
  actionpack (3.2.19) lib/action_dispatch/middleware/head.rb:14:in `call'
  actionpack (3.2.19) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
  actionpack (3.2.19) lib/action_dispatch/middleware/flash.rb:242:in `call'
  rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
  rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
  actionpack (3.2.19) lib/action_dispatch/middleware/cookies.rb:341:in `call'
  activerecord (3.2.19) lib/active_record/query_cache.rb:64:in `call'
  activerecord (3.2.19) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
  actionpack (3.2.19) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
  activesupport (3.2.19) lib/active_support/callbacks.rb:405:in `_run__2302799743034094018__call__3164167435205094136__callbacks'
  activesupport (3.2.19) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.19) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
  activesupport (3.2.19) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.19) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (3.2.19) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
  actionpack (3.2.19) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
  actionpack (3.2.19) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.19) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.19) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.19) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.19) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.19) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.19) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.5) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.19) lib/action_dispatch/middleware/static.rb:63:in `call'
  rack-cache (1.2) lib/rack/cache/context.rb:136:in `forward'
  rack-cache (1.2) lib/rack/cache/context.rb:143:in `pass'
  rack-cache (1.2) lib/rack/cache/context.rb:155:in `invalidate'
  rack-cache (1.2) lib/rack/cache/context.rb:71:in `call!'
  rack-cache (1.2) lib/rack/cache/context.rb:51:in `call'
  railties (3.2.19) lib/rails/engine.rb:484:in `call'
  railties (3.2.19) lib/rails/application.rb:231:in `call'
  rack (1.4.5) lib/rack/content_length.rb:14:in `call'
  railties (3.2.19) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
  /usr/local/Cellar/ruby/2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
  /usr/local/Cellar/ruby/2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
  /usr/local/Cellar/ruby/2.1.4/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

求助!为什么提交表单的时候出出这样的错误呢?

麻烦 lz 先加上代码高亮....

#1 楼 @cn_boris 不会调啊,麻烦提示一下了。。。

你可以使用 markdown 语法书写,在该页面按 M 可以查看简单的帮助:

#3 楼 @zlx_star 多谢!能否顺便帮我看一下这个问题吗?多谢!

你提供的信息不容易让人帮你排查,个人提两个建议:

  1. 建议 配合 https://github.com/rweng/pry-rails 帮助你排查问题;
  2. 如果需要更多帮助,最好能提供版本信息和完整的源代码。

从目前你提供的信息看,你使用了 rails 3.2.19, 但是使用了 https://github.com/rails/strong_parameters 的功能,

可以重点看看这里

嘎嘎,楼上分析得对 @note = Note.new(params[:note]) 试试

#5 楼 @zlx_star 多谢!!!我自己用的是 rails 4.1.7,自己搭建的 redmine 是 3.2.19 多谢提醒!

#6 楼 @badboy 十分感谢!提供的这段代码已经完美解决问题了!多谢多谢!!!

#5 楼 @zlx_star 我想顺便问一下,以下这段代码中

 def create
        @note = Note.new(note_params)
        @note.save
         redirect_to :action => 'index'
    end
private
    def note_params
        params.require(:note).permit(:name,:department,:position,:time,:text,:opinion)
    end

note_params 方法根据教程上的说法:

Rails 提供了很多安全防范措施保证程序的安全,你所看到的错误就是因为违反了其中一个措施。这个防范措施叫做“健壮参数”,我们要明确地告知 Rails 哪些参数可在控制器中使用。

链接 http://guides.ruby-china.org/getting_started.html (该教程的 5.6 节中)

在 rails 3 中是怎么样保证类似的安全措施呢?

十分感谢!!!

#6 楼 @badboy 请看 9 楼,十分感谢!!!

rails3 的思路是在 model 里设置 attr_accessible 设置哪些是数据是需要保护和不需要保护的。。。虽然方式也没问题,只是程序员默认情况都很懒,这就成摆设了。。。基本上,所有字段全加上了

需要 登录 后方可回复, 如果你还没有账号请 注册新账号