Search code examples
ruby-on-railsrubycontrollerhamlhidden-field

How to access a hidden field in form in the params value but it says unexpected line end


I have a hidden field that I want to permit to my proposal controller.

When I submit the form it says

unexpected line end for proposal_params

Am I missing something here?

_form.html.haml:

%fieldset.col-md-12 = form.label t('.select_tags'), { class: 'tags-label' }
  = form.hidden_field :tags, { class: 'hidden_tags' }

controller:

def proposal_params 
  params.require(:proposal).permit(:title, :description, :target_audience,
                                   :details, :pitch, :difficulty, :track_id, 
                                   :main_tag_id, comments_attributes: %i[body proposal_id person_id], 
                                   speakers_attributes: %i[person_id id], :tags) 
end

Although I am able to access the value by doing params[:proposal][:tags].


Solution

  • In Ruby when calling methods that take positional and keyword arguments you MUST place the keyword arguments last:

    def foo(*args, **kwargs); end
    foo(1, 2, 3, bar: 'baz')
    foo(bar: 'baz', 1, 2, 3) # syntax error
    
    params.require(:proposal).permit(:title, :description, :target_audience,
                                       :details, :pitch, :difficulty, :track_id, 
                                       :main_tag_id, :tags,
                                       comments_attributes: %i[body proposal_id person_id], 
                                       speakers_attributes: %i[person_id id])