capybara Rails3 'cannot fill in' - ruby-on-rails-3

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

Related

rails check_box always has 0 value

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>

Pass Contest Id into URL for search in Index

I have two models, contest and submission. submission belongs_to contest and contest has_many submissions.
In the index action for submissions I have a search:
def index
contest_id = params[:contest_id]
#contest = Contest.find(contest_id)
if params[:search].blank?
#submissions = Submission.paginate(:per_page => 10, :page => params[:page])
else
#submissions = Submission.search(params[:search]).paginate(:per_page => 10, :page => params[:page])
end
#search = params[:search]
end
I think the right way to pass it in is through the search form in the submissions>index view:
<div class ="span12 row">
<%= form_tag submissions_path, :method => 'get', :class => "form-search pull-right" do %>
<%= text_field_tag :search, params[:search], :class => 'input-xlarge', :placeholder => 'Search by member, title or description' %>
<%= submit_tag "Search", :title => nil, :class => 'btn btn-primary' %>
<% end %>
</div>
And I have been able to come close using this:
<%= hidden_field :contest_id, #contest.id %>
In the form, but it's returning this in the url:
http://localhost:3000/submissions?utf8=%E2%9C%93&search=test&contest_id%5B%5D=&commit=Search
And an error:
Couldn't find Contest with id=
I've also tried this:
<%= hidden_field(:contest_id, :value => #contest.id ) %>
But it's returning similar url and error.
Right now, I'm stuck. If you have any idea, please let me know.
[edit - added html]
Before search:
<div class ="span12 row">
<form accept-charset="UTF-8" action="/submissions" class="form-search pull-right" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /></div>
<input class="input-xlarge" id="search" name="search" placeholder="Search by member, title or description" type="text" />
<input id="contest_id_5" name="contest_id[5]" type="hidden" />
<input class="btn btn-primary" name="commit" type="submit" value="Search" />
</form>
</div>
Here's what works from other links going to submissions:
From the submission show page:
<%= link_to 'Browse All Submissions', submissions_path(:contest_id => #contest.id), :class => 'btn btn-mini pull-right' %>`
and from the contest show page:
<%= link_to 'Browse All Submissions', submissions_path(:contest_id => #contest.id), :class => 'btn btn-mini pull-right' %>
Both of these pass the url "contest_id=5" which is what the controller needs to find a contest. The issue I'm having with search is finding the right syntax to get contest_id=5 to appear without the mumbo jumbo mucking it up.
This turned out to be an easy solution. Insert a hidden_field_tag in the search form:
<%= hidden_field_tag 'contest_id', #contest.id %>
This will pass the correct value into params:
http://localhost:3000/submissions?utf8=%E2%9C%93&search=new&contest_id=5&commit=Search

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.

Capybara won't fill in field with duplicate labels

I have a form page with a confirmation field for the email address and the password, both with the label "Confirmation". This fails when running with RSpec, as expected. If I make the labels unique, say by changing the email label to "Email Confirmation", then it will pass. I'd rather keep the labels the same. If I use the email ID generated by form_for, Capybara seems happy but the test fails. I've also tried the name with the same result.
RSpec/Capybara extract:
fill_in "company_contact_email_confirmation", with: "example#example.com"
form_for extract:
<div class="field">
<%= f.label :contact_email_confirmation, "Confirmation" %><br />
<%= f.email_field :contact_email_confirmation %>
</div>
Generated source:
<div class="field">
<label for="company_contact_email_confirmation">Confirmation</label><br>
<input id="company_contact_email_confirmation" name="company[contact_email_confirmation]" size="30" type="email">
</div>
Error message:
1) Company pages signup with valid information should create a company
Failure/Error: expect { click_button submit }.to change(Company, :count).by (1)
count should have been changed by 1, but was changed by 0
If I use Capybara within, as has been suggested elsewhere here, it still does not work:
RSpec/Capybara extract:
within ".email_stuff" do
fill_in "Confirmation", with: "example#example.com"
end
form_for extract:
<div class="email_stuff">
<div class="field">
<%= f.label :contact_email_confirmation, "Confirmation" %><br />
<%= f.email_field :contact_email_confirmation %>
</div>
</div>
Generated source:
<div class="email_stuff">
<div class="field">
<label for="company_contact_email_confirmation">Confirmation</label><br />
<input id="company_contact_email_confirmation" name="company[contact_email_confirmation]" size="30" type="email" />
</div>
</div>
Capybara does not seem to complain but the test fails with the same error. How do I get Capybara to allow me to use the same label twice on the same form?
I added some debugging statements to the ActiveRecord after_validation callback to see the status of the virtual confirmation fields. As shown in my code above, I'd referenced the email confirmation field by name or ID, but not BOTH the password confirmation and email confirmation fields with IDs. Capybara was putting the password data in the wrong confirmation field since the email confirmation field was first on the form. If I'd put the IDs on the password field, it would have worked. IDs on both are safest.