从视图链接到控制器功能
我有一个函数可以获取更新数据库以更新表行中的用户名的作业的所有权。我想从视图链接到该函数,然后重定向到适当的页面。
如何从视图链接到控制器函数或模型函数?
从索引中,我想在显示、编辑、删除旁边有另一个链接,上面写着“取得所有权” 然后,这将在应用程序控制器中触发一个操作
def accept_job(job_type, id, username)
if (job_type == 'decom')
Decommission.update(id, :username => username)
else
end
end
I have a function to take ownership of a job which updates the database to update the username in a table row. I want to link to this function from the view and then redirect to the appropriate page.
How do you link to a controller function or a model function from the view?
from the index i want to have another link beside show, edit, delete, which says 'take ownership'
This will then fire off an action in the application controller
def accept_job(job_type, id, username)
if (job_type == 'decom')
Decommission.update(id, :username => username)
else
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用实例变量@controller来获取对控制器的引用。至于调用模型函数,您可以调用
Model.function
来调用类方法,或者如果您有一个名为model_instance
的特定Model
实例,然后使用 model_instance.function 来调用实例方法。编辑:好的,我想我现在明白你在问什么了。
您应该
在控制器中创建一个新操作,我们将其命名为
update_username
:将您的操作添加到routes.rb 中的路由中。有关更多详细信息,请参阅从外向内的 Rails 路由。
在视图中添加您的链接:
You can use the instance variable
@controller
to get a reference to the controller. As for calling a model function, you can callModel.function
to call class methods, or if you have a particularModel
instance calledmodel_instance
, then usemodel_instance.function
to call an instance method.Edit: Okay, I think I understand what you're asking now.
You should
Create a new action in the controller, let's call it
update_username
:Add your action the routes in routes.rb. See Rails Routing from the Outside In for more details.
Add your link in the view:
首先,在模型中创建一个函数,例如
class Decommission
def allocate_permission(name)
#你的更新代码
1
-
正如我所看到的,您可以通过 3 种不同的方式来执行此操作
创建一个帮助程序方法来更新权限(这可以在应用程序帮助程序或与您的视图相关的帮助程序中完成)
2 - 通过创建一个控制器方法(如但如果您不在其他视图中使用此方法,则无需在应用程序控制器中创建此方法
3 - 如果您想在控制器和视图中使用您的方法,请在应用程序控制器中创建您的方法并将其作为帮助器方法。通过这种方式,您可以从控制器访问它以及视图
欢呼
sameera
First you create a function in your model, say
class Decommission
def assign_permission(name)
#your update code
end
end
As I can see, you can do this in 3 different ways
1 - Create a helper method to update the permission (This can be done either in Application helper or helper related to your view)
2 - By creating a controller method (as you proposed) But if you are not using this method in other views you dont need to create this method in application controller
3 - If you want to use your method in both controllers and views, create your method in application controller and make it as helper method. By that way you can access it from controllers as well as views
cheers
sameera