Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails-将数据保存到数据库_Ruby On Rails_Ruby_Database_Activerecord - Fatal编程技术网

Ruby on rails Rails-将数据保存到数据库

Ruby on rails Rails-将数据保存到数据库,ruby-on-rails,ruby,database,activerecord,Ruby On Rails,Ruby,Database,Activerecord,我有一个关于将数据保存到数据库的问题。这是初学者的问题。我刚开始学习RubyonRails。我的数据库中有一个用户、医生和预约 class User < ActiveRecord::Base has_many :appointments has_many :doctors, :through => :appointments end class Doctor < ActiveRecord::Base has_many :appointments has_many :users

我有一个关于将数据保存到数据库的问题。这是初学者的问题。我刚开始学习RubyonRails。我的数据库中有一个用户、医生和预约

class User < ActiveRecord::Base
has_many :appointments
has_many :doctors, :through => :appointments
end

class Doctor < ActiveRecord::Base
has_many :appointments
has_many :users, :through => :appointments
end

class Appointment < ActiveRecord::Base
belongs_to :doctor
belongs_to :user
end
class用户:预约
结束
类医生:约会
结束
班级约会
我有以下控制器:

class AppointmentsController < ApplicationController
def create
    @appointment = Appointment.new(appointment_params)
    if @appointment.save
        flash[:success] = "Welcome!"
    else
        render 'new'
    end
end

def destroy
end 
end
类指定控制器
如何通过视图将特定约会保存到数据库?如何从视图访问“约会控制器”中的该方法并设置参数


多谢各位

您听说过一种叫做“更新属性”的东西吗?,这将允许您根据需要更新特定属性。只需从视图中传递包括id在内的参数

然后在控制器中执行以下操作:

user = User.find_by_id(params(:id))
user.appointments.update_attribute(:date, params(:date))

查看详细说明的签出链接。

您可以在视图中使用
@appointment
,如果您正在创建新的约会,那么该方法是新的,即在
约会控制器
中,您应该将一个空约会传递到视图,其中您有
new.html.erb
表单(u)
内容,然后使用create保存它们。

通过视图访问该内容将是一种可怕的做法,请不要那样做。我建议阅读一些关于MVC框架的文档。我知道MVC方法的用途,但我不知道在这里应用它。如果我在新视图中有一个表单,该方法将在我单击“提交”后触发,对吗?在我的例子中,我需要用Javascript填充所有数据,然后以某种方式将其保存到数据库中。我使用完整的日历进行约会。所以,我可以得到点击日期,用户id和医生id。如何将它们保存到数据库?任何帮助都将不胜感激,因为我还有一点时间。