我最近开始创建一个网站,用户可以加入群组,并能够与群组互动。 到目前为止,我已经为用户使用了设计,但我现在想知道我用什么来创建用户配置文件甚至组配置文件。 这是我的第一个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 MembershipNow 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:migrateAnd 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 endNow 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.firstUse 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.
更多推荐
发布评论