rails check_box always has 0 value - ruby-on-rails-3

I have a form with checkbox. The form is mapped to a model.
According to rails documentation to prevent a problem when checkbox is unclicked and nothing for this field is sent, rails generate hidden field just before the checkbox with 0 as a value.
That's correct.
What is not correct is that after I click the checkbox and submit the form, I can see in the post request that first is sent the value 1 and then value 0 (from hidden field)
And as a result of this, the checkbox has always value 0.
The documentation is correct when it says that parameters extraction gets the last occurrence of any repeated key in the query string, but the order in the post request is not correct ????
I use rails 3.2.11.
view
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :name %><br />
<%= f.text_field :name %></p>
...
<div><%= f.label :send_dayplan_info %><br />
<%= f.check_box :send_dayplan_info %>
<div><%= f.submit "Update" %></div>
<% end %>
controller
...
def update
...
if resource.update_with_password(params[resource_name])
....
end
end
summary
When I debug and evaluate params[resource_name] it contains always value 0 for send_dayplan_info field.
When I click the checkbox, I can see in firebug post request contains values:
user[send_dayplan_info]=1
user[send_dayplan_info]=0
When I unclick the checkbox, I can see in firebug post request contains values:
user[send_dayplan_info]=0
Below there is form data for clicked checkbox
utf8:✓
_method:put
authenticity_token:....
user[name]:....
user[send_dayplan_info]:1
user[send_dayplan_info]:0
And the source code of the generated html is:
...
<div><label for="user_send_dayplan_info">Send dayplan info</label><br />
<input name="user[send_dayplan_info]" type="hidden" value="0" /><input id="user_send_dayplan_info" name="user[send_dayplan_info]" type="checkbox" value="1" />
<div><input name="commit" type="submit" value="Update" /></div>

Related

Pass a variable to text_field without changing form

I have _form.html.erb
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :password %>
<%= f.password_field :password %>
Now if I render this form in homepage, HTML code should be:
<label for="session_name">Name</label>
<input id="session_name" name="session[name]" size="30" type="text">
...
If I change my _form.html.erb to:
<%= f.label :name %>
<%= f.text_field :name, disabled: true %>
...
HTML code should be:
...
<input disabled="disabled" id="session_name" name="session[name]" size="30" type="text">
...
But, I don't want to change my _form.html.erb, so how can I pass the disabled: true into my form? I tried to use render partial: but don't know syntax.
I just learn Ruby on Rails for 2 weeks, so please help me.
i donno why u wnt to do so but u can solve the prob by this method. Try doing-
in application helper, add method -
def add_disabled
#i suppose that u want the field to be disabled in the users controller for edit action but not for other controllers or actions
return "disabled='disabled'" if params[:controller] == "users" and params[:action] == "edit"
end
in _form.html.haml
= f.text_field :name, #{add_disabled}
this will call the helper method and return "disabled='disabled'" depending upon the controller and action

Rails Active Admin: Why isn't my custom form rendering any inputs?

Rails 3.1.1
Active Admin 0.4.4
formtastic 2.1.1
This is the active admin controller for my Agency model.
ActiveAdmin.register Agency do
form do |f|
f.input :name
f.input :contact_email, :label=>"Email invoices to"
f.input :api_key, :hint=>"Create a key by following these instructions".html_safe
f.actions
end
end
The form should render the three inputs, followed by the submit button, but all I get is:
Just to be clear, the HTML shows no signs of the missing inputs:
<form accept-charset="UTF-8" action="/admin/agencies" class="formtastic agency" id="agency_new" method="post" novalidate="novalidate" name="agency_new">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓"><input name="authenticity_token" type="hidden" value="...">
</div>
<fieldset class="actions">
<ol>
<li class="action input_action" id="agency_submit_action">
<input name="commit" type="submit" value="Create Agency">
</li>
</ol>
</fieldset>
</form>
I also tried using fieldsets, with the same (faulty) output:
ActiveAdmin.register Agency do
form do |f|
f.inputs "New Agency" do
f.input :name
f.input :contact_email, :label=>"Email invoices to"
f.input :api_key, :hint=>"Create a key by following these instructions".html_safe
end
f.actions
end
end
Update: I've discovered that commenting out the f.actions line gets formtastic to actually print the inputs, but now just without the button.
ActiveAdmin.register Agency do
form do |f|
f.inputs "New Agecny" do
f.input :name
f.input :contact_email, :label=>"Email invoices to"
f.input :api_key, :hint=>"Create a key by following these instructions".html_safe
end
#f.actions
end
end
Form with inputs but no button:
f.buttons should be used, rather than f.actions.
f.buttons, prints the buttons. f.actions expects a block for formatting the buttons, similar to f.inputs.
You are right on track: wrap your inputs in an f.inputs block.
Uncomment the line "f.actions" outside the block.
You should be set.
Try using this: f.input :type => :submit .This worked for me
form do |f|
f.input :starts_at
f.input :ends_at
f.input :type => :submit
end
Try using this: f.buttons :submit
I also think that the block for buttons/actions changed a bit in a version of active admin at some point, so you may be tripped up by old tutorials, etc.

ruby on rails form and passing its parameter to controller

When the view pass the parameters to controller,
controller gets nil for all of the arguements somehow.
Can anyone how to fix this?? Thanks!
and I have no model called "Message"
controllers/messages_controller.rb
def deliver
recipient = User.find_by_username(params[:recipient])
subject = params[:subject]
body = params[:body]
current_user.send_message(recipient, body, subject)
redirect_to :controller => 'messages', :action => 'received'
flash[:notice] = "message sent!"
end
views/messages/new.html.erb
<%=form_for :messages, url: url_for( :controller => :messages, :action => :deliver ) do |f| %>
<div class="field">
<%= f.label :subject %><br />
<%= f.text_field :subject %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_field :body %>
</div>
<div class="actions">
<%= f.submit %>
<% end %>
Check your source HTML to better understand what FormHelpers do.
With the form_for f.text_field will generate names attributes in the format:
messages[subject]
Consequently, your params will be in the format:
params[:messages][:subject]
You can also use <%= debug params %> to inspect what's in params, it's very helpful.
You can get parameter value using datas = params[:messages]
These values are in array form. So you can fetch array datas If you want to individual data then usesubject = datas[:subject]
body = datas[:body]
To check run following code in view
<%= subject %>
this gives the value of subject.

capybara Rails3 'cannot fill in'

This is so frustrating that after examining all stackoverflow queries on a related subject, having the tests seemingly pass before, I cannot get this simple thing to line up and pass. It cannot find a label "quality" in a form_for (#price).
And I fill in "good" in Quality
cannot fill in, no text field, text area or password field with id, name, or label 'price[quality]' found (Capybara::ElementNotFound)
My feature (abridged)
Scenario: Adding corn price
And I fill in "good" in Quality
My step (abridged)
When /^I fill in "([^"]*)" in Quality$/ do |text|
fill_in('price[quality]', :with => text)
end
My form:
<%= form_for (#price), :url => prices_path do |f| %>
<div class="field">
<%= f.label :quality %><br />
<%= f.text_field :quality %>
</div>
My source:
<div class="field">
<label for="price_quality">Quality</label><br />
<input id="price_quality" name="price[quality]" size="30" type="text" />
</div>
I've tried so many combinations to get it to pass, and I've run out of combinations to try, sam
What about use next:
When /^I fill in "([^"]*)" in Quality$/ do |text|
fill_in('price_quality', :with => text)
end

ActiveResource, a model, and Form_Tag

I am trying to use form_tag to pass the params captured by the form to my users controller. I am attempting to communicate with a Sinatra server, and so I do not have a database on the client. My view is as follows:
<% form_tag(#user) do %>
<div class="field">
<%= label_tag :first_mame %><br />
<%= text_field_tag :first_name %>
</div>
<div class="field">
<%= label_tag :last_name %><br />
<%= text_field_tag :last_name %>
</div>
<div class="field">
<%= label_tag :email %><br />
<%= text_field_tag "user[email]" %>
</div>
<div class="field">
<%= label_tag :device_id %><br />
<%= text_field_tag "user[device_id]" %>
</div>
<div class="field">
<%= label_tag :type %><br />
<%= text_field_tag "user[device_type]" %>
</div>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>
The create action on my controller is simply:
def create
#user = User.new(params[#user])
#user.save
respond_to do |format|
if #user.save
format.html { redirect_to(#user, :notice => 'User was successfully created.') }
format.json {render :json => #user }
format.xml { render :xml => #user, :status => :created, :location => #user }
else
format.html { render :action => "new" }
format.xml { render :xml => #user.errors, :status => :unprocessable_entity }
end
end
end
Here's what I get as a result => expected an attributes Hash, got nil
Anybody know why? Thanks for the help.
You need to use form_for and not form_tag. form_for(#user) do
In your model you need to create a schema. Without it Rails doesn't know what do with the data you enter into the form.
When you pass the object into the parameter hash use :user and not #user. #user = User.new(params[:user])
For your form you need to do
<%= form_for #user do |f| %>
<div class="field">
<%= f.label :first_name %>
<%= f.text_field :first_name %>
</div>
# more fields
<% end %>
Note the:
<% %> --> <%= %>
form_tag(#user) do --> form_for(#user) do |f|
label_tag --> f.label
text_field_tag --> f.text_field
In you controller:
#user = User.new(params[:user])
Update:
<% %> --> <%= %>: This is just the convention in rails3, when ever you want to write something in the response you should use later(with = sign). Earlier still works but is deprecated.
form_tag(#user) do --> form_for(#user) do |f|
form_tag(#user) do: form_tag is used to for simple forms which are not tied with any model. You can have the tags inside form_tag named so that they resemble form for, but then why wouldn't you use form_for directly. Apparently the first parameter to the helper is target url, and in this particular case rails magically identifies the url from #user and you didn't notice any bug
form_for(#user) do |f|: form_for is used to create a form for a model, and ties up the form with the instance of the model passed to it. The block for form_for receives a form_builder object, which has equivalents of text_field_tag, label_tag etc. as text_field, label
label_tag --> f.label: first is the common tag which just creates a label tag with no magic attached to it.The later is related with the model object and follows different naming and id conventions, than former. It also ties up with the value of the field, i.e. if the field has an error(failed validation), your label will be surrounded by a div tag with class fields_with_error or something, I can't remember the class name.
text_field_tag --> f.text_field: Former will create a field with name first_name with no magic attached. The later follows a naming convention, the input field will be named user[first_name], so that when you do params[:user] you get a first_name parameter there. It also ties up with the value of the field with the html input, i.e. you get the same error functionality as label and you also get the input automatically prefilled with whatever the value field has in the model instance.