How to access data sent by user using controller on Ruby on Rails

你离开我真会死。 提交于 2019-12-04 17:41:36

Params

The problem is you're trying to access the params hash in your view

This won't work, as the params are HTTP values sent from a request to your site - only being available on a per-request basis. As your view is essentially another request (kind of like a redirect), the params are not available in there

When you receive the error:

undefined method `[ ]' for nil:NilClass

This basically says that your calling of [] on the params variable is not possible, as this object is nil (not populated with data)

--

Variables

What you'll have to do is set your data in your controller, as per the MVC programming pattern (you should never have to use ActiveRecord methods in your views btw):

#app/controllers/your_controller.rb
Class YourController < ApplicationController
   def action
       @resource_field = @benchmark.find params[:resource][:field]
   end
end

This should make this data available in your view, whilst using the params which have just been passed to Rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!