Ruby on rails 如何在没有表单的rails中保存数据?
我是rails新手,我只是在学习基础知识。 这是我保存数据的代码: app/controllers/employee\u controller.rbRuby on rails 如何在没有表单的rails中保存数据?,ruby-on-rails,ruby,forms,save,Ruby On Rails,Ruby,Forms,Save,我是rails新手,我只是在学习基础知识。 这是我保存数据的代码: app/controllers/employee\u controller.rb class EmployeesController
class EmployeesController
app/views/employees/new.html.erb
名字
中间名
姓氏
联系电话
地址
用户名
密码
但是,我的目标是在不使用html表单的情况下立即保存。(无输入)例如,当我访问某个URL时,值会自动保存在数据库中首先,我想在每个字段中指定一个常量值,看看它是如何工作的 例如
- fname='sample name'
- mname='sampleMidName'
- lname='sampleLastName' 等等
访问某个URL/站点后,如何立即分配这些值。在控制器中,尝试以下操作:
class EmployeesController < ApplicationController
def custom
@employee = Employee.create(fname: "sample name")
end
end
在浏览器中输入正确的url时,如:
localhost:3000/custom
应保存员工
祝你好运 在控制器中尝试以下操作:
class EmployeesController < ApplicationController
def custom
@employee = Employee.create(fname: "sample name")
end
end
在浏览器中输入正确的url时,如:
localhost:3000/custom
应保存员工
祝你好运 在控制器中尝试以下操作:
class EmployeesController < ApplicationController
def custom
@employee = Employee.create(fname: "sample name")
end
end
在浏览器中输入正确的url时,如:
localhost:3000/custom
应保存员工
祝你好运 在控制器中尝试以下操作:
class EmployeesController < ApplicationController
def custom
@employee = Employee.create(fname: "sample name")
end
end
在浏览器中输入正确的url时,如:
localhost:3000/custom
应保存员工
祝你好运 首先要向控制器添加一个方法
def update_fname
# get the parameters
fname = params[:fname]
# get the employee ID
id = params[:id]
# find the employee
@employee = Employee.find(id)
# update the employee
employee.update_attributes(fname: fname)
redirect_to @employee
end
然后,在路线中添加:
resources :employees do
get 'update_fname'
end
你呼叫路线,谁应该是
http://localhost:3000/employees/{:id}/update\u fname?fname={your\u fname}
首先向控制器添加一个方法
def update_fname
# get the parameters
fname = params[:fname]
# get the employee ID
id = params[:id]
# find the employee
@employee = Employee.find(id)
# update the employee
employee.update_attributes(fname: fname)
redirect_to @employee
end
然后,在路线中添加:
resources :employees do
get 'update_fname'
end
你呼叫路线,谁应该是
http://localhost:3000/employees/{:id}/update\u fname?fname={your\u fname}
首先向控制器添加一个方法
def update_fname
# get the parameters
fname = params[:fname]
# get the employee ID
id = params[:id]
# find the employee
@employee = Employee.find(id)
# update the employee
employee.update_attributes(fname: fname)
redirect_to @employee
end
然后,在路线中添加:
resources :employees do
get 'update_fname'
end
你呼叫路线,谁应该是
http://localhost:3000/employees/{:id}/update\u fname?fname={your\u fname}
首先向控制器添加一个方法
def update_fname
# get the parameters
fname = params[:fname]
# get the employee ID
id = params[:id]
# find the employee
@employee = Employee.find(id)
# update the employee
employee.update_attributes(fname: fname)
redirect_to @employee
end
然后,在路线中添加:
resources :employees do
get 'update_fname'
end
你呼叫路线,谁应该是
http://localhost:3000/employees/{:id}/update\u fname?fname={your\u fname}
如果我要添加新记录怎么办?如何获得参数链接?您只需在控制器中添加一个新函数,如create\u employee
。如果我要添加新记录怎么办?如何获得参数链接?您只需在控制器中添加一个新函数,如create\u employee
。如果我要添加新记录怎么办?如何获得参数链接?您只需在控制器中添加一个新函数,如create\u employee
。如果我要添加新记录怎么办?如何获得参数链接?您只需在控制器中添加一个新函数,例如create\u employee
。