Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `permit' for "titleofcoolstuff":String

I am just trying to get two parameters from a view to my controller. I'm using Rails 4.2.x and strong params are killing me.

One param, :query, resolves correctly. However the second param, :location, throws the error in the questions title. I have Googled the issue but everyone's scenario seems to be different and their solutions (relatively) unique.

The view in question is index.html.erb which only contains a simple search form.

<%= form_tag("/searches", action: "create", method: "post") do %>
          <div>Job Title</div>
          <%= text_field_tag(:query) %>
          <div>Location</div>
          <%= text_field_tag(:location) %>
          <%= submit_tag("Go") %>
<% end %>

The controller in question is searches_controller.rb.

class SearchesController < ApplicationController
        def index
                binding.pry
        end

        def show
                binding.pry
        end

        def update
        end

        def create
                @query = search_params["query"].to_s || nil
                @location = search_params[:location].to_s || nil
                binding.pry
        end

        def delete
        end

        private

        def search_params
                params.require(:query).permit(:location)
        end
end

The stack trace points to the search_params method, and shows me that I have the following params in the controller

{ "utf8"=>"✓", "authenticity_token"=>"DEcTwT/NnSY3S3n25zZGXD+KRZcsRkWj9bmN57AMNivFbMXwHF5Vf/psgzSMkZPBa+OWJgafXYGdW+o5KN3xxg==", "query"=>"titleofcoolstuff", "location"=>"milwauke", "commit"=>"Go" }

What am I missing?


2 Answers

Strong parameters is for providing a hash of attributes, for example:

<%= form_for @user do |f| %>
 ## form
<% end %>

This may send parameters like this:

 "user" => { "name"=> "Your Name", "age" => "23", "location" => "USA" }

Strong parameters in this case would be instructing rails to process the users hash of attributes and specifically these attributes, like this:

params.require(:user).permit(:name, :age, :location)

In your case, you are passing in individual parameters (not hashes of attributes), so if you want to grab them, you grab them explicitly:

def create
  @query = params[:query].to_s || nil
  @location = params[:location].to_s || nil
  #do something
end

No need for strong parameters to whitelist model attributes here. Hope this helps.

like image 63
miler350 Avatar answered Aug 24 '26 14:08

miler350


In your case

"query"=>"titleofcoolstuff",
"location"=>"milwauke",
"commit"=>"Go"

since your data are not wrapped with any keys (they are at the root) so you can simply access them using like params[:query].

Whitelisting/Strong params

We need to whitelist params only for mass assignment. like @user.update(user_params) Here, unless the params sent by users in user_params are whitelisted i.e. permitted using .permit method; the update method will throw an exception ActiveModel::ForbiddenAttributes.

In your case since your not updating anything you do not need to create strong params for it.

 def create
   @query = params["query"].to_s || nil
   @location = params[:location].to_s || nil
   binding.pry
  end

If you are gonna do mass assignment in future you have to whitelist your params

For more info see https://cbabhusal.wordpress.com/2015/10/02/rails-strong-params-whilisting-params-implementation-details/

like image 34
illusionist Avatar answered Aug 24 '26 14:08

illusionist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!