I have right now two models (There will be more later, therefor I use one Polymorhpic model.

The Polymorhpic table is called Events, and the model has this:

belongs_to :eventable, :polymorphic => true

Then, the Users table, which has many events:

has_many :events, :as => :eventable

The Users table is a Devise table, so when a event is created, i simply want to pass the user id and type to Events:

def create
    event_params[:eventable] = current_user
    @event = Event.new(event_params)
    if @event.save
        redirect_to @event
    else
        render text: @event
    end
end
private
    def event_params
        params.require(:event).permit(:name, :text, :start_date, :end_date, :eventable)
    end

The Event is saved, but i do not get the eventable-fields filled ...

current_user is defined, so thats no problem.

Should i pass params[:eventable_id] and params[:eventable_type] manual?

If so, in which format should eventable_type be, so that it will work when the i get the users events?

有帮助吗?

解决方案

Whenever you call event_params you are building a new hash, you need to keep it somewhere as in:

current_params = event_params
current_params[:eventable] = current_user
@event = Event.new(current_params)
if @event.save
    redirect_to @event
else
    render text: @event
end

其他提示

I'd suggest:

@event = current_user.build_event(event_params)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top