We use simple_form 2.0.2 in our rails 3.2 app. For some boolean fields, we want to make it readonly in certain situation and does not allow to update. Here is the code in our app:
<%= f.input :signed, :label => t('Signed'), :as => :boolean, :readonly => readonly?(#project, 'signed') %>
What we find out is that if :readonly => true, when mouse is over the field on simple form, there is a little red circle (with a slash in the circle) showing up. However the check box still could be changed and saved. Is there a way to make boolean check box on simple_form read only and can't be updated with :readonly? Thanks.
Not with :readonly, no.
The readonly HTML input attribute only prevents the user from changing the value of the field. It doesn't stop them from interacting with it, as clicking on it and toggling the checkmark shows. That only changes the state of the checkbox, whether it's on or off.
The specs on the readonly attribute say this:
readonly
This Boolean attribute indicates that the user cannot modify the value of the control.
If you don't want them changing the state at all, you may want to use disabled:
disabled
This Boolean attribute indicates that the form control is not available for interaction. In particular, the click event will not be dispatched on disabled controls. Also, a disabled control's value isn't submitted with the form.
But since the input is not sent along, the value will be missing. So consider pairing it with a hidden field that actually holds the value.
<%= f.input :signed_display, :label => t('Signed'), :as => :boolean, :disabled => true %>
<%= f.input :signed, :as => :hidden, input_html: {value: #project} %>
Related
I have some trouble here trying to include an include_blank option for 'option_groups_from_collection_for_select'
I would like to have include_blank option but have the currently selected value displayed instead of a blank selection on update action. I have tried this here but it still shows blank.
select_tag(:candidate_source, option_groups_from_collection_for_select(grouped_candidate_sources, :second, :first, :id, :source), { :include_blank => true, :selected => :source })
The last argument in the option_groups_from_collection_for_select is the selected option, which should map to your value.
Since your option_key_method (4th arg) is :id you're allowed to pass the form object directly as an argument. Example:
select_tag(:candidate_source, option_groups_from_collection_for_select(grouped_candidate_sources, :second, :first, :id, :source, f.object.source), { :include_blank => true }
Anyway, see here for more argument information: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/option_groups_from_collection_for_select
Here is the quote_task field in simple form.
<%= f.input :quote_task, :label => t('Quote Task'), :input_html => {:value => #quote_task.id}, :readonly => true %>
Now we want to add an embeded html link for the #quote_task.id to show the content of the quote_task. When a user click the id, he will be directed to the content of the quote task. Something like:
<%= f.input :quote_task, :label => t('Quote Task'), :input_html => {:value => (link_to #quote_task.id.to_s, quote_task_path(#quote_task.id))}, :readonly => true %>
Is there a way to do this in simple_form? Thanks for help.
your expectation for HTML are way beyond any possible semantic.
http://www.w3schools.com/tags/tag_input.asp
http://www.w3.org/html/wg/drafts/html/master/forms.html#the-input-element
Yes it is possible that when one click on a input, it will show desired content. However without JS this wont be possible
input:
<%= f.input :quote_task, :label => t('Quote Task'), :readonly => true, class="redirecter", :'data-quote-task-path'=>quote_task_path(#quote_task.id) %>
coffee script using jQuery:
#app/assets/javascript/my_input.coffee
jQuery ->
$('input.redirecter').click ->
path = $(this).data('quote-task-path')
document.write(path); # ugly redirect, use Ajax
simple solution, but better would be if you load some content from server to your page with Ajax http://api.jquery.com/jQuery.ajax/
but my opinion is that you shouldn't do this at all. Inputs are for forms, forms are for submitting data. What you should be really using is pure link_to without any input due to HTML semantics. If you want it to look like input that you can style it to look like input, point is don't rape input tag for what it not meant to do.
it's not possible to embed anchors within input fields.
you can use javascript to do whatever magic that field should have.
if you want to find out more about javascript. go to amazon, buy a book, read it.
I have a property search page (:controller => 'properties', :action => 'index') that consists of a right sidebar that has a search form which displays the search results below the form. When the user clicks on a property in the right sidebar I want to display the details of that property in the main area of the index page on the left.
Right sidebar is a partial called properties/_property.html.erb:
<%= form_tag properties_path, :method => 'get' do %>
<%= text_field_tag 'location', (params[:location]) %>
<%= select_tag(:max, options_for_select([['No Max', ""], ['$100,000', 100000], ['$200,000', 200000], etc %>
more search fields for baths beds etc
<%= submit_tag "Search", :name => nil %>
<% end %>
<% #properties.each do |property| %>
<%= link_to([property.Address,property.City].join(", "), {:action => 'show', :id => property.id}) %>
<li><strong><%= number_to_currency(property.Price, :precision => 0) %></strong></li>
etc etc
<% end %>
The only way I know how to show the property details is with the 'show' action, but that takes the user to a new page, for example localhost:3000/properties/1865. I've made the 'show' view with the same layout as the 'index' view and have made the right sidebar a partial (properties/_property.html.erb) which appears on both 'show' and 'index' so when the user clicks on a property in the right sidebar and goes to localhost:3000/properties/1865 the property details are displayed correctly in the main area and the right sidebar is on the right.
But because localhost:3000/properties/1865 is a different page than localhost:3000/properties/index the search form in the right sidebar has forgotten it's parameters which means the list of search results in the right sidebar has changed back to the default list of all properties.
How can I display the 'show' action within a partial on the index page so the user's search parameters are remembered by the form in the right sidebar? Or if I have to go to the 'show' page how can I make the right sidebar stay exactly as it is?
Any ideas greatly appreciated, just a suggestion in the right direction would be good, have spent all day trying to figure it out and have got nowhere, thanks
I have made the show view with the same layout as the index view and
have made the right sidebar a partial (properties/_property.html.erb)
which appears on both show and index.
Yes but this will only get you a similar layout for both the pages. You want the list to persist between different requests.
You basically have two options. use session to remember the list which I wont recommend.
Other is you use form :remote => true or ajax and update the page partially.
EDIT:
What version of rails you are using? Do u have jquery loaded in your application?
Follow this SO POST.
You might have to change your show action a bit.
respond_to do |format|
format.js {render :partial => 'property_details' ,:layout => false}
format.html
end
Create a partial for property details and just put body content here.
And link will look like
<%= link_to "link name", {:action => :show, :id => item_id}, :remote => true ,:html => {:class => 'links_product'} %>
Also to update the view you may use(make sure you have rails.js in your page):
$(document).ready(
function(){
$("a.links_product").bind("ajax:success",
function(evt, data, status, xhr){
$("#response").html(data); // in case data is html. (_*.html.erb)
}).bind("ajax:error", function(evt, xhr, status, error){
console.log('server error' + error );
});
});
Have a div with id response or any valid id. Done!
I am attempting to make a voting system for some video. On the vote page I want a radio button for each finalist and next to it an image pulled form youtube the file name and the finalist name. I have a domain for submission and user and made a domain for the voting that is associate to both with the fields submission_id and user_id with submission has_many votes and users has_one vote. Using simple form I create the following:
= simple_form_for #vote do |f|
= f.input :submission_id, :as => :radio, :collection => #finalists, :label_method => :file_name, :value_method => :id, :label => false
= f.submit "Submit Vote 2"
This does work and it puts up a radio button for each and allows the user to select on of the submissions to vote for. However, wanted to put next to the radio button a thumbnail and some text to make it clear what they are voting for. I tried just making a field of HTMl text that of the all the information I wanted:
<img src="youtube/thumbnail" /> <div> Text for label</div>
However, it just prints out the HTML mark up. tried raw(:file_name) but that errored out with bad syntax.
At this point I though I might just need to break down the colleciton and make the loop my self. I tryed the following:
= simple_form_for #vote do |f|
- #finalists.each do |finalist|
= f.input :submission_id, :as => :radio, :value => finalist.id, :input_html => {:name => 'vote[submisison_id]', :id => "vote_submission_id_#{finalist.id}", :value => finalist.id}
= f.submit "Submit Vote"
This results in 5 different radio button collections with their values being yes/no. I of course want one set of radio buttons where one of the submissions gets selected.
I have looked up a few different page on radio_button_tag and collections and I am having a lot of troubles trying to figure out how to tell rails that I want all the radio button to be part of the same set.
Any help would be much appreciated. Thank you.
I'm adapting the authentication system from Rails 3 Tutorial by Michael Hartl to better learn about rails before (most likely) switching to a system like authlogic or devise. My authentication system works great, and I have a user model that now includes fields that build a user profile. For example, users can write a blurb about themselves such as "tennis player, rock climber".
What I'd like to do is to be able to have a simple form that updates only that specific field ("blurb"). I've set this up on a user profile page where the user clicks a link called "edit" and this causes a partial to render showing a form with a field to update a user blurb as such:
in _edit_blurb.html.erb:
<div class = "editforms">
<%= form_for current_user do |f| %>
<div class="field">
<%= f.text_field :blurb, :class => "inputform round", :placeholder => "blurb" %>
</div>
<div class="actions">
<%= f.submit "Update", :class => "submit_button round mini_button" %> <br>
</div>
<% end %>
</div>
This form submits to an update action in the users controller, and for the most part updates the user blurb and nothing else. However, one glitch is that this update action submits a nil password (as there is no password field in the form) - leading to a change in the user password. This is problematic for obvious reasons. Originally, I added a line :if => lamdba {new_record? || !password.nil? } to the password validation to allow the update from the edit_blurb partial to update without needing a password. But, as you can see this allows for a nil password to be saved.
validates :password, :presence => true,
:if => lambda{ new_record? || !password.nil? },
:confirmation => true,
:length => { :within => 6..40 }
How can I alter either the form or the validation (or both) so that a user can update a blurb without updating/inputting his password (and fixing the reset-to-nil password update problem)?
I think I've provided the relevant code, but will add more if it will help! Thanks everyone!
what you'll want to do is prevent the user model from saving the password when the password value is nil. I don't know how your user model is set up, but if you've been following MH's railstutorial, the password should be saved by a method called:
before_save :encrypt_password
in User.rb. To fix this, add:
before_save :encrypt_password, :if => lambda { !password.nil? }
This should work.