Conditional logic in simple_form not working in Ruby? - ruby-on-rails

Trying to include conditional logic within my form. If the user choose a certain type, then information shows up that is associated with that type. How can I make this happen within the form?
<%= simple_form_for #post do |f| %>
<%= f.input :title %>
<%= f.input :image %>
<%= f.input :description %>
<%= f.input :link %>
<%= f.collection_select :typeof, Post::TYPEOFS, :to_s, :humanize %>
<% if #post.typeof.include?('audio') %>
<p>Audio Selected</p>
<% else %>
<p>Something else selected</p>
<% end %>
<%= f.input :typeof %>
<%= f.input :approve %>
<%= f.button :submit, "Submit", class: "button" %>
<% end %>

Related

How to put nested fields_for code directly in parent _form?

How can I rewrite the code in duels/_form so I can put the code from dueler_fields directly in there?
duels/_form
<%= simple_form_for(#duel) do |f| %>
<%= f.text_field :consequence %>
<%= f.text_field :reward %>
<%= f.fields_for :duelers do |dueler| %>
<%= render 'dueler_fields', :f => dueler %>
<% end %>
<%= link_to_add_association f, :duelers do %>
<span class="glyphicon glyphicon-plus"></span> Dueler
<% end %>
<% end %>
_dueler_fields
# I want to place these two lines of code in the _form
<%= f.number_field :user_id, placeholder: "Enter User ID" %>
<%= f.number_field :challenge_id, placeholder: "Enter Challenge ID" %>
duels_controller
def new
#duel = Duel.new
respond_with(#duel)
end
duels has_many duelers.
change f to dueler and you should be good to go. or:
<%= simple_form_for(#duel) do |f| %>
<%= f.text_field :consequence %>
<%= f.text_field :reward %>
<%= f.fields_for :duelers do |dueler_form| %>
<%= dueler_form.number_field :user_id, placeholder: "Enter User ID" %>
<%= dueler_form.number_field :challenge_id, placeholder: "Enter Challenge ID" %>
<% end %>
<% end %>

Change Simple_Form Submit Button Action?

I've created the following SimpleForm for my Rails application. I want the submit button to take the data selected in each of the fields, and carry it over to the next screen. Does anyone know how I can change the property of the SimpleForm submit button? Right now it just seems to create something? I'm a little confused by what it's default action is.
<div class="infoheaders">1. Question
<%= simple_form_for #category do |f| %>
<%= f.collection_select(:title, Category.all, :title, :title) %>
<div class="grey-text-subheader">Device: (optional)</div>
<%= f.check_box :Apple %> <%= f.label "Apple" %>
<%= f.check_box :iPhone %> <%= f.label "iPhone" %>
<%= f.check_box :iPad %> <%= f.label "iPad" %>
<%= f.check_box :Mac %> <%= f.label "Mac" %>
<%= f.check_box :Android %> <%= f.label "Android" %><br><br>
<%= f.check_box :Samsung %> <%= f.label "Samsung" %>
<%= f.check_box :Microsoft %> <%= f.label "Microsoft" %>
<%= f.check_box :Windows %> <%= f.label "Windows" %>
<%= f.check_box :Google %> <%= f.label "Google" %>
<%= f.button :submit, 'Submit' %>
<% end %>
</div>
simple form for action can be changed to the action(use 'url' option), we want for example,
<div class="infoheaders">1. Question
<%= simple_form_for #category, :url => url_for(:action => 'your_action', :controller => 'your_controller'), :method => 'post' do |f| %>
<%= f.collection_select(:title, Category.all, :title, :title) %>
</div>
<div class="grey-text-subheader">Device: (optional)</div>
<%= f.check_box :Apple %> <%= f.label "Apple" %>
<%= f.check_box :iPhone %> <%= f.label "iPhone" %>
<%= f.check_box :iPad %> <%= f.label "iPad" %>
<%= f.check_box :Mac %> <%= f.label "Mac" %>
<%= f.check_box :Android %> <%= f.label "Android" %>
<br><br>
<%= f.check_box :Samsung %> <%= f.label "Samsung" %>
<%= f.check_box :Microsoft %> <%= f.label "Microsoft" %>
<%= f.check_box :Windows %> <%= f.label "Windows" %>
<%= f.check_box :Google %> <%= f.label "Google" %>
<%= f.button :submit, 'Submit' %>
<% end %>
</div>
In your controller, you will get the params that were send through the form,
class Yourcontroller
def your_action
// render text: params and return false
if params[:category][:ipad].present?
#checked = params[:category][:ipad]
end
if params[:category][:ipad].present?
#checked = params[:category][:android]
end
if params[:category][:ipad].present?
#checked = params[:category][:samsung]
end
/* check the params and assign to the variables */
// render text: params[:category] and return false
end
end
Now create a your_action.html.erb and you will get values there.
<div>
The checked valus is <%= #checked %>
</div>
Thats it.
Replace Your form with the following
<div class="infoheaders">1. Question
<%=form_for #category, :url=>"/register", do |f| %>
<%= f.collection_select(:title, Category.all, :title, :title) %>
<div class="grey-text-subheader">Device: (optional)</div>
<%= f.check_box :Apple %> <%= f.label "Apple" %>
<%= f.check_box :iPhone %> <%= f.label "iPhone" %>
<%= f.check_box :iPad %> <%= f.label "iPad" %>
<%= f.check_box :Mac %> <%= f.label "Mac" %>
<%= f.check_box :Android %> <%= f.label "Android" %><br><br>
<%= f.check_box :Samsung %> <%= f.label "Samsung" %>
<%= f.check_box :Microsoft %> <%= f.label "Microsoft" %>
<%= f.check_box :Windows %> <%= f.label "Windows" %>
<%= f.check_box :Google %> <%= f.label "Google" %>
<%= f.button :submit, 'Submit' %>
<% end %>
</div>
And in routes Add the following lines
get "/register"=> "controller#action"

Extend forms without page reload, rails

I'm searching for a solution to extend my form without page reload.
First I tried to render a partial with coffee or javascript, but escape_javascript didnt work.
Here's the view
<%= form_for #recipe = current_user.recipes.build do |f| %>
<%= f.label :name, "Recipe Name" %>
<%= f.text_field :name %>
<%= button_to "Add Ingredient", '#', class: "btn btn-lg btn-primary", id: "add" %>
<p></p>
<%= f.submit class: "btn" %>
<% end %>
The form above should be extented with following partial by every click on the button
<div id="recipe-ingredients">Quantity
<%= f.fields_for :quantities, #recipe.quantities.build do |quantity| %>
<%= render 'quantity_fields', f: quantity %>
<% end %>
</div>
_quantity_fields
<%= f.label :amount, "Amount:" %>
<%= f.text_field :amount %>
<%= f.collection_select(:ingredient_id, Ingredient.all, :id, :name) %>
This approach did not work (recipes.js.erb)
$(document).ready(function() {
$("#add").click(function() {
$("p").append("<%= escape_javascript
render(:partial => 'quantities') %>");
});
});
There's a workaround (see below) but I'm searching for a better solution.
$(document).ready(function() {
$("#workout-week").append(<%= escape_javascript(
Haml::Engine.new(File.read(File.join(Rails.root,'app/views',
'_show_period.html.haml'))).render(Object.new, period: #period)) %>);
});
A second approach is to write following lines in Coffee or JavaScript:
<%= f.fields_for :quantities, #recipe.quantities.build do |quantity| %>
<%= f.label :amount, "Amount:" %>
<%= f.text_field :amount %>
<%= f.collection_select(:ingredient_id, Ingredient.all, :id, :name) %>
<% end %>
I'm a newbie so take this with a grain of salt, but have you tried using render :foo instead of redirect_to :foo in the appropriate controller function?
I solved it with cocoon
<%= form_for #recipe do |f| %>
<div class="field">
<%= f.label :name %>
<br/>
<%= f.text_field :name %>
<%= f.fields_for :quantities do |quantity| %>
<%= render 'quantity_fields', :f => quantity %>
<% end %>
<div class="links">
<%= link_to_add_association 'add', f, :quantities %>
</div>
</div>
<%= f.submit %>
<% end %>

Rails: Errors close to particular fields in forms

I am trying to add some errors to a form close to the field that caused the error and here is how I am doing this:
<%= lesson_form.text_field :description %><br />
<% unless #lesson.errors[:description].blank? %>
<span id="error_explanation">
Description <%= #lesson.errors[:description].join(", ") %>
</span>
<% end -%>
<%= lesson_form.label :category %>
<%= lesson_form.text_field :category %><br />
<% unless #lesson.errors[:category].blank? %>
<span id="error_explanation">
Category <%= #lesson.errors[:category].join(", ") %>
</span>
<% end -%>
I would like to know if there is a better an non repetitive way of doing this. As you see I am repeating the same unless errors... for each of the fields.
Use a helper method:
def errors_for(model, attribute)
if model.errors[attribute].present?
content_tag :span, :class => 'error_explanation' do
model.errors[attribute].join(", ")
end
end
end
And in view:
<%= lesson_form.text_field :description %><br />
<%= errors_for #lesson, :description %>
<%= lesson_form.label :category %>
<%= lesson_form.text_field :category %><br />
<%= errors_for #lesson, :category %>
<% end %>
Or you could use simple_form which will do it all for you like this:
<%= simple_form_for #lesson do |f| %>
<%= f.input :description %>
<%= f.input :category %>
<%= f.button :submit %>
<% end %>
And if you use simple_form and haml, things look a bit neater:
= simple_form_for #lesson do |f|
= f.input :description
= f.input :category
= f.button :submit
The above will show the error next to the field and will detect type of the attribute and show the appropriate input box (eg text, password, checkbox etc.), all with one simple line f.input.

RoR field set on form_for

How do you add a field set to a form_for method?
You can use field_set_tag. For example, using a generic 'user' object
For Rails 2.3.x
<% form_for(#user) do |f| %>
<% field_set_tag 'Name' do %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<% end %>
<% end %>
And for Rails 3.0.0:
<%= form_for(#user) do |f| %>
<%= field_set_tag 'Name' do %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<% end %>
<% end %>
You need to have a new object or get an existing object from your dc since it is a 'form for' and then you create a form builder f and call methods on that form builder such as the following:
<% form_for(#object) do |f| %>
<%= f.text_field :method_name %>
<% end %>

Resources