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
El vie., 4 oct. 2019 a las 9:14, fugee ohu (<[email protected]>) escribió:
>
>
> On Friday, October 4, 2019 at 6:32:20 AM UTC-4, Avdhesh Sharma (ROR) wrote:
>>
>> 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}
>>
>> On Friday, October 4, 2019 at 1:13:11 PM UTC+5:30, fugee ohu wrote:
>>>
>>> <%= form_tag new_item_path, :method => :get do %>
>>>
>>> <div class="field">
>>> <%= select_tag(:item_type, options_for_select(Item::ITEM_TYPES))
>>> %>
>>> </div>
>>>
>>> <div class="actions">
>>> <%= submit_tag "New item" %>
>>> </div>
>>>
>>> ActionController::ParameterMissing in ItemsController#new param is
>>> missing or the value is empty: item
>>>
>>> In my controller I have the usual filter
>>> def item_params
>>> params.require(:item).permit(:item_type, :title, :item_id,
>>> item_ids: [])
>>> end
>>> ...
>>> Started GET
>>> "/items/new?item_type=General&commit=New+item&utf8=%E2%9C%93" for 127.0.0.1
>>> at 2019-10-04 03:32:18 -0400
>>> Processing by ItemsController#new as HTML
>>> Parameters: {"item_type"=>"General", "commit"=>"New item", "utf8"=>"✓"}
>>> Completed 400 Bad Request in 2ms (ActiveRecord: 0.0ms)
>>>
>>>
>>>
>>> ActionController::ParameterMissing (param is missing or the value is
>>> empty: item):
>>>
>>> app/controllers/items_controller.rb:109:in `item_params'
>>> app/controllers/items_controller.rb:23:in `new'
>>> ...
>>>
>>
> What does this do?
> if params.fetch(:item, {}).fetch(:item_type, false)
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/52be0f6e-0e44-4a9a-aefb-38a3eb7a555d%40googlegroups.com
> <https://groups.google.com/d/msgid/rubyonrails-talk/52be0f6e-0e44-4a9a-aefb-38a3eb7a555d%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/CAPS3bcBhOd0eJUuUThPCfvYDgb3o%3Dxz48k090LMyRZLxsprTXg%40mail.gmail.com.