简体   繁体   English

Ruby on Rails-在一个视图中执行多个操作

[英]Ruby on Rails - Perform Multiple Actions in One View

I am currently working on a project called "Gradebook" which will be implemented in Ruby on Rails. 我目前正在开发一个名为“ Gradebook”的项目,该项目将在Ruby on Rails中实现。 In one of the pages in Gradebook, it is required that the instructor will be able to add, remove, or update an assignment. 在成绩簿的其中一个页面中,要求教师能够添加,删除或更新作业。

I'm a beginner with Ruby on Rails, so I went through a couple tutorials and realized that I could simply define "assignment" as a resource in the routes file. 我是Ruby on Rails的初学者,因此我经历了一些教程,并意识到我可以简单地将“ assignment”定义为routes文件中的资源。 The only issue I have with this approach is for each CRUD operation, there is a specific URL associated with it (ex: "/assignment/new" or "/assignment/:id/edit"). 我对这种方法唯一的问题是对于每个CRUD操作,都有一个与之关联的特定URL(例如:“ / assignment / new”或“ / assignment /:id / edit”)。 However in my case, I want to be able to carry out multiple CRUD operations in a single view. 但是,就我而言,我希望能够在单个视图中执行多个CRUD操作。

My idea was to have a single controller action associated with my view. 我的想法是让一个控制器动作与我的视图关联。 The view would contain multiple submit buttons that would be named "Add", "Remove", and "Edit" respectively. 该视图将包含多个提交按钮,分别称为“添加”,“删除”和“编辑”。 The value of each of these submit buttons would be passed to the single controller action, and code would be executed depending on which value was passed. 这些提交按钮中的每个提交按钮的值都将传递到单个控制器操作,并且将根据传递的值执行代码。 For example: 例如:

def my_view_name
    #So all assignments can be viewed in a table       
    @assignments = Assignment.all        

    if params[:commit] == 'Add'
        #Save assignment to database 
    elsif params[:commit] == 'Remove'
        #Remove assignment from database
    elsif params[:commit] == 'Edit'
        #Update assignment in database
    end
end

But would this be the best way to implement the ability to perform multiple CRUD actions in a single view? 但这是否是实现在单个视图中执行多个CRUD动作功能的最佳方法?

Check out this SO question. 看看这个SO问题。

Rails: Multi-submit buttons in one Form Rails:一种形式的多次提交按钮

The answer there links to this railscast. 那里的答案链接到这个轨道。

http://railscasts.com/episodes/38-multibutton-form http://railscasts.com/episodes/38-multibutton-form

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM