在rails上创建一个站点,用户可以在该站点上加入组并能够与组进行交互(Creating a site on rails where users will be able to join groups

编程入门 行业动态 更新时间:2024-10-22 13:38:23
在rails上创建一个站点,用户可以在该站点上加入组并能够与组进行交互(Creating a site on rails where users will be able to join groups and be able to interact with the group)

我最近开始创建一个网站,用户可以加入群组,并能够与群组互动。 到目前为止,我已经为用户使用了设计,但我现在想知道我用什么来创建用户配置文件甚至组配置文件。 这是我的第一个rails应用程序,我只需要一些关于从哪里开始的指导? 我需要什么工具? 这样做的最佳方式是什么?

I recently started creating a site where users will be able to join groups and be able to interact with the group. So far I have used devise for the users but I'm now wondering what do I use to create user profiles and even the groups profile. This is my first rails application and I just need some guidance on where to go from here? What tools will I need? What is the best way of doing this?

最满意答案

Rails是您需要的唯一工具。 首先,您需要在应用程序中创建其他模型。 根据您的描述,我看到了UserProfile和Group。 Rails的生成器命令将为您存根:

$ rails generate model UserProfile $ rails generate model Group $ rails generate model Membership

现在,您将在app / models目录中拥有user_profile.rb和group.rb,以及db / migrate / create .rb中的迁移。 接下来,您需要通过编辑迁移脚本告诉rails在数据库中创建哪些字段。 您可以自由地在此处包含任何内容,但您至少需要外键来关联您的数据。

def CreateUserProfiles < ActiveRecord::Migration create_table :user_profiles do |t| t.belongs_to :user ...

def CreateMemberships < ActiveRecord::Migration create_table :memberships t.belongs_to :user t.belongs_to :group ...

现在,您可以执行迁移以为您创建数据库表:

$ rake db:migrate

并且您可以使用ActiveRecord关联类方法在代码中定义这些关系,以便Rails将为您处理SQL。

app/models/membership.rb class Membership < ActiveRecord::Base belongs_to :user belongs_to :group end app/models/user.rb class User < ActiveRecord::Base has_one :user_profile has_many :memberships has_many :groups, :through => :memberships ... end app/models/group.rb class Group < ActiveRecord::Base has_many :memberships has_many :users, :through => :memberships end app/models/user_profile.rb class UserProfile < ActiveRecord::Base belongs_to :user end

现在,您拥有了为用户提供配置文件所需的所有工具:

UserProfile.create(:user => User.first, :attr => "value", ...)

或者将用户放入组中:

group = Group.create(:name => "Group 1") group.users << User.first

在他们节省您的时间时使用工具,但学会使用他们首先依赖的工具-Rails。 查看Rails指南 ,它们非常棒。

Rails is the only tool you need. First you'll need to create the other models in your application. From your description I see a UserProfile and a Group. Rails' generator command will stub those out for you:

$ rails generate model UserProfile $ rails generate model Group $ rails generate model Membership

Now you will have user_profile.rb and group.rb in your app/models directory, as well as migrations in db/migrate/create.rb. Next you'll need to tell rails what fields to create in the database by editing the migration script. You are free to include whatever you wish here, but you will at least want foreign keys to relate your data.

def CreateUserProfiles < ActiveRecord::Migration create_table :user_profiles do |t| t.belongs_to :user ...

and

def CreateMemberships < ActiveRecord::Migration create_table :memberships t.belongs_to :user t.belongs_to :group ...

Now you can execute your migrations to create the database tables for you:

$ rake db:migrate

And you can use ActiveRecord association class methods to define those relationships in code, so that Rails will take care of the SQL for you.

app/models/membership.rb class Membership < ActiveRecord::Base belongs_to :user belongs_to :group end app/models/user.rb class User < ActiveRecord::Base has_one :user_profile has_many :memberships has_many :groups, :through => :memberships ... end app/models/group.rb class Group < ActiveRecord::Base has_many :memberships has_many :users, :through => :memberships end app/models/user_profile.rb class UserProfile < ActiveRecord::Base belongs_to :user end

Now you have all the tools in place that you need to give users profiles:

UserProfile.create(:user => User.first, :attr => "value", ...)

Or to put a user in a group:

group = Group.create(:name => "Group 1") group.users << User.first

Use tools when they save you time, but learn to use the tool on which they all depend first–Rails. Check out the Rails Guides, they are excellent.

更多推荐

本文发布于:2023-07-23 00:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225189.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:该站   用户可以   创建一个   点上   站点

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!