0
votes

Here is what my params look like,

{
:project_id => "1",
:ticket => {:name => "Ticket1"}
}

How do I add the project_id to the permitted params?

Controller

params.require(:ticket).permit(:name)

3

3 Answers

1
votes

Here is what I did
I added another function for project params

   def ticket_group_params
      params.require(:ticket_group).permit(:name)      
   end

   def project_params
      params.permit(:project_id)
   end

Then I just merge the results only for the create action eg.

  # POST /ticket_group
  # POST /ticket_group.json
  def create
    @ticket_group = TicketGroup.new(ticket_group_params.merge(project_params))
    respond_to do |format|
1
votes
def ticket_params
 name_params = params.require(:ticket).permit(:name)
 name_params.merge(params.permit(:project_id))
end
# => {name: 'val', project_id: 'val'}

or

def ticket_params
  params.require(:ticket).permit(:name).tap do |_params|
    _params[:project_id] = params.permit(:project_id)
  end
end
# => {name: 'val', project_id: 'val'}

or, it may be

def ticket_params
  params.permit(:project_id, ticket: [:name])
end
# => {project_id: 'val', ticket: {name: 'val'}}
0
votes

You likely need to add this as a hidden attribute on your form, like so:

<input type="hidden" name="ticket[project_id]" value="<%= @ticket.project_id %>" />

Then in the controller you can do this:

params.require(:ticket).permit(:name,:project_id)

Here is a helpful link: https://github.com/rails/strong_parameters