Ruby on Rails ||向用户添加会议

时间:2017-03-19 19:44:35

标签: 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

1 个答案:

答案 0 :(得分:0)

我找到的答案是添加一个嵌套在ConferenceControllerRelationshipController

中的SubscriptionsController

<强> 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