I have a form using rails and being submitted by angularjs. The problem I'm having is that when I try to access the form data from angular, the input that is not hidden will show up in the form newTask object, but the fields that are hidden won't. Is there something I am missing here?
= form_for [#patient, Task.new], html: {action: nil, name: 'newTaskForm', 'ng-submit' => 'taskCreate($event, newTask)'} do |f|
= f.hidden_field :taskable_id, :value => #patient.id,
'ng-model' => 'newTask.taskable_id'
= f.hidden_field :taskable_type, :value => #patient.class.to_s,
'ng-model' => 'newTask.taskable_type'
= f.hidden_field :creator_id, :value => current_user.id,
'ng-model' => 'newTask.creator_id'
= f.text_field :text, required: true, placeholder: 'Add a task...',
'ng-model' => 'newTask.text'
= f.submit 'Add task', 'ng-disabled' => 'newTaskForm.$invalid'
Related
I have two forms, one containing a dropdown where the user can choose how a list is beeing sorted, the other one containing a searchfield, where the user can search through that list. Now if a user searches for "test" and ten results show up, I want the user to be able to choose from the dropdown, how the results are beeing sorted. Accordingly if he sorts the whole list, I want himto be able to search through the list, with the results showing up in the sorted way he choose before. Due to code restrictions I have to keep those two inputs in different forms.
Here is the sort-dropdown:
= simple_form_for path, :method => "get", html: {id: "sortform"} do |f|
= f.input :sort, :collection => [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :label => false, :required => false, :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')
And here is the search:
= simple_form_for path, :method => 'get', :label => t(:'videos.search'), html: {id: "search-form"} do |f|
= f.input :q, { input_html: { class: 'form-control searchbar', :name => :q, id: "search", :value => params[:q]}, :placeholder => t(:'videos.search'), :required => false, :label => false}
Is it possible to keep the two inputs seperate or would it be way easier to use just one form?
You can use separate forms if you need to, just store the other parameter in a hidden field in each of the forms.
= simple_form_for path, :method => "get", html: {id: "sortform"} do |f|
= f.input :sort, :collection => [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :label => false, :required => false, :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')
= f.input :q, as: :hidden, input_html: { :name => :q, :value => params[:q] }
= simple_form_for path, :method => 'get', :label => t(:'videos.search'), html: {id: "search-form"} do |f|
= f.input :q, { input_html: { class: 'form-control searchbar', :name => :q, id: "search", :value => params[:q]}, :placeholder => t(:'videos.search'), :required => false, :label => false}
= f.input :sort, as: :hidden, collection: [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')
I have ajax-based select in my form, my form:
= form_for #car, :html => {:id => "new-car-form"} do |f|
- if #car.errors.any?
- if #car.errors.any?
.flash-notice
%b= "Ошибки в полях: #{#car.errors.count} ошибка(и)"
%ul
- #car.errors.each do |attr,msg|
%li= msg
.field
= f.label :vehicle_manufacturer_id, "Марка"
= f.select :vehicle_manufacturer_id, options_from_collection_for_select(VehicleManufacturer.all, :id, :name, #car.vehicle_manufacturer_id), {:prompt => "Выберите марку"}, required: true, id: "manufacturer-select"
.field
= f.label :vehicle_model_id, "Модель"
#models-area
= render :partial => 'models', :object => #models
and partial:
- if #models.present?
= select_tag "by_model", options_from_collection_for_select(#models, "id", "name", selected: #selected), :prompt => "Выберите модель", required: true, id: "model-select"
- else
= select_tag "by_model", nil, :prompt => "Выберите модель", required: true, id: "model-select", disabled: true
I test, and could have such case: when I didn't enter something and submit form - I see validation errors and my select values are nil, but how and in which action to append this something like this:
#models = VehicleModel.where(manufacturer_id: #car.vehicle_manufacturer_id)
#selected = #car.vehicle_model_id
I use rails4.
What I do wrong and how to solve my problem?
Try passing your form variable through to the partial; as the select_tag on its own won't be able to associate it's value with the form_for #car.
= render :partial => 'models', :object => #models, :form => f
Then, in the partial:
= form.select "by_model ..."
To be more specific, that form_for #car clause at the top of your form says 'wrap eveything in this block in a 'car' object when it is submitted'. So right now the form you're submitting has values like this:
car[vehicle_manufacturer_id]
car[vehicle_model_id]
by_model
But you need to wrap that last by_model attribute in the car array like the others:
car[vehicle_manufacturer_id]
car[vehicle_model_id]
car[by_model]
I am using strong parameters and when I try to save the following form I get the following message.
undefined method `household_params' for # '<'VisitsController:0x007fa88deec428'>'
I am confused because I am using the visits controller and not the households controller - The visit is associated with a household as shown below and I call the view with the following code:
= link_to 'New Visit', {controller: 'visits', action: 'new',
household_id: household.id, method: 'post'}, class: 'btn btn-primary'
The Form is:
%h3 Household: #{household.name}
%h4 Household Members: #{household.neighbors.count}
%h4 Visits: #{household.visits.count}
= simple_form_for visit do |f|
= f.input :visited_on, :label => 'Visited On', as: :date_picker, input_html: { class: 'span2' }
= f.input :starch, :label => false, collection: ['Beans','Rice','Potatoes'],selected: 'Beans'
= f.input :cereal, :label => false, collection: ['Cereal','Grits','Oatmeal']
= f.input :option1, :label => false, collection: ['Peanut Butter Jelly', 'Deserts','Baby Fromula'], prompt: 'Options'
= f.input :items_received, :label => 'Special Needs',input_html: {rows: 4, class: 'span9' }
= f.input :notes, :label => 'Notes',input_html: {rows: 4, class: 'span9' }
= f.button :submit, :class => 'btn-primary', :label=> 'Save'
The form works fine without the three lines that display information about the household
I am thinking strong_parameters is getting confused
I am using mongoid with a model field and given validation:
field :status, type:String, :default=>'Active'
validates :status, :inclusion=>{:in=>%w(Active, Done, Canceled, Merged)}, :allow_nil=>true, :allow_blank=>true
in the form, I do not have the status field, so it's supposed to be not POST-ed therefore it's nil on creation:
= simple_form_for([#user, #task], :html => {:class=>'form-horizontal',:'data-type'=>'html'}) do |f|
- if #task.errors.any?
.error_explanation
.alert.alert-error
The form contains
= pluralize(#task.errors.count, 'error')
%ul
- #task.errors.full_messages.each do |msg|
%li=msg
.form-inputs
= f.error_notification
= f.association :project, :collection => current_user.projects.all
= f.input :description, :as => :text, :input_html => {:rows => 5}
= f.input :priority, :as=>:radio_buttons, :collection=>1..4, :item_wrapper_class=>'inline'
= f.input :due_date
.control_group.select.optional
= f.label :assigned_to, :class=>'select optional control-label', :for => 'assigned_to_id'
.controls
= f.collection_select :assigned_to_id, User.all, :id, :username, :class => 'select optional'
.form-actions
= f.button :submit, :class => 'form-button btn-primary', 'data-loading-text' => 'Submitting...'
however, I am still getting this despite setting a default value "Active", which is obviously in the array provided for the validation of inclusion:
Status is not included in the list
why am I still getting this error?
Thanks in advance!
This is your issue
%w(Active, Done, Canceled, Merged)
which translates to
["Active,", "Done,", "Canceled,", "Merged"]
solution is to remove the commas
%w(Active Done Canceled Merged)
How would one do something like:
= f.input_field :age,
:collection => 18..60,
:id => 'age',
:selected => params[:age].blank? or "20"
Above doesen't work. I want to be able to set a default value if there is no param available for that attribute.
Any smart way to do this thx!
EDIT 1:
Full form:
= simple_form_for :people, :url => request.fullpath, :method => :get, :html => { :class => 'form-search' } do |f|
#container_search_small.form-search
= f.input_field :age,
:collection => 18..60,
:id => 'age',
:selected => params[:people][:age_from] || "20"
= f.submit "Go »"
You're using helpers that are taking their values from the object you're building the form on.
So in your controller, you should preset the values on the object.
def some_action
#people = People.new
#people.age = params[:age] || 20
end
Then in the form, remove the :selected option and it should be fine. Make sure you build the form for #people :
= simple_form_for #people, :url => request.fullpath, :method => :get, :html => { :class => 'form-search' } do |f|
#container_search_small.form-search
= f.input_field :age,
:collection => 18..60,
:id => 'age'
= f.submit "Go »"