I have this code in my routes:
controller :active_car do
put 'switch_car' => :update
get 'switch_car' => :edit
end
This is my code in on my edit page.
<% form_tag('switch_car', :method => :put) do%>
<div class="field">
<label for="car_info_id">Car Name:</label>
<%= select("", "car_info_id", #available_cars.collect {|v| [v.name, v.id]})%>
</div>
<div>
<%= submit_tag "Switch Car" %>
</div>
<% end %>
When I click submit I get the following routing error.
No route matches "/switch_car" with the url pointing to http://localhost:3000/switch_car?method=put
The get is working just fine as I end the url with switch_car I get my page to edit. For some reason the put definition is not working.
After changing method to second argument it just doesn't work. It seems to have post as the method still instead of put. Here is generated HTML
<form accept-charset="UTF-8" action="switch_car" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="_method" type="hidden" value="put" />
:method belongs to the "options" hash, which is form_tag's second argument, not the first.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag
Please inspect the html generated by that tag and post it here.
Related
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.
I tried to insert a simple_form helper into a view helper (sorry for the pun) and using it with the Haml::Helpers, example:
def front_box_search_html(form_object)
my_custom_wrapper() do
non_haml do
simple_form_for( form_object ||= Form::Search.new, :url => front_search_path ) do |f|
haml_concat f.input :phrase
haml_concat f.button :submit
end
end
end
end
but this code is not rendered correctly: all html code appears (the various html tags wrapper, the inputs and the labels) but without the tag, As you can see this is the result (testing with RSpec, but in the browser is the same thing):
Failure/Error: helper.capture_haml{ helper.front_box_search_html }.should have_xpath("/html/body/div[#*]/form")
expected following text to match xpath /html/body/div[#*]/form:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<body>
<div class= "boxSearch box">
<p class = "sum">search title</p>
<div class = "row string optional">
<label class = "string optional" for="form_search_phrase"> word</label>
<input class="string optional" id="form_search_phrase" name="form_search[phrase]" type="text">
</div>
<div class = "row buttons last">
<input class = "button" id="form_search_submit" name="commit" type="submit" value="search">
</div>
</div>
</body>
</html>
to be honest I have not fully understood how to use various methods of Haml::Helpers, about the documentation is poor.
my env: rails 3.0.7, haml 3.1.4, simple_form 1.5.2
Just a guess but the 'simple_form_for' helper also outputs html so I think it should be:
haml_concat simple_form_for( form_object ||= Form::Search.new, :url => front_search_path ) do |f|
haml_concat f.input :phrase
haml_concat f.button :submit
end
It's not the only remote form I'm using, but I can't see the problem here. It's a simple table where every row has its own form and inline form fields.
<% for temporary_address in #temporary_addresses %>
<tr id="row-<%= temporary_address.id -%>">
<%= form_for temporary_address, :remote => true do |form| %>
<td>
<%= link_to "x", temporary_address, :method => :delete, :remote => true %>
<%= form.submit "s" %>
</td>
[... form fields ...]
<% end %>
</tr>
<% end %>
However, this only works when :remote => true is not used.
resources :temporary_addresses
is in the routes file. Delete links are working and the form is also working when not using :remote => true.
pressing submit with remote => true gives
Started POST "/temporary_addresses/12" for 127.0.0.1 at 2011-07-17 12:45:26 +0200
ActionController::RoutingError (No route matches "/temporary_addresses/12")
and submitting without remote => true works with the following response
Started POST "/temporary_addresses/12" for 127.0.0.1 at 2011-07-17 12:46:15 +0200
Processing by TemporaryAddressesController#update as HTML
Parameters: {[...] "commit"=>"s", "temporary_address"=>{[...]}, "id"=>"12"}
does someone have a clue about this?
EDIT 1
Removed the link_to :remote to destroy a record to check. Same error...
A rendered form looks like this:
<tr id="row-1">
<td>
</td>
<form accept-charset="UTF-8" action="/temporary_addresses/1" class="edit_temporary_address" data-remote="true" id="edit_temporary_address_1" method="post">
<div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" />
<input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="vpFbYO7L9fVtBsWej37FUBobraYjjdWA/F8AjWTwC7A=" />
</div>
<td>
<input id="temporary_address_firstname" name="temporary_address[firstname]" size="30" type="text" value="John" />
</td>
<td>
<input id="temporary_address_lastname" name="temporary_address[lastname]" size="30" type="text" value="Doe" />
</td>
<td>
<input id="temporary_address_email" name="temporary_address[email]" size="30" type="text" value="john.doe#example.com" />
</td>
<td>
New record
</td>
<td>
<input id="temporary_address_submit" name="commit" type="submit" value="save" />
</td>
</form>
</tr>
Solved!
Simple thing.. Firefox rearranges things when they're not valid html. The output given above is the source code. Now that I've seen the generated source code (thanks to web development toolbar) the problem is clear: The hidden form fields, generated in divs with display:none have been moved outside of the form.
So I have this file:
<h1>Calendar view</h1>
<div class="events">
<% #events.each do |e| %>
<%= raw(e.content)%>
<% end %>
</div>
<br />
<div class="messages">
<% #messages.each do |m| %>
<%= raw(m.content)%>
<% end %>
</div>
With #events and #messages being valid instance variables in the controller...but when I go to the page the html looks like this:
<h1>Calendar view</h1>
<div class="events">
<br>
<div class="messages">
This is another message test
</div
Event Content
</div>
I'm confused. Maybe I'm missing something obvious?
The problem is that raw() will output raw HTML content. The Rails template engine will try to merge that with the .erb template you supplied.
Therefore, if either m.content or e.content are malformed, you will most likely get unexpected output.
The best way would be to look for syntax errors, especially missing closing elements.
I am trying to DRY up some of my HTML in my application currently I have a block of HTML that will get re-used multiple times
<div class="block">
<div class="block_head">
<div class="bheadl"></div>
<div class="bheadr"></div>
<h2>Configuration Needed</h2>
</div>
<div class="block_content">
<div class="message warning">
<p>You have not create an admin user yet</p>
</div>
</div>
<div class="bendl"></div>
<div class="bendr"></div>
</div>
What I would like to do is to create a partial or something along those lines and be able to pass in the content to the block header and content
Does anyone know of a way to do this in rails 3
The way i do it is to have a views/shared folder. Then, i create partials inside and i call them like :
<%= render "shared/flash_error", :error => flash[:error] %>
where shared/flash_error is :
<% if error %>
<%= error %>
<% end %>
If you want to have your partials in the partial folder, use this syntax :
<%= render :partial => "partials/your_partial", :locals => { :error => flash[:error] } %>