简体   繁体   中英

Creating new model instance with paramethers (Rails)

I need to create new Event from User's guest page and the Event.visitor_id should be User.id

Event.rb

def create
@event = current_user.owner_events.new(event_params)
end

protected

def event_params
params.require(:event).permit(:visitor_id)
end

I need to correct the view below which is not working:

<%= link_to "Create event with this user", events_path(visitor_id: @user.id), method: :post %>

I get: ActionController::ParameterMissing in EventsController#create

Try that (you forgot to add event key):

<%= link_to "Create event with this user", events_path(event: { visitor_id: @user.id }), method: :post %>

For more details read 'strong parameters' gem documentation .

我只需要将.to_i添加到@ user.id

<%= link_to "Create event", events_path(:event =>{:visitor_id => @user.id.to_i} ), :method => :post %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM