Render new action with params - ruby-on-rails

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]

Related

Simple form array of hashes

I have a form that updates an #identity object. Two #identity attributes need to be assigned from a separate collection #accounts.
#accounts = [{
'name' => 'A',
'page_id' => 1},
{
'name' => 'B',
'page_id' => 2
}]
The form needs display the name, but pass both the name and page_id.
<%= simple_form_for(#identity, :remote => true) do |f| %>
<%= f.input :page_name, :collection => #accounts.map { |a| a['name'] }, :as => :radio_buttons, :item_wrapper_tag => :li %>
<%= f.submit 'Submit', :class => 'btn btn-primary' %>
<% end %>
How can I also pass the matching page_id attribute without displaying it?
:collection => #accounts.map { |e| [e['name'], e['page_id']] }

Hidden values show up as blank

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'

Rails 3: Text_field_tag default value if params[:something].blank?

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 »"

Pass parameter into controller from select box

Here is my view:
<%= form_tag({ :action => "display"}, :method => "get") do %>
<%= select(:music, :type, MusType::TYPES, {:include_blank => true}) %>
Here is my array constant in the model:
class MusType < ActiveRecord::Base
TYPES = ['Jazz','Rock','Blues']
end
My select menu draws values out of an array. How do I pass the selected value into the controller as a parameter after the submit button is pressed?
you can do something like below.
view code
<%= form_tag(:url => {:controller => "mycontroller" :action => "display"}, :method => "get") do %>
<%= select_tag(:music, :type, MusType::TYPES, {:include_blank => true}) %>
<%= submit_tag "Search", :id => 'search' %>
<% end %>
read the selected value in mycontroller.rb
def display
value = params[:music][:type]
// do something with value
end

DRY me. Rails code:

I have several of this, where the only thing that differs is if its a text_field or a password_field etc etc etc.
I'd like to pass this as a parameter to the render, like :as => :password_field for example.
And I don't wan't to do a case compare, the value passed in the :as is the value of the field. Is this possible ?
.text{:class => form.object.errors[field].any? ? "error" : nil}
= form.label field
-if defined? value
= form.text_field field, :value => value
-else
= form.password_field field
-if defined? hint
%p#hint= hint
= render 'shared/error_messages', :object => form.object, :field
.text{:class => form.object.errors[field].any? ? "error" : nil}
= form.label field
-if defined? value
= form.text_area field, :value => value
-else
= form.text_area field
-if defined? hint
%p#hint= hint
= render 'shared/error_messages', :object => form.object, :field => field
Answer is the one below, with some fixes:
-# expects form, field_name, field_type, value and hint variables to be passed
.text{:class => form.object.errors[field_name].any? ? "error" : nil}
= form.label field_name
- if defined?(value)
= form.send(field_type, field_name, :value => value)
- else
/= form.send(:field_type, field_name)
= form.send(field_type, field_name)
-if defined? hint
%p#hint= hint
= render 'shared/error_messages', :object => form.object, :field => field_name
Usage:
= render 'shared/form_field', :form => f, :field_name => :email, :field_type => :text_field
Create a partial called shared/form_field.html.haml
- # expects form, field_name, field_type, value and hint variables to be passed
.text{:class => form.object.errors[field_name].any? ? "error" : nil}
= form.label field_name
- if defined?(value)
= form.send(:field_type, field_name, :value => value)
- else
= form.send(:field_type, field_name)
-if defined? hint
%p#hint= hint
= render 'shared/error_messages', :object => form.object, :field => field_name
You can invoke the partial as
- form_for :user do |form|
= render 'shared/form_field', :locals => {:form => form,
:field_name => :login, :field_type => :text_field}
If I understand your question right, you're after this:
render :partial => 'user/login_errors', :locals => { :field => :first_name, :value => #user.first_name, :form => form }
You use the locals hash to pass in any number of variables. In your case, the variables in your code snippet were form, field, and value. The keys in the hash determine what the variable will be referenced as in the partial, and the values in the hash determine the variable values.

Resources