get request to new action using form instead of link causes param is missing or the value is empty error

<%= form_tag new_item_path, :method => :get do %>

<%= select_tag(:item_type, options_for_select(Item::ITEM_TYPES)) %>
<%= submit_tag "New item" %>

Because in form_tag we don’t define scope so all parameters comes in params directry. so you need to fix item_params method

params.permit(:item_type) to permit parameters

your’s item_params method is written to permit this params structure {item: {item_type: q}

What does this do?

if params.fetch(:item, {}).fetch(:item_type, false)

https://apidock.com/ruby/Hash/fetch

fetch tries to get the value from a hash given a key, the second parameter is a fallback in case the key does not exist on the hash (params), in your case, if params[:item] does not exist, it returns {} so the next .fetch won’t fail. now the second .fetch tries to get the value for the key :item_type, if it does not exist y returns “false”. fetch is used when you have optional keys on a hash and you need a fallback in case the key is not present

1 Like