Rails Fullcalendar:为源url添加自定义参数 [英] Rails Fullcalendar: add custom params to source url

查看:99
本文介绍了Rails Fullcalendar:为源url添加自定义参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Rails中运行的Fullcalendar。我还为事件表和模式添加了额外的字段以获取用户输入。保存记录时,它会自动将当前用户的user_id保存到事件表中。



我希望只显示current_user事件的日历。 / p>

现在,calendar.js.coffee文件具有获取事件数据的功能:

  eventSources:[{
url:'/ events',
}],

我如何解决这个问题,以便只显示current_user的事件数据?
我是否在事件控制器中放置了一些代码?或者在咖啡代码中有什么?



我应该在控制器的json部分添加一些选择代码吗?

  class EventsController< ApplicationController 
#GET / events
#GET /events.json
def index
@events = Event.scoped
@events = Event.between(params ['start '],params ['end'])if(params ['start']&& params ['end'])
respond_to do | format |
format.html#index.html.erb
format.json {render:json => @events}
end
end

以下内容似乎不起作用:

  format.json {render:json => @events,:user_id => current_user.id} 

谢谢!!!

你使用范围(我将它命名为 for_user ,我可以发布关于这个范围的代码

  def索引
@events =事件(Event)模型中的事件,以检索特定用户的事件。 .scoped
@events = @ events.between(params ['start'],params ['end'])if(params ['start']&& params ['end'])
@events = @ events.for_user(params [:user_id])if params [:user_id] .present?
respond_to do | format |
format.html#index.html.erb
format.json {render:json => @events}
end
end

现在您需要传递一个 params [:user_id] 添加到索引操作中,您的情况可能是 current_user.id ;)

  eventSources:[{
url:'/ events',
data:{user_id:current_user.id}
}],

警告:安全问题



这样做可让您能够将任何user_id放入URL中并查看其他用户的数据。在index操作上添加验证,以确保您只能访问current_user的数据:

  @events = @ events.for_user( params [:user_id])if params [:user_id] .present? &安培;&安培; params [:user_id] == current_user.id 


I've got a Fullcalendar working in Rails. I've also added extra fields to the events table and a modal to get user input. When a record is saved, it automatically saves the user_id of the current user in the event table.

I would like to have the calendar display with only the current_user's events.

Right now the calendar.js.coffee file has this for getting the events data:

   eventSources: [{
  url: '/events',
}],

How can I fix it so only the current_user's event data shows up? Do I put some code in the events controller? Or something in the coffee code?

Should I add some selection code in the json part of the controller?

  class EventsController < ApplicationController
  # GET /events
  # GET /events.json
  def index
    @events = Event.scoped
    @events = Event.between(params['start'], params['end']) if  (params['start'] && params['end'])
   respond_to do |format|
  format.html # index.html.erb
  format.json { render :json => @events }
  end
end

The following doesn't seem to work:

      format.json { render :json => @events, :user_id => current_user.id }

Thanks!!!

解决方案

You are on the right way ;) Use a scope (I named it for_user, I can post the code about this scope if needed) in the Event model to retrieve the events of a particular user.

def index
  @events = Event.scoped
  @events = @events.between(params['start'], params['end']) if  (params['start'] && params['end'])
  @events = @events.for_user(params[:user_id]) if params[:user_id].present?
  respond_to do |format|
    format.html # index.html.erb
    format.json { render :json => @events }
  end
end

Now you need to pass a params[:user_id] to the index action, which could be current_user.id in your case ;)

eventSources: [{
  url: '/events',
  data: { user_id: current_user.id }
}],

WARNING: Security issue

Doing this will let you be able to put any user_id in the URL and see the data of other users. Add a verification on the index action to ensure you can only access to the current_user's data:

@events = @events.for_user(params[:user_id]) if params[:user_id].present? && params[:user_id] == current_user.id

这篇关于Rails Fullcalendar:为源url添加自定义参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆