0
我正在创建一个应用程序,您可以在其中查看世界上所有的会议。Ruby on Rails ||向用户添加会议
我有什么:
- 创建会议
- 显示所有会议
现在我想创建是一个按钮,它可以让我的会议添加到用户。
什么我想用这个来完成:
- 加入会议的用户
- 显示加入会议在列表
- 观看会议和添加内容
我在想的一个按钮,用于复制所选对象的属性并将其添加到选定用户以供将来操作和查看会议
我问,如果有人能告诉我如何做到这一点
https://consulegem-salman15.c9users.io/conferences
迁移会议
class CreateConferences < ActiveRecord::Migration[5.0]
def change
create_table :conferences do |t|
t.string :conference
t.string :country
t.string :month
t.string :presence
t.string :audience
t.integer :cost
t.text :content
t.references :user, foreign_key: true
t.timestamps
end
add_index :conferences, [:user_id, :created_at]
end
end
控制器发布会
class ConferencesController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
before_action :admin_user, only: :destroy
def index
@conferences = Conference.paginate(page: params[:page])
if params[:search]
@conferences = Conference.search(params[:search]).order("created_at DESC").paginate(page: params[:page])
else
@conferences = Conference.all.order('created_at DESC').paginate(page: params[:page])
end
end
def new
@user = User.new
@conference = Conference.new
end
def create
@conference = current_user.conferences.build(conference_params)
if @conference.save
flash[:success] = "conference created!"
redirect_to conferences_path
else
@feed_items = current_user.feed.paginate(page: params[:page])
render 'new'
end
end
def destroy
@conference.destroy
flash[:success] = "conference deleted"
redirect_to request.referrer || root_url
end
private
def conference_params
params.require(:conference).permit(:conference,:country , :month, :presence, :audience, :cost ,:picture)
end
# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.admin?
end
def correct_user
@conference = current_user.conferences.find_by(id: params[:id])
redirect_to root_url if @conference.nil?
end
end
模型控制器
class Conference < ApplicationRecord
belongs_to:user
default_scope -> { order(created_at: :desc) }
mount_uploader :picture, PictureUploader
validates :user_id, presence: true
validates :conference, presence: true, length: { maximum: 140 }
validate :picture_size
scope :conference, -> (conference) { where conference: conference }
def self.search(search)
where("conference LIKE ? OR country LIKE ? OR month LIKE ?", "%#{search}%", "%#{search}%", "%#{search}%")
end
private
# Validates the size of an uploaded picture.
def picture_size
if picture.size > 5.megabytes
errors.add(:picture, "should be less than 5MB")
end
end
end