gpt4 book ai didi

ruby-on-rails - rails : Connecting one model to another through forms

转载 作者:太空宇宙 更新时间:2023-11-03 16:34:56 25 4
gpt4 key购买 nike

我目前遇到了将一个模型连接到另一个模型的问题。基本上我希望用户(用户模型)能够通过表格选择特定学校(学校模型),并且用户将在该特定学校下,直到他们更改它。我不知道该怎么做,任何帮助将不胜感激!谢谢!

用户模型

class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :biography, :avatar
has_secure_password
belongs_to :school, foreign_key: "school_id"
end

我没有为用户模型中的任何学校定义任何东西,因为我不确定要放什么。

学校模型

class School < ActiveRecord::Base
attr_accessible :name, :school_id

has_many :users

validates :school_id, presence: true

end

用户 Controller

def create
@user = User.new(params[:user])
if @user.save
redirect_to @user
else
redirect_to current_school
end
end

def update
@user = current_user
if @user.update_attributes(params[:user])
flash[:notice] = "Successfully updated profile."
redirect_to home_path
else
redirect_to current_school
end
end

学校管理员

def create
school = School.find(params[:name])
if school
session[:school_id] = school.id
redirect_to school_path(school)
end
end

def show
@school = School.find(params[:id])
@user = User.new
end

校内用户表格

<%= simple_form_for @user do |f| %>
<%= f.label :Name %></br>
<%= f.text_field :name, :class => 'modal_settingfield' %>
</br></br>
<%= f.label :Email %></br>
<%= f.text_field :email, :class => 'modal_settingfield' %>
</br></br>
<%= f.label :Password %> <span class='faded'>(Leave blank to not change)</span>
<%= f.password_field :password, :class => 'modal_settingfield'%>
</br></br>
<%= f.label :Password_Confirmation %> <span class='faded'>(Leave blank to not change)</span>
<%= f.password_field :password_confirmation, :class => 'modal_settingfield'%>
</br></br>
<%= f.label :School_id %> <span class='faded'>(Leave blank to not change)</span>
<%= f.association :school, collection: @schools, label_method: :name, value_method: :id, prompt: "Select a school", :label => false, :required => false %>
<%= f.label :Biography %> <span class='faded'>(Please limit Biography to <span class='TextCounter'></span>letters)</span></br>
<%= f.text_area :biography, :class => 'modal_settingfield'%>
</br></br>
<%= f.label :Photo %> <span class='faded'>(Add a Profile Picture)</span></br>
<%= f.file_field :avatar %>
<%= f.submit 'Update', :class => 'update_button' %>
<% end %>

就读学校

def current_school
@current_school ||= School.find(session[:school_id]) if session[:school_id]
end
helper_method :current_school

session Controller (这是登录代码)

def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
if params[:remember_me]
cookies.permanent[:auth_token] = user.auth_token
else
cookies[:auth_token] = user.auth_token
end
redirect_to home_path
else
flash.now.alert = "Invalid email or password"
redirect_to current_school
end
end

def destroy
cookies.delete(:auth_token)
redirect_to current_school
end

最佳答案

我建议 simple_form gem对于这种类型的东西,通常是为了更简单、更清晰、更简洁的形式。请参阅集合部分。

你会有这样的代码:

<%= simple_form_for @user do |f| %>
<%= f.input :name %>
<%= f.input :school_id, collection: @schools, label_method: :name, value_method: :id, prompt: "Select a school" %>
<%= f.button :submit %>
<% end %>

这假设 @schools变量由呈现此表单的 Controller 操作设置。我假设这些是 School对象,具有 :name:id方法。

此外,您的 School模型可能不应该有 school_id作为attr_accessible , 我想你想要这个 User模型,以及验证规则。如果您遵循常规命名,也不需要显式设置外键,即:

class User < ActiveRecord::Base
attr_accessible :school_id, :name, :email, :password, :password_confirmation, :biography, :avatar
has_secure_password
belongs_to :school

validates :school, presence: true
end

关于ruby-on-rails - rails : Connecting one model to another through forms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9284613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com