Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用has\u范围gem Rails显示有源滤波器_Ruby On Rails_Ruby_Filter_Ruby On Rails 5_Has Scope - Fatal编程技术网

Ruby on rails 使用has\u范围gem Rails显示有源滤波器

Ruby on rails 使用has\u范围gem Rails显示有源滤波器,ruby-on-rails,ruby,filter,ruby-on-rails-5,has-scope,Ruby On Rails,Ruby,Filter,Ruby On Rails 5,Has Scope,我有一个入门模式,通过入门课程开设了很多课程(通过协会开设了很多课程)。我已经在my entry.rb(model)中设置了我的作用域 使用这个,我可以使用URL进行过滤 这将过滤带有课程id=5的正确条目 所以,我的问题是,在URL中传递参数时,如何在index.html.erb视图顶部显示活动筛选器(课程名称) 模型 class Entry < ApplicationRecord mount_uploader :attachment, AttachmentUploader

我有一个入门模式,通过入门课程开设了很多课程(通过协会开设了很多课程)。我已经在my entry.rb(model)中设置了我的作用域

使用这个,我可以使用URL进行过滤

这将过滤带有
课程id=5的正确条目

所以,我的问题是,在URL中传递参数时,如何在index.html.erb视图顶部显示活动筛选器(课程名称)

模型

class Entry < ApplicationRecord
    mount_uploader :attachment, AttachmentUploader

    has_many :entry_topics
    has_many :topics, through: :entry_topics

    has_many :entry_categories
    has_many :categories, through: :entry_categories

    has_many :entry_courses
    has_many :courses, through: :entry_courses



    belongs_to :user
    scope :by_categories, ->(ids_ary) { joins(entry_categories: :category).where("categories.id" => ids_ary) }
    scope :by_topics, ->(ids_ary) { joins(entry_topics: :topic).where("topics.id" => ids_ary) }
    scope :by_level, -> level { where(:level => level) }
    scope :by_courses, -> (ids_ary) { joins(entry_courses: :course).where("courses.id" => ids_ary) }

end
类条目(ids_-ary){joins(entry_-categories::category)。其中(“categories.id”=>ids_-ary)}
作用域:按主题,->(ids_-ary){joins(entry_-topics::topic)。其中(“topics.id”=>ids_-ary)}
范围:按级别,->level{where(:level=>level)}
范围:按课程,->(ids_-ary){joins(entry_-courses::course)。其中(“courses.id”=>ids_-ary)}
终止
控制器

class EntriesController < ApplicationController
  before_action :set_entry, only: [:show, :edit, :update, :destroy]
  before_action :set_variables, only: [:new, :create, :show, :edit, :update, :index]
  before_action :authenticate_user!, only: [:new, :create, :edit, :update]
  before_action :user_is_current_user, only: [:edit, :update, :destroy]
  has_scope :by_categories, type: :array
  has_scope :by_level, type: :array
  has_scope :by_topics, type: :array
  has_scope :by_courses, type: :array


  # GET /entries
  # GET /entries.json
  def index
 @entries = if params[:term]
    Entry.where('title ILIKE ?', "%#{params[:term]}%")
  else    
    @entries = apply_scopes(Entry.order(:created_at => 'DESC'))
  end

  end



  # GET /entries/1
  # GET /entries/1.json
  def show
  end

  # GET /entries/new
  def new
    @entry = Entry.new(:user => @current_user)
  end

  # GET /entries/1/edit
  def edit

  end

  # POST /entries
  # POST /entries.json
  def create

    @entry = Entry.new(entry_params)
    @entry.user_id = current_user.id

    respond_to do |format|
      if @entry.save

        format.html { redirect_to @entry, notice: 'Entry was successfully created.' }
        format.json { render :show, status: :created, location: @entry }

      else
        format.html { render :new }
        format.json { render json: @entry.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /entries/1
  # PATCH/PUT /entries/1.json
  def update
   user = User.find_by_id(@entry.user_id)
   entry = @entry
    respond_to do |format|
      if @entry.update(entry_params)
        format.html { redirect_to @entry, notice: 'Entry was successfully updated.' }
        format.json { render :show, status: :ok, location: @entry }
      else
        format.html { render :edit }
        format.json { render json: @entry.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /entries/1
  # DELETE /entries/1.json
  def destroy
    @entry.destroy
    respond_to do |format|
      format.html { redirect_to entries_url, notice: 'Entry was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_entry
      @entry = Entry.find(params[:id])

    end
    def set_variables
      @categories = Category.all
      @topics = Topic.all
      @courses = Course.all

    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def entry_params
      params.require(:entry).permit({topic_ids: []}, {category_ids: []}, {course_ids: []}, :description, :category_id, :title, :entry_type,:entry_type_other, :genre, :level, :course,  :attachment, :remove_attachment, :entry_id, :user, :user_id, :id, :url_link, :term)
    end
    def user_is_current_user
    unless current_user == @entry.user or current_user.admin?
      redirect_to(root_url, alert: "Sorry! You can't edit this resource since you didn't create it.") and return
    end
  end
end
class EntriesControllerDESC'))
终止
终止
#获取/entries/1
#GET/entries/1.json
def秀
终止
#获取/输入/新建
def新
@entry=entry.new(:user=>@current\u user)
终止
#获取/条目/1/编辑
定义编辑
终止
#帖子/条目
#POST/entries.json
def创建
@entry=entry.new(entry_参数)
@entry.user\u id=当前用户id
回应待办事项|格式|
if@entry.save
format.html{将_重定向到@entry,注意:'entry已成功创建。}
format.json{render:show,status::created,location:@entry}
其他的
format.html{render:new}
format.json{render json:@entry.errors,status::unprocessable_entity}
终止
终止
终止
#修补程序/放置/条目/1
#PATCH/PUT/entries/1.json
def更新
user=user.find\u by\u id(@entry.user\u id)
entry=@entry
回应待办事项|格式|
if@entry.update(条目参数)
format.html{将_重定向到@entry,注意:'entry已成功更新。}
format.json{render:show,status::ok,location:@entry}
其他的
format.html{render:edit}
format.json{render json:@entry.errors,status::unprocessable_entity}
终止
终止
终止
#删除/条目/1
#删除/entries/1.json
def销毁
@毁灭
回应待办事项|格式|
format.html{redirect_to entries_url,注意:'条目已成功销毁。}
format.json{head:no_content}
终止
终止
私有的
#使用回调在操作之间共享公共设置或约束。
def set_条目
@entry=entry.find(参数[:id])
终止
def set_变量
@categories=Category.all
@topics=Topic.all
@当然
终止
#永远不要相信来自恐怖网络的参数,只允许白名单通过。
def入口参数
参数require(:entry).permit({topic_id:[]}、{category_id:[]}、{course_id:[]}、:description、:category_id、:title、:entry_type、:entry_type、:entry_type_other、:genre、:level、:course、:attachment、:remove_attachment、:entry_id、:user、:user、:user_-id、:url_-link、:term)
终止
def user_是_当前_用户
除非当前用户==@entry.user或当前用户.admin?
将_重定向到(root_url,警告:“抱歉!您无法编辑此资源,因为您没有创建它。”)并返回
终止
终止
终止

在视图中使用
当前作用域将返回
{by\u courses:5}
或类似值的哈希值

如何显示活动筛选器(课程名称)

在应用程序中\u helper.rb

def entries_name
  scope = current_scope.first 
  model = entries_scope_map[scope[0]]
  model.find(scope[1][0]).name
end 

def entries_scope_map
  {by_courses: Course}
end
在您看来(或类似):



是!成功了。但是,是否有任何方法可以显示name属性而不是{:by\u courses=>[“5”]}
current\u scopes.keys.first
Hmm…这不起作用。它现在只是展示而已。哎呀,我误解了你的问题。看到我的编辑我会做什么:)谢谢你的帮助,迄今为止,老兄!我已经尝试了上面的解决方案,但仍然不起作用(只显示“课程名称”而没有其他内容)。为了澄清,每个课程都有一个id(显然)和一个名为:name的字段。传递当前\u作用域成功地显示了ID(因此我知道作用域正在工作!),但我无法获得要显示的相应:name字段而不是ID。
def entries_name
  scope = current_scope.first 
  model = entries_scope_map[scope[0]]
  model.find(scope[1][0]).name
end 

def entries_scope_map
  {by_courses: Course}
end
<%= entries_name %>