-
-
Notifications
You must be signed in to change notification settings - Fork 795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unchecked checkbox in Rails 4: undefined method `name' for nil:NilClass #400
Comments
Hi @exchgr, for handling boolean results in searches, I've found I usually need three states: In that case, assuming your checkbox <%= f.select :field_not_eq, [['True', 0], ['False', 1], ['All', 2]] %> Hope this is helpful. |
I just tried that, and I still got the same error. |
Hm. I use that code in production. Which version of Ransack/Ruby/Rails are you using? More information to reproduce the issue would be helpful. |
Ransack: 1.2.3 (Rails 4 branch) |
Same problem here but it happens on any field |
Same problem. search_form -> attribute_fields -> attribute_select works, but i can't seem to create any form field from search_form . Gives me the same error. |
Thanks for the input. The non-master branches are not maintained. At the time they were for testing updating Ransack for compatibility with not-yet-released versions of Rails. It's better to use the latest release or master. Please chime in if you're having this issue using one of those, with all the info needed to reproduce 👍 |
Unfortunately I meant to say I was getting this with the current version as well. Some code # app/controller/leads_controller.php
def index
leads
@search = @leads.ransack(params[:q])
@search.sorts = 'requested_at desc' if @search.sorts.empty?
@leads = @search.result.page(params[:page]).per(30)
@search.build_condition if @search.conditions.empty?
@leads = LeadDecorator.decorate_collection(@leads)
end
private
def leads
@leads = current_organization.leads
end # app/views/leads/index.html.haml
= render partial: "shared/dynamic_search_form", locals: { s: @search } # app/views/shared/_dynamic_search_form.html.haml
= search_form_for s, url: search_leads_path, method: :post do |f|
[...]
= f.search_field :name_first_cont Full trace
# config/initializers/ransack.rb
Ransack::Adapters::ActiveRecord::Base.class_eval('remove_method :search') ransack-1.7.0 Much obliged! |
Just pulled master, same result :( |
Hm... it almost looks like the different posters are possibly having different issues... and yet no one has provided enough info to reproduce their issue. I suspect your issue involves your models or associations, but you didn't show them. I'm not sure the issue is with Ransack but can't tell from what you've provided (please see the Contributing Guide). What would be helpful would be a self-contained gist like this one or this one, or if you don't like writing test assertions, then perhaps this one 😉 |
Yep. I have many gems loaded. I'm suspecting the same. I got some From the start I have been using a complex initializer |
The top of that stack trace looks familiar. I have not been able to reproduce my issue outside of a Rails view, so I put up a minimal rails app on github that demonstrates my issue. See https://github.com/driehuis/ransack-issue400. Obviously doing something wrong, but for the life of me can't figure it out. Needless to say, my app is usable without the dropdown select, but getting it to work aould be way cool. |
I'm having a similar issue with search_form_for. In the controller when I create the search object in the index view, it looks like this:
I would expect this to be the case on the initial load of the index view since I'm not doing any searching at this point but as you can see, in the conditions there is no predicate like there is when you create a search object on the command line, like so:
It's failing in this method when you reference predicate.name:
from: https://github.com/activerecord-hackery/ransack/blob/master/lib/ransack/nodes/condition.rb Should predicate.name be replaced with predicate_name where you check for the existence of predicate?
Edit to add more info: Calling
Calling
|
If it helps anyone else, my problem referenced in the comment above was that build_grouping and build_condition were being called when the params were nil. Once I added a check to make sure they weren't called unless some params were present my issue was resolved. |
Thanks @bplexico, your solution fixed my problem! |
Issues closed as resolved. |
Inside a
search_form_for
, I havef.check_box :some_setting_true
. The checkbox seems to influence searches correctly, but when it's unchecked, it returns:After some digging, the issue seems to be here: https://github.com/activerecord-hackery/ransack/blob/master/lib/ransack/nodes/condition.rb#L146, and occurs due to
Condition
'sattributes
beingnil
when the checkbox is unchecked.The text was updated successfully, but these errors were encountered: