Rails: show only specific objects inside a form select field - ruby-on-rails

I have a form which I'm creating tokens for users. First I find all the users #users = User.order('name ASC') inside a controller. Then inside the create token form I have a select field from which I'm trying to show only the users that don't have a token:
<% #users.each do |user| %>
<% if user.token.blank? %>
<%= f.input :user_id, collection: user, label: "Associate with", value_method: :id, :include_blank => "Select a user" %>
<% end %>
<% end %>
But for some reason I'm getting this error:
undefined method `to_a' for #<User:0x00007f95a54086b0>
Did you mean? to_s
Any idea how I can fix this?

UPDATE.
Just to clarify stuff. You get this error because you pass one single user as a collection to the input. And expecting collection it will try to convert whatever you passed to array. But it won't be able to properly convert a single user to array. So you should have passed a collection of users instead. Like this:
<%= f.input :user_id, collection: #users, label: "Associate with", value_method: :id, :include_blank => "Select a user" %>
Of course, doing this you will need to have the users without tokens already assigned to your #users variable.
Or as I understand your problem, I can offer an alternative:
So assuming users without a token have token column set to nil you can do something like this:
<%= f.select :users, options_for_select(#users.where(token: nil).pluck(:name, :id)), include_blank: true %>
This will eliminate your need to iterate #users explicitly and to create an input for each of them i.e. the code above should replace this completely
<% #users.each do |user| %>
<% if user.token.blank? %>
<%= f.input :user_id, collection: user, label: "Associate with", value_method: :id, :include_blank => "Select a user" %>
<% end %>
<% end %>
Of course feel free to adjust that select helper as you need.
If you do not need #users with tokens anywhere in the current controller action or form, you should consider assigning #users = User.where(token: nil) right in your controller action.

#users should already contain the users who do not have a token to do so:
<%= f.input :user_id, collection: #users, label: "Associate with", value_method: :id, :include_blank => "Select a user" %>

Related

rails forms updating a db record

I would like to rewrite a form which is used to update a record on a database.
I want to update the form so that the form input does not show the record, as
the record is outputted by the line
<%= q.object.content %>.
I want the
form input not to display the record, and I want that the record is updated
when the input field is edited, and is not edited when it is left blank.
I am new at working with forms and don't know the best way to achieve this.
Can anyone provide any help on achieving this ? Below is the current form. Any help would be appreciated.
<%= semantic_form_for #bunchOfThings do |f| %>
<%= f.inputs do %>
<%= f.semantic_fields_for :aThing, #aThing do |q| %>
<%= q.object.content %>
<%= q.input :content, label: "A Thing: #{q.object.content}" %>
<% end %>
<% end %>
<%= f.action :submit , label: t('Some Text'), button_html: { class: 'btn btn-primary' } %>
<% end %>
You can manually set the default value of a field to an empty string by changing this line:
<%= q.input :content, label: "A Thing: #{q.object.content}" %>
To this:
<%= q.input :content, label: "A Thing: #{q.object.content}", input_html: {value:''} %>
You would also need to filter out blank fields on the backend within the update controller method. Something like this:
def update
filtered_params = permitted_record_params
filtered_params.keep_if{|k,v| !v.blank? }
record.update(filtered_params)
...
end
Where of course the permitted_record_params method returns your permitted params hash.

I want to change the role field in devise sign up dynamically

In my user model i add
ROLES = Role.all.collect(&:name)
In my view part of sign up in
<% if can? :manage, #users %>
<p><%= f.label :role %>: <%= f.collection_select :role, User::ROLES, :to_s, :humanize, {:include_blank => true} %></p>
<% end %>
The problem is ROLES = Role.all.collect(&:name) is not updated when i add and edit . For example i add new role user then i have to restart the server to see the changes in the role inside the signup field. How can i update the changes in role without restart the server?
Well, the simple solution
<% if can? :manage, #users %>
<p><%= f.label :role %>: <%= f.collection_select :role, Role.all.map(&:name), :to_s, :humanize, {:include_blank => true} %></p>
<% end %>
You can also place this in a helper, or let the controller handle the retrieving of the roles (e.g. set a instance variable #roles).
So in short: do not use a constant :)
Use a class method instead of a constant:
def self.roles
Role.all.collect(&:name)
end
Of course, this is provided for free if user has many roles.

undefined method `name' for ["Yes", true]:Array

I acctual don't know where Array ["Yes",true] came from. I have two models like this:
GeneralExam has_many topic_questions
TopicQuestion belongs to general_exam, belongs_to topic
In TopicQuestion, I have columns: general_exam_id, topic_id, number_question, to store a number question for each topic in general exam.
I built a nested form for create new general exam, on form I built a dynamic select, when user choose a course from a dropdown list, I have other dropdown lists to display topics of course chosen by user.
This is my javascript code for dynamic select:
<% content_for :head do %>
<script type="text/javascript">
$(document).ready(function() {
$('#general_exam_course_id').change(function () {
$.ajax({
type: "POST",
url: "<%= update_topics_general_exams_path %>",
data: { course_id: $('#general_exam_course_id').val() },
dataType: "script"
});
});
</script>
<% end %>
#general_exam_course_id is ID of dropdown list for select course. update_topics_general_exams_path is route to my update action in general exam controller, this is my update action:
def update_topics
#course = Course.find(params[:course_id])
#topics = #course.topics
end
I have a update_topics.js.erb also:
$('div#topic_questions select').html("<%= j options_from_collection_for_select(#topics, 'id', 'name') %>");
Dropdown lists in div#topic_questions will get #topics value from update_topics action (I think so) and display it. When I create new general exam, it's ok. But when I press Edit link, it show me an error which I don't know why and where it comes:
NoMethodError in General_exams#edit
undefined method `name' for ["Yes", true]:Array
Extracted source (around line #3):
1: <div class="row">
2: <div class="span6"><%= remove_child_link "Remove below topic", f %></div><br><br>
3: <div class="span3"><%= f.association :topic, collection: #topics, label_method: :name, value_method: :id, prompt: "Choose a topic", label: false %></div>
4: <%= f.input :number_question, placeholder: 'Number of questions', label: false, style: 'display: inline' %>
5: </div>
My edit action only have: #general_exam = GeneralExam.find(params[:id]). I don't know why topic association field of general exam display an Array ["Yes", true]. I don't have it, don't define it any where, why it come when I edit general exam?
Update
If I remove label_method: :name, value_method: :id in:
<%= f.association :topic, collection: #topics, label_method: :name, value_method: :id, prompt: "Choose a topic", label: false %>
The page is not error, but Dropdown lists have value Yes, No in select, not the name of topic I have created for exam. Form can get the number question of each topic, but it can not get name of topic.
When I create:
When I edit:
I found this in config/locales/simple_form.en.yml:
en:
simple_form:
"yes": 'Yes'
"no": 'No'
Is this a problem?
Update: Don't pass #topics in new action but it's still have on new page When course even is not selected yet
My new action:
def new
#general_exam = GeneralExam.new
5.times { #general_exam.topic_questions.build }
##topics = Topic.all
end
As in my extract source above, I have below code in new, edit too (use same form):
<%= f.association :topic, collection: #topics, label_method: :name, value_method: :id, prompt: "Choose a topic", label: false %>
So I don't pass #topics, but when I go to new page, dropdown lists for topic still display Yes, No value.
Ok , our time for comments chat-like is over :)(the System suggested to move to the chat room ) . The solution it seems to be passing the collection inline in the association of the simple_form :
<%= f.association :topic, collection: #topics, label_method: :name, value_method: :id, prompt: "Choose a topic", label: false %>
becomes :
<%= f.association :topic, collection: Topic.limit(1), prompt: "Choose a topic", label: false %>
It would work for now , I think .
You've got the right idea by assigning your query to #topics and passing it to your view... but, you're putting it in the wrong actions and not passing them to the right views:
def update_topics
#course = Course.find(params[:course_id])
#topics = #course.topics
end
-
What you should do:
There is NO update view for your update action, so, you must place the following 2 lines:
#course = Course.find(params[:course_id])
#topics = #course.topics
in both your new action and your edit action
Then you can go ahead and use your original simple_form code, which IS/WAS ORIGINALLY correct:
<%= f.association :topic, collection: #topics,
label_method: :name, value_method: :id,
prompt: "Choose a topic", label: false
%>
Alternatively, you could SKIP the #topics in your controllers and simply put the following in your views:
new view
<%= f.association :topic, collection: Topics.all,
label_method: :name, value_method: :id,
prompt: "Choose a topic", label: false
%>
edit view
<%= f.association :topic, collection: Course.find(params[:course_id]).topics,
label_method: :name, value_method: :id,
prompt: "Choose a topic", label: false
%>
NOTE the difference:
I would think that in your new view you would want the user to see ALL possible topics.
While in your edit view you just want them to see the topics for this course, correct?
-
Why it happened:
Since, you were not passing ANY collection earlier, there was no 'name' method/column for simple_form to use as the labels/display values in the dropdown and thus the error message you got regarding Array ["Yes",true]
If/when you removed the label_method: :name, value_method: :id, options you probably would have seen as many choices as you had expected rows in the dropdown, but instead of a list of the course topic names you would have seen a list of Topic objects (something like this):
#<Topic::xxxx>
#<Topic::xxxx>
...
#<Topic::xxxx>
And, when you don't supply the collection, you get only 2 yes/no values which is the simple_form default per the config file you found (config/locales/simple_form.en.yml).
So, as you might have guessed by now, this is why what you finally used...
<%= f.association :topic, collection:
Topic.limit(1),
prompt: "Choose a topic",
label: false
%>
...kind of "worked" as a workaround -- because it *did* execute a proper query and supplied it to the simple_form, but it limited the results to only the *first* topic in the table (without any ordering/sort)
At least, this is what I have learned/gone through. HTH!

Editing a single attribute with more than one form field

Background: Users and communities share a
has_many :through
relationship. Each community has a "community_type" string that identifies it (ie "Gender", "City", etc.).
Objective: In my edit form, I'd like to allow the user to edit his :community_ids based on community type. Something like:
<%= form_for current_user do |f| %>
<%= f.collection_select(:community_ids, Community.filtered_by("Gender"), :id, :name) %>
<%= f.collection_select(:community_ids, Community.filtered_by("City"), :id, :name) %>
<% end %>
The issue is that the form only accepts the last form field value for :community_ids - in this case being the "City" - rather than merging all of them as one big :community_ids array.
Solution:
For those interested, I ended up refactoring my model code from the answers below to this:
%W[ community1 community2 community3 community4 ].each do |name|
define_method "#{name}" do
self.communities.filtered_by("#{name}").map(&:name)
end
define_method "#{name}_ids" do
self.communities.filtered_by("#{name}").map(&:id)
end
define_method "#{name}_ids=" do |val|
self.community_ids += val
end
end
Is there a reason you're using select boxes for a has_many relationship? It seems checkboxes would be more appropriate. If you want to go with select boxes, I don't think you can use FormHelper#select, because as far as I know, it's expecting a single value, and your community_ids is an array. This is why it's only picking one of the values you give it.
For a select box (or any field), you can combine the values across fields by adding [] to the parameter name which tells Rails that the parameter is an array of values. You can do this by using regular select_tag to create the fields, and setting the parameter name as follows:
<%= form_for current_user do |f| %>
<%= select_tag("user[community_ids][]", options_for_select(Community.filtered_by("Gender").map{|c| [c.name, c.id]}, :selected => current_user.communities.filtered_by("Gender").first.id) %>
<%= select_tag("user[community_ids][]", options_for_select(Community.filtered_by("City").map{|c| [c.name, c.id]}, :selected => current_user.communities.filtered_by("City").first.id) %>
<% end %>
You could also go with Ryan's approach of sending separate parameters, though one downside is your User model will have to be very aware of the types of communities that exist, and you'll have to write separate logic in the User model for each type of community. This will make your resources less modular. But if you do go that way, I'd suggest using pseudo-attributes instead of a before_save so that your community_ids get updated automatically from the params:
class User < ActiveRecord::Base
...
def community_gender_ids=(cg_ids)
self.community_ids ||= []
self.community_ids += cg_ids
end
def community_city_ids=(cc_ids)
self.community_ids ||= []
self.community_ids += cc_ids
end
...
end
And then your select_tag calls would look something like this:
<%= form_for current_user do |f| %>
<%= select_tag("user[community_gender_ids][]", options_for_select(Community.filtered_by("Gender").map{|c| [c.name, c.id]}, :selected => current_user.communities.filtered_by("Gender").first.id) %>
<%= select_tag("user[community_city_ids][]", options_for_select(Community.filtered_by("City").map{|c| [c.name, c.id]}, :selected => current_user.communities.filtered_by("City").first.id) %>
<% end %>
Updated to complete tsherif's (better than my original) answer.
view.rb
<%= form_for current_user do |f| %>
<%= f.collection_select(:community_gender_ids, Community.filtered_by("Gender"), :id, :name, {}, id: 'community-gender-options') %>
<%= f.collection_select(:community_city_ids, Community.filtered_by("City"), :id, :name, {}, id: 'community-city-options') %>
<% end %>
model.rb
def community_gender_ids=(cg_ids)
self.community_ids ||= []
self.community_ids += cg_ids
end
def community_city_ids=(cc_ids)
self.community_ids ||= []
self.community_ids += cc_ids
end
def community_gender_ids
self.communities.select(:id).where(:community_type => 'gender').map(&:id)
end
def community_city_ids
self.communities.select(:id).where(:community_type => 'city').map(&:id)
end
Alternatively, you could write some CoffeeScript/Javascript to bind to the select tags and add the IDs to a hidden value which is then submitted to the server with the form.

Two records with two different layouts/views in one create

Ok, here my Problem:
i have a tabel in a radius-system. to enable a hotspot-user i need two different records in this table.
the record has the fields user, attr, op, value
first record: attribute has to be Password (this record is only for the password here) and value is the password itself.
second record: attribute has to be something like Max-Allowed-Session an value is an integer in seconds.
So i add this as nested attributes to my controller clients. In new i do that:
def new
#client = Client.new
2.times do
radcheck = #client.radchecks.build
end`
respond to........
end`
Ok, in my view i have this at the moment:
f.fields for :radchecks do |rcbuilder|
<p><%= rcbuilder.label :username %><br /><%= rcbuilder.text_field :username %>
for all fields .....
end
In my controller i built two radchecks, so this part is shown two times.
That is status, but i wanna have something like this in my website
<first occurence of that form>
<%= rcbuilder.hidden_field :attr, :value => "password" %>
<%= rcbuilder.hidden_field :value, :value => #generated_password %>
<end first occurence>
<second occurence of that form>
<%= rcbuilder.hidden_field :attr, :value => "Max-Allowed-Session" %>
<%= rcbuilder.label :value, 'Time in hours' %><%= rcbuilder.text_field :value %>
<end second occurence>
Somone has an idea to realize that. Maybe i have to write the indexed fields myself, but how can i achieve that?
thanks for help....
Assuming you are using accepts_nested_attributes_for :radchecks in your Client model.
<% index=1 %>
f.fields for :radchecks do |rcbuilder|
<%= render :partial=>'radcheck_fields', :locals=>{:rcbuilder=>rcbuilder, :index=>index}%>
<% index+=1 %>
end
Next create a partial named radcheck_fields.html.erb with the following code.
<% if index.eql?(1) %>
<%= rcbuilder.hidden_field :attr, :value => "password" %>
<%= rcbuilder.hidden_field :value, :value => #generated_password %>
<% else %>
<%= rcbuilder.hidden_field :attr, :value => "Max-Allowed-Session" %>
<%= rcbuilder.label :value, 'Time in hours' %><%= rcbuilder.text_field :value %>
<% end %>

Resources