I am trying to reuse code in a form between an edit and a new view. In the New view, I'd like the checkbox to be checked by default. In the edit view I'd like it to reflect the current state of the object. What's the best way to share this info? The code below is what I want for "new" but for edit I don't want it hard-coded to "checked: true".
<div class="row">
<%= form_for #producer_type do |f| %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :enabled %>
<%= f.check_box :enabled, {checked: true} %>
<% end %>
</div>
You will need to pass an object from the controller to the form.
def new
#producer_type = ProducerType.new(enabled: true)
end
def edit
#producer_type = ProduceType.find(params[:id])
end
And use that in the form, Rails automatically checks it if enabled is true
<%= form_for #producer_type do |f| %>
<%= f.check_box :enabled %>
Replace true with current_page?(action: 'edit')
See ActionView::Helpers::UrlHelper#current_page? for more on the current_page methods.
And I don't think you need the curly braces because the hash is at the end of a parameter list.
Related
How can I disable a button once the user was able to send a request. I used the logic of checking if an object in empty or has value in the controller and if the object is empty the user can submit a request otherwise the button will be hidden or disabled.
Object in the Controller
#c_req = Notification.where("message LIKE ?", "%{friend}%")
View
<%= form_for :friend, url: friends_path, method: :post do |f| %>
<%= f.hidden_field :message, :value => "Accepted your friend request" %>
<%= f.hidden_field :date, :value => #date %>
<%= f.submit 'Submit', :disabled => #c_req.present? %>
<% end %>
I used :disabled => #c_req.present? to check if it has a value.
If disabled attribute is added in any input field then it will be disabled either it is disabled = false.
You want to enable button if object is empty.
<%= form_for :friend, url: friends_path, method: :post do |f| %>
<%= f.hidden_field :message, value: "Accepted your friend request" %>
<%= f.hidden_field :date, value: #date %>
<%= f.submit 'Submit',class: "submit_btn" %>
<% end %>
<script type="text/javascript">
$(document).ready(function(){
<% unless #c_req.empty? %>
$(".submit_btn").attr('disabled', true)
<% end %>
})
</script>
Above js code runs when page is loading, if the #c_req is empty then your submit button is enable else disable
Try to replace <%= f.submit 'Submit', :disabled => #c_req.present? %> with <%= f.submit 'Submit', :disabled => !#c_req.present? %>
You have twisted the logic. You want to disable the button if it does NOT exist.
I'd try changing to .any? or .empty?
And be nice you your future self and name the variable better.
#present? is used to check if the item is there. Disabling has to happen if the item is not there. For that we have #empty?
Please don't use things like c_req, be a little descriptive in variable names. Also follow a code style, some thing like this...
Update to your code...
<%= form_for :friend, url: friends_path, method: :post do |f| %>
<%= f.hidden_field :message, value: "Accepted your friend request" %>
<%= f.hidden_field :date, value: #date %>
<%= f.submit 'Submit', disabled: #c_req.empty? %>
<% end %>
If you are hiding it... Use a similar query to decide not to show the button or hide the button from css.
Ex:
<%= f.submit 'Submit', class: ('hidden' if #c_req.empty?) %>
I guess you get the idea!
After my users fill out a form, I want them to have the option of sending or not sending an email.
I can do this easily in the controller by doing something like:
send_email if params[:entry]
but I'm not sure how to introduce this param under my form_for, since it isn't part of the model.
How can I get this param to show up in the view and be available upon submit?
Use the #check_box_tag form helper in the form_for block
It can be something as simple as this:
<%= form_for #notice do |f| %>
<%= f.label :text, 'Notice Text' %>
<%= f.text_area :text %><br />
<%= label_tag 'entry', 'Send Email?' %>
<%= check_box_tag 'entry' %>
<%= f.submit %>
<% end %>
I've got a form for an order and with this, the user have to choose a category then associated product. As I want it to be dynamic, I wrote this :
_form.html.erb
<%= form_for(#order) do |f| %>
<%= f.collection_select :category_id, #categories_list, :id, :name, :prompt => "Selectionner" %>
<%= render :partial => 'products' %>
<%= f.submit 'Enregistrer', :class=>'button add'%>
<% end %>
_products.html.erb :
<%= form_for(Order.new, :remote => true) do |f| %>
<% if !#products.blank? %>
<%= f.label 'Produit :' %><br />
<%= f.select :product_id, #products.collect{ |s| [s.name,s.id]}, :prompt => "Selectionner" %>
<% else %>
<%= f.label 'Produit :' %><br />
<%= f.select :product_id, '' %>
<% end %>
<% end %>
in the controller :
def update_product_select
#products = Product.where(:category_id=>params[:id]).order(:name) unless params[:id].blank?
render :partial => "products", :layout => false, :locals => { :products => #products }
end
For the dynamic part, it works.
But when I click on the submit button, the product ID is not sent !
Can you tell me how is it possible for me to combine dynamic menu and form submitting please ?
Thanks.
I don't understand why you need two forms. Something is wrong here.
Explain a bit more on the workflow if possible.
I see you should either make use of fields_for if you are trying to edit other associated instances of #order.
For a dynamic menu, have a javascript event binding on the category that updates the products list based on the category. Let me know if you need an example.
I think you are looking for this: http://railscasts.com/episodes/88-dynamic-select-menus-revised
It needs a subscription, but you can also see the previous episode to get an idea if you dont want to subscribe.
I have a table rooms and I have a field type(Single, Double). I want to use a radio button for this field. So, I am using it like this:
<% form_for(#room) do |f| %>
<%= f.radio_button :type, "Single" %>
<%= f.radio_button :type, "Double" %>
<% end %>
This works fine for edit view. The problem is that for the new view, I want to default the radio button to "Single". For this code, no value is checked for the new view.
I am now adjusting that with condition check
<% form_for(#room) do |f| %>
<%= f.radio_button :type, "Single", :checked => #room.new_or_single? %>
<%= f.radio_button :type, "Double" %>
<% end %>
Room model
def new_or_single?
type.nil? or type == "Single"
end
Is there a better way to achieve this?
Set default :type in constructor
def new
#room = Room.new(:type => "Single")
end
Is it possible to specify html attributes while using the form_for helper methods?
For example:
<% form_for #user do |f| %>
<%= f.label :username%>
<%= f.text_field :username %>
<%= f.submit "Signn Up" %>
<% end %>
How would I go about specifying the class for the label? Is it possible, or do I have to resort to label()?
On mostly helpers, the last arg is a hash of html options for the element.
<%= f.label :username, "Username", :class => "class" %>