Using the best_in_place gem within a table - ruby-on-rails

I have a table that lists all users. Instead of opening each user and then editing, I want to edit certain fields within that table.
- #users.each do |u|
%tr
%td= u.name
%td= best_in_place u, :email_flag, :type => :checkbox
The issue seems to be that the only param getting passed back is the checkbox field:
Processing by UsersController#update as
Parameters: {"user"=>{"email_flag"=>"true"}
Am I approaching this the wrong way? Why isn't the user object 'u' getting passed back?

best_in_place is working correctly here. The entire set of attributes from that you're editing need not be transferred each time. That's what the HTTP PUT (or PATCH depending on your version of Rails) is used for -- updating attributes on existing instances of models. It's like in SQL, when you run UPDATE users SET email='something#example.com' WHERE id=1, you don't have to list every field every time. Only the specified columns are touched.
When you add another field for last_name or country as you said in your comment, it will work the same way.

Related

How do I create a drop down in a Rails form where the user can change the default value?

I'd like to have a drop down in my Rails form where users can select an area of a city, e.g. "Marchmont", "New Town", "Baberton" etc, when adding an order. I'd like that once they have made a selection, this will then be the default selection for the following times they use the form to add an order (so that they don't have to keep selecting it) but also that they can change their selection at any time. Hope that makes sense. I'm using the Simple Form gem. Thanks in advance! :)
#Steve
I will make a couple of assumptions.
1.) you know how to create forms within the rails templating engine.
2.) you understand how to create a dropdown menu using the Simple Form gem
So you have a couple of options based on what you actually want to accomplish. Based on what you are briefly describing, it sounds like you have some kind of an e-commerce/checkout situation that you want auto-completion to make it easier for a user.
there are a couple of approaches to storing this data.
Saving the user Data.
1.) Save it right on the user model under district_of_last_order
2.) Save it right on the order model that a user has_many orders. Then you can pull the first order's city district and select that
Personally I would lean on #2 as you probably want to be able to tightly couple the order with the user and saving that information twice is redundant since you can always do something like current_user.orders.first.district or whatever,
in your ERB where you build the form you can then do something along these lines:
<%= simple_form_for(#order) do |f| %>
... other input fields
<% if current_user.orders.first %>
<%= f.input as: :select selected: current_user.orders.first.district %>
<% else %>
<%= ... your regular dropdown menu here without the default %>
<% end %>
... rest of your form
If you have the option of using gems, I have had good results with select2-rails.

Issue with Rails multi select options field

I've set in my model('Bambino') a multi-select field to assign a value to the string attribute 'status'. Find the code below from my form partial:
<%= f.select(:status, options_for_select([['segnalato','segnalato'],
['inserito','inserito'],['drop','drop'],['concluso','concluso']])) %>
When I want to edit my record the edit form does not give me back the previous stored value but sets automatically the default value to 'segnalato' (E.g.:if I create a new record setting the status to 'inserito' and after I want to edit the record I get the edit form with the default value of 'segnalato' while I am expecting to see in the field 'inserito').
In this way when you edit a record chances to make a data entry mistake are very high. Why so? Is there a way to retrieve the proper 'status' value that was assigned when the record was created? Thanks
Are you sure that #your_record.status is equal to one of those values? Check it out before any further debugging.
Whilst Andrey Deineko's answer is probably the one you want, there is a better way to achieve what you're doing: enum.
#app/models/bambino.rb
class Bambino < ActiveRecord::Base
enum status: ['segnalato', 'inserito', 'drop', 'concluso']
end
This will give you the ability to use the following:
<%= f.select :color, Banbino.status.to_a.map { |w| [w.humanize, w] } %>
This will store a number for the status, whilst allowing you to define what each number means. It won't do anything about loading a pre-selected object (that's what Andrey's answer will do), but will give you the ability to make your application & select more succinct.

Display money-rails value in nested form

I have a nested form where a user can add multiple instances of a class. This nested form is part of a larger wizard (multi-step form) where the user can go forwards and backwards through the process.
In my rails application I'm using the money rails gem. When a user goes backwards in the wizard, I'd like to populate the fields so they can see what they've previously entered. Up until now, I've had no issue doing this with other nested objects. However, displaying a money field is stumping me.
# the field
<%= f.text_field :guarantor_net_worth, :value => ( f.object.guarantor_net_worth_cents.nil? ? nil : f.object.guarantor_net_worth_cents ) %>
# In console when I see if there is a value assigned to an object that I'm testing
guarantor_net_worth_cents: 4564600, guarantor_net_worth_currency: "CAD"
The thing I can't seem to figure out is when I set :value => f.object.guarantor_net_worth_currency, the field displays CAD, but won't seem to display the cents. To debug futher, I used puts to see the ID's of the objects I was creating and searching them in console and they are being created properly with the values being passed through params as they should.

Accessing form values for use in hidden fields using Ruby on Rails

I have three select tags for birthday (day/month/year) that look like this one:
<td><%= select_tag(:birth_day, options_for_select((1..31)), {:id => 'select1'}) %></td>
I'm trying to access the three values and combine them into a single string and then submit that string using a hidden field. I can't figure out how to get the values to read them into the hidden field.
Right now my best attempt is:
<%= f.hidden_field :combined_date, :value => "#{#birth_day}" %>
But birth_day just comes through as an empty string. (I'm planning on combining this string with birth_month and birth_year after I get the values to read in).
Can someone please help me with this problem?
Why not just combine them when you save the record?
In controller:
combined_date: (params[:day]+"/"+params[:month]+"/"+params[:year]).to_date
#record.save
It's not elegant, but at least you don't need to use javascript just to unnecessarily combine them on the client side.

select field, text field and foreign key

I would like to know which way is the best to resolve my question :
I have a form in order to select people via a select field. If the name is missing in the select field, a text field is available to add directly the person's name.
- The form in new.html.erb is the format of the new action of the Team controller.
- The list of the people is extracted from the People model.
def new
#team = Team.new
#people = People.all
end
I created an attribute in the Team model to store the new_person text field :
class Team < ActiveRecord::Base
attr_accessor :new_person
...
end
Finally, here's an extract of my view :
<%= f.select :person_id, #people.map { |p| [p.name, p.id] } %>
<%= f.text_field :new_person %>
Obviously, I would like to save the new person in the table Person before saving the data from the form. As usual, the id are saved instead of the names
At this point, I've got two issues :
1/ The params array has the key new_person what doesn't have the table. So it is not possible to use the Team.new(params[:team]) method. Does exist an easy solution to avoid this problem ?
2/ As I need the person_id, how can I get it when the name comes from the new_person field? In using the before_filter method ?
Thanks a lot,
Camille.
1) You should consider using fields_for in your view within your form_for block. This will allow you to specify that the fields within the fields_for block are attributes of a different model, will generate the appropriately named input fields, and allow you to use params[:team] in your controller. See the FormHelper documentation for more on this.
2) While you could do something in your controller to first check for a value in the new_person field, create the record, update the contents of params[:team] with the value of the newly created person and create the team, this feels a bit like a hack to me. Another possible solution which may be less fragile would be to use some JavaScript on the page that would render some kind of modal dialog for the user to create the new person, submit the new person to the person#create controller method, then refresh your drop down. It would probably not be terribly difficult to do this using a jQuery UI modal form (very good example at that link to do what you need) with Rails remote form and unobtrusive JavaScript.
This is probably a more difficult solution to your second question than you are hoping for, but probably more useful in the long run.

Resources