2017-03-19 89 views
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 

回答

0

我发现回答是通过添加嵌套在ConferenceController一个SubscriptionsController和RelationshipController

SubscriptionController

class SubscriptionsController < ApplicationController 
    before_action :set_conference, only: :create 

    def index 
    @subscriptions = current_user.subscriptions 
    @subscriptions = Subscription.paginate(page: params[:page]) 
    end 

    def show 
    @subscription = Subscription.find_by(id: params[:id]) 

    end 

    def create 
    if current_user.subscriptions.create(conference: @conference) 
     flash[:success] = "You are now subscribed to { @conference.conference }" 
    else 
     flash[:error] = "Could not create subscription." 
    end 
    redirect_to conferences_path 
    end 

    def destroy 
    @subscription = current_user.subscriptions.find(params[:id]) 
    if @subscription.destroy 
     flash[:success] = "You are no longer subscribed to { @conference.conference }" 
    else 
     flash[:error] = "Oh noes" 
    end 

    redirect_to conferences_path 
    end 

    def set_conference 
    @conference = Conference.find_by id: params["conference_id"] 
    end 
end 

RelationshipController

class RelationshipsController < ApplicationController 
    before_action :logged_in_user 

    def create 
    @conference = Conference.find(params[:followed_id]) 
    current_user.follow(@conference) 
    respond_to do |format| 
     format.html { redirect_to @conference } 
     format.js 
    end 
    end 

    def destroy 
    @user = Relationship.find(params[:id]).followed 
    current_user.unfollow(@user) 
    respond_to do |format| 
     format.html { redirect_to @user } 
     format.js 
    end 
    end 
end 

ConferenceController

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 show 

     @conference = Conference.find(params[:id]) 

    end 

    def new 
    @user = User.new 
    @conference = Conference.new 
    end 

    def create 

    @conference = Conference.new(conference_params) 
    @conference.user = current_user 
    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