我该把“helper”放在哪里? 方法?
在我的 Ruby on Rails 应用程序中,我得到:
class AdminController < ApplicationController
def create
if request.post? and params[:role_data]
parse_role_data(params[:role_data])
end
end
end
而且
module AdminHelper
def parse_role_data(roledata)
...
end
end
还收到一条错误消息,指出 parse_role_data
未定义。 我究竟做错了什么?
In my Ruby on Rails app, I've got:
class AdminController < ApplicationController
def create
if request.post? and params[:role_data]
parse_role_data(params[:role_data])
end
end
end
and also
module AdminHelper
def parse_role_data(roledata)
...
end
end
Yet I get an error saying parse_role_data
is not defined. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
助手主要用于复杂的输出相关任务,例如根据日期列表制作日历的 HTML 表。 与业务规则相关的任何内容(例如解析文件)都应该放入关联的模型中,可能的示例如下:
还要考虑使用(RESTful 路由或 :conditions 选项)[http://api.rubyonrails.org/classes/ActionController/Routing.html] 制作路线时,而不是检查
request.post?
在你的控制器中。Helpers are mostly used for complex output-related tasks, like making a HTML table for calendar out of a list of dates. Anything related to the business rules like parsing a file should go in the associated model, a possible example below:
Also look into using (RESTful routes or the :conditions option)[http://api.rubyonrails.org/classes/ActionController/Routing.html] when making routes, instead of checking for
request.post?
in your controller.您不应该通过 AdminHelper 访问 parse_role_data 吗?
更新 1:检查此项
http://www.johnyerhot.com /2008/01/10/rails-using-helpers-in-you-controller/
Shouldn't you be accessing the parse_role_data through the AdminHelper?
Update 1: check this
http://www.johnyerhot.com/2008/01/10/rails-using-helpers-in-you-controller/
从外观上看,如果您尝试创建一个用于向用户添加角色的 UI。 我假设您已经有一个 UsersController,所以我建议添加一个 Role 模型和 RolesController。 在你的routes.rb中你会做类似的事情:
这将允许你有一个类似的路线:
在你的RolesController中你会做类似的事情:
这将从新操作中显示的表单中获取角色参数数据并创建一个该用户的新角色模型。 希望这对您来说是一个良好的起点。
From the looks of if you're trying to create a UI for adding roles to users. I'm going to assume you have a UsersController already, so I would suggest adding a Role model and a RolesController. In your routes.rb you'd do something like:
This will allow you to have a route like:
In your RolesController you'd do something like:
This will take the role params data from the form displayed in the new action and create a new role model for this user. Hopefully this is a good starting point for you.