Creating a drop down list in rails - ruby-on-rails

Can any one help me in creating a drop down list in rails. I'm having a user table with a role field and I want to create a drop down list with values manager, investigator, director for role.How I can extract this value to parms[:role] . I'm new to rails.

If you are using form_for (not form_tag), then it should go like this.
<%= form_for #user do |f| %>
...
<%= f.select :role, options_for_select(%w[manager investigator director]) %>
<% end %>
Then you'll have params[:user][:role] available in your controller.
Refer this for more info on select helper.

Related

Rails - Multiple forms, different Models (Objects), one submit button

I have a view with 3 forms, Schedules, Workouts and Exercises, all behaving like an edit form, each. And one submit(save) button in the all the view.
When I click on the save button. Every data changed on those forms should be updated after click.
What is the best solution for this ? Javascript updating each data separated ? How to do that ? Is there a more Rails way to do this easily ?
My difficulty is how to integrated all those models in one view, while all this is happening in the show(view) from the Student model.
If you're implementing something like a profile / edit page (where you can save all the records at once), the two ways I would look at would either be to save the forms via Ajax, or use a single submit method to handle them
Ajax
The ajax method would be the most conventional:
Every form you submit will go to the form's own update method in the backend
Each form could be handled by a single button, but it's best to split them up
#app/controllers/profile_controller.rb
def edit
#schedules = Schedule.all #-> not sure how many records you're using
#workouts = Workout.all
#exercises = Exercise.all
end
#app/views/profile/edit.html.erb
<%= form_for #schedule do |f| %>
<%= f.text_field :test %>
<% end %>
# -> other forms
<%= button_to "Save", "#", id: "save" %>
#app/assets/javascripts/application.js
$("#save").on("click", function() {
$("form").submit(); // we'll have to define the form to submit
});
Single
If you submit all the forms as one, you'll have to encase them all in a single form, as sending different errors. This could be achieved by using _, and handled in the backend by looping through the different params, saving each one individually.
I'd do this:
#app/controllers/application_controller.rb
def submit
types = %w(schedules exercises workouts)
for type in types do
type.constantize.update_attributes()
end
end
This allows you to create a form with the different data types submitted in the same action:
#app/views/profile/edit.html.erb
<%= form_tag profile_submit_path do %>
<%= fields_for #schedules do |f| %>
<%= f.text_field :title %>
<% end %>
# -> fields_for for the other objects
<% end %>
This will allow you to send the updated objects to your controller, allowing them to submit
If all of your models (Schedules, Workouts and Exercises) are associated, using fields_for should be a good option.
From the above link:
<%= form_for #person do |person_form| %>
First name: <%= person_form.text_field :first_name %>
Last name : <%= person_form.text_field :last_name %>
<%= fields_for :permission, #person.permission do |permission_fields| %>
Admin? : <%= permission_fields.check_box :admin %>
<% end %>
<%= f.submit %>
<% end %>
Read the guides.
You could have some simple javascript that iterates over all form tags and submits each of them.
Alternatively, if you are going to use javascript anyways, you could follow an AJAXish auto-save approach upon changing any field.
But I think it might be cleaner if you just had one form for multiple models, using fields_for.

Rails form for mailing

I'm trying to build a form which preloads content from two models (two variables being passed, being shown in the textfields) and then, not saves the data but sends the altered content (from the textfields) as two(?) variables to a mailer class.
I've managed to preload the data from one of the two models but am not sure how the form_for tag has to look like to get both models loaded as well as targeting the mailer class method instead of updating the model entity when pressing "send".
Do I need the accepts_nested_attributes_for attribute inside the model if I'm not saving anything?
I hope someone could give me an small example of the crucial parts. A thousand thanks!
You can use fields_for to include other models in same form. You can use it inside the same form_for what is present.
Checkout the example here from the api docs,
<%= form_for #person do |person_form| %>
First name: <%= person_form.text_field :first_name %>
Last name : <%= person_form.text_field :last_name %>
<%= fields_for #person.permission do |permission_fields| %>
Admin? : <%= permission_fields.check_box :admin %>
<% end %>
<%= f.submit %>
<% end %>
when you submit the data from this form, you can just use that data to pass to the mailer class from controller. UserMailer.get_user_info(params[:name], params[:address]).send
Creates a scope around a specific model object like #form_for, but doesn't create the form tags themselves. This makes #fields_for suitable for specifying additional model objects in the same form.
Refer Docs here:.
fields_for(record_name, record_object = nil, options = {}, &block)

How to make a check box?

Hello. I am totally new to Ruby on Rails and I want to know how to make a checkbox or link_for in a Ruby on Rails application that will change the value of an attribute called admin in a table called users from false to true , to make the normal user an admin.
NOTE : I am using form_for in creating forms
You may want to read, it explains how to create forms: http://guides.rubyonrails.org/form_helpers.html
You probably want something like (not tested for syntax):
<%= form_for :user do |f| %>
<%= f.input :admin %>
<%= f.button :submit %>
<%= end %>
in your user model make sure you have:
attr :admin

rails 3.1 select_tag usage

Ive got a select_tag field at the top of a page and trying to get the selected option to change the content on the page based on the users selection.
Im a learner and have found pieces of information around but without detailed examples and good explanations on how to best approach and implement.
The scenario is as follows:
I have a belongs_to association between a project and documents and in one of my views which lists documents, I want to only show all the documents that belong to the currently selected project in the select tag.
Passing the selected project's id to the documents index action which only shows documents for a specified project id via a link_to tag came to mind. This would thus refresh the page with the correct documents in the view but I believe that is not the correct way to do it and that I cant use link_to tags as options in a select_tag. Can anyone help and offer an example?
I would suggest using the form.select method and options_for_select as in
f.select :attribute, options_for_select(#array, default_value)
and in your controller you should create or update using the submitted parameter
n = record.new(:attribute => params[:attribute])
have fun
In your controller:
def index
if params[:project]
#documents = Document.where(:project => params[:project]
else
#projects = Project.all
end
end
In your form/view:
<%= form_tag 'projects', :method => :get do %>
<%= options_from_collection_for_select(#projects, :id, :name)
<%= submit_tag %>
<% end %>
<% if #documents %>
<%= #documents.each do |d| %>
....
<% end >
<% end %>

Rails Multiple Entries in a form

I'll try and explain this as much and as easily as possible.
I have a Rails form, and 3 models.
Models: DemoModule, SalesDemo, and SalesDemoModule
What I want to do in my view/form is create a new SalesDemo, but a SalesDemo has many SalesDemoModules.
In the controller I have:
#sales_demo = SalesDemo.new
#demo_modules = DemoModule.find(:all, :conditions => ['active = true'])
How can I, in my view, have a text field row for each DemoModule, which I can pass back to the controller action, to save into SalesDemoModule?
You can specify that the SalesDemo accepts_nested_attributes_for SalesDemoModule, which then allows you to created a nested form (i.e. within a form_for a SalesDemo, you can have fields_for SalesDemoModule). Here's a simple example.
Simply put:
<%= form_for #sales_demo do |sales_demo_form| %>
<%= sales_demo_form.text_field "some_sales_demo_property" %>
<%= sales_demo_form.fields_for #demo_modules do |modules| %>
<%= modules.text_field "some_module_text_field" %>
<% end %>
<% end %>
In the SalesDemo, you will need to have
accepts_nested_attributes_for :demo_modules
You can get some more information here.

Resources