Cant send form with html content - ruby-on-rails

Hello I have a really annoying problem with my rails 3.0 form. I'm using CKEditor to let the user edit html content of their page. Everything was working fine in rails 2 he made a lot of pages with html content. But then I needed to update the rails gem to 3.0.1 since the was a need for that (server admins updated to rails 3 and removed 2).
After this change He wanted to edit the pages for a new season (tourism oriented business). Some of the pages could't be updated I get a
403 Forbidden
Access to this resource on the server is denied!`
It drives me nuts, I tried every solution that could be relevant but still no luck.
I figure it could has to do with the html content submitted with form because when I delete the content and put some simple paragraph text it works OK. But I have no idea what could it be.
Here's a sample form
<form accept-charset="UTF-8" action="/pages/113" class="jNice" id="edit_page_113" 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="w5ZUDHYHnFKHhLQc6UO/a3zwbN5p6JMgKYUWlVhbMiU=" /></div>
<fieldset>
<p>
<label for="page_category_id">Kategoria:</label>
<select id="page_category_id" name="page[category_id]"><option value=""></option>
<option value="13">School And Study Tours Groups</option>
<option value="23" selected="selected">Regular & Group Tours To Poland </option>
<option value="14">Jewish History And Heritage Tours</option>
</select>
</p>
<p>
<label for="page_name">Nazwa podstrony:</label>
<input id="page_name" name="page[name]" size="30" type="text" value="Poland in a week - the Southern face" />
</p>
<p>
<label for="page_cities">Miasta imprezy:</label>
<input id="page_cities" name="page[cities]" size="30" type="text" value="Warsaw - Krakow - Wieliczka - Auschwitz - Czestochowa" />
</p>
<p>
<label for="page_keywords">Słowa kluczowe:</label>
<input id="page_keywords" name="page[keywords]" size="30" type="text" value="Poland tour, tour of Warsaw, tours of Cracow, czestochowa Jasna gora tour" />
</p>
<p>
<label for="page_description">Skrócony opis:</label>
<input id="page_description" name="page[description]" size="30" type="text" value="2012 tour to Poland with visit to Wieliczka Salt Mine and Czestochowa " />
</p>
<p>
<label for="page_tour_of_the_month">Tour of the month na stronie głównej?:</label>
<input name="page[tour_of_the_month]" type="hidden" value="0" /><input id="page_tour_of_the_month" name="page[tour_of_the_month]" type="checkbox" value="1" />
</p>
<p>
<label for="page_html_body">Zawartość:</label>
<textarea class="ckeditor" cols="40" id="page_html_body" name="page[html_body]" rows="20">test</textarea>
</p>
<p>
<label for="page_published">Opublikowana?:</label>
<input name="page[published]" type="hidden" value="0" /><input checked="checked" id="page_published" name="page[published]" type="checkbox" value="1" />
</p>
<p>
<label for="page_show_request_link">Pokaż link rezerwacyjny?:</label>
<input name="page[show_request_link]" type="hidden" value="0" /><input checked="checked" id="page_show_request_link" name="page[show_request_link]" type="checkbox" value="1" />
</p>
</fieldset>
<p><input id="page_submit" name="commit" type="submit" value="edytuj" /></p>
</form>
Here's the content of the routes.rb file
Gfp3::Application.routes.draw do
resources :newscasts do
get :publish, :on => :member
end
resources :links
resources :payments
resources :fronts
resources :categories do
collection do
:sort
end
end
resources :pages do
get :publish, :on => :member
end
resources :menus
match 'login',:to=>'index#login'
match 'logout', :to=>'index#logout'
match 'images', :to=>'images#index'
match 'images/upload', :to=>'images#upload'
match 'front_page',:to=>'front_page#index'
match 'search',:to=>'index#search'
match ':controller(/:action(/:id(.:format)))'
root :to => 'index#index'
match ':id',:to=>"pages#show"
end
Theese are the gems used in the app
gem 'mongrel'
gem 'rails', '3.0.1'
gem 'haml'
gem "will_paginate", "3.0.pre2"
gem 'mysql',"2.8.1"

Look like routes wrong defined or not defined

Related

How to fix No route matches [GET] "/pages/send_form" routing error?

I am setting up so a user is able to send an email using the contact us form. I have created a file called "send_form" so once the submit button is clicked, the message automatically displays "Thanks for contacting us".
In the past, I have tried to include different routing in the routes.rb for example, "pages#send_form" and "contact#send_form".
# Example of regular route:
get 'about' => 'pages#about'
get 'contact' => 'pages#contact'
get 'news' => 'pages#news'
get 'show' => 'topics#index'
get 'send_form' => 'pages#send_form'
get 'new' => 'booking#new'
views/pages/contact.html:
<h1>Any enquiries:</h1>
<center><form name="htmlform" method="get" action="/pages/send_form">
<table width="700px">
<form id="contact_form" action="#" method="POST" enctype="multipart/form-data">
<div class="row">
<label for="name">Your Name:</label><br />
<input id="name" class="input" name="name" type="text" value="" size="30" /><br />
</div>
<div class="row">
<label for="email">Your Email:</label><br />
<input id="email" class="input" name="email" type="text" value="" size="30" /><br />
</div>
<div class="row">
<label for="message">Your Message:</label><br />
<textarea id="message" class="input" name="message" rows="7" cols="30"></textarea><br />
</div>
<input id="submit_button" type="submit" value="Send email" />
</form>
views/pages/send_form
<h1>Thanks for contacting us,</h1>
I expect the output to be "Thanks for contacting us" once the submit button is clicked. However, I am receiving the error of "No route matches [GET] "/pages/send_form"
Change get 'send_form' => 'pages#send_form' to get 'pages/send_form' => 'pages#send_form'.
Or instead change the action of your form to action="/send_form"

How to dynamically generate a PayPal button with Rails?

I have an e-commerce website with many different products with different prices, and I want to allow people to buy them using PayPal. This guide details how to create a PayPal Buy Now button, but you have to manually fill out a form. This would be very very time consuming to do every time I add a product.
I looked at the HTML of the Buy Now button, hoping to be able to inject my product information with ERB, but nowhere in the HTML is the product information like price listed. Instead, there's a very long "value" element, and I assume the product information is encrypted within this.
Anyone know how to dynamically generate a PayPal button with Rails? If not, does anyone know of am alternative I could use?
You can inject the data as below:
ERB sample (in case you are sending one product):
<li>
<b>product name:</b> <%= #product.name %>
<br>
<b>product price:</b> <%= #product.price %>
<b style='text-decoration: underline;'>Pay:</b>
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post" >
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="yourbusinessmail#hotmail.com*">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="item_name" value="<%= #product.name %>">
<input type="hidden" name="item_number" value="<%= #product.id %>">
<input type="hidden" name="amount" value="<%= #product.price %>">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="button_subtype" value="products">
<input type="hidden" name="no_note" value="0">
<input type="hidden" name="shipping" value="<%= #product.shipping %>">
<input type="hidden" name="add" value="1">
<input type="hidden" name="bn" value="PP-ShopCartBF:btn_cart_LG.gif:NonHostedGuest">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_cart_LG.gif" border="0" name="submit" >
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
</li>
Use below Paypal website link to customize your button then you can inject values as in Ruby code:
Design Paypal button

Rails access uploaded file

I'm building a site on rails and backbone. On the front end I have a simple form:
<form action="/api/users" method="post">
<input type="file" name="profile_image" />
<input type="submit" value="submit" />
<input type="hidden" name="id" value="1">
<input type="hidden" name="method" value="put">
<input type="hidden" name="authenticity_token" value="<%= csrf_token %>">
</form>
When I post this form and print params[:profile_image] from my UsersController the line
logger.debug params[:profile_image].class
just returns
String.
Where's the file?
For what it's worth, I'm using carrierwave, but don't want to mount an uploader. I would just like to pass a file to myUploader.store!.
You need to set enctype on your form in order to submit files. See http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2
Example
<form action="/api/users" enctype="multipart/form-data" method="post">

Selecting multiple checkboxes with Capybara, weird behaviour

I am making an acceptance test on Ruby on Rails 3.0.11 with Capybara 1.1.2, and trying to select two checkboxes in a form.
Here is the view code for the checkboxes (using haml 3.1.3 and simple_form 2.0.4):
= f.input :countries,
:collection => MusicTeaser.teaser_countries,
:as => :check_boxes
The collection looks like this:
[["AT", "at"], ["CH", "ch"], ["DE", "de"], ["ZA", "za"]]
And the HTML generated:
<div class="input check_boxes optional">
<label class="check_boxes optional control-label">Countries</label>
<label class="checkbox">
<input class="check_boxes optional" id="music_teaser_countries_at" name="music_teaser[countries][]" type="checkbox" value="at">AT
</label>
<label class="checkbox">
<input class="check_boxes optional" id="music_teaser_countries_ch" name="music_teaser[countries][]" type="checkbox" value="ch">CH
</label>
<label class="checkbox">
<input class="check_boxes optional" id="music_teaser_countries_de" name="music_teaser[countries][]" type="checkbox" value="de" checked="">DE
</label>
<label class="checkbox">
<input class="check_boxes optional" id="music_teaser_countries_za" name="music_teaser[countries][]" type="checkbox" value="za">ZA
</label>
<input name="music_teaser[countries][]" type="hidden" value="">
</div>
The code to select them looks like this (pretty straightforward):
check 'music_teaser_countries_de'
check 'music_teaser_countries_za'
I expected that, on submit, the countries param would look like this:
params[:music_teaser][:countries] = ["de", "za"]
But it appears as this:
params[:music_teaser][:countries] = ["deza"]
So it seems like Capybara is doing something wrong when selecting more than one checkbox. Ideas?

error (div class="fieldWithErrors") workaround?

In my project, I have a register form.
My HTML looks like
<form method="post" action="/users">
<div style="margin: 0pt; padding: 0pt;">
<input type="hidden" value="lDHrKRC2ENhRKcaoBR4XGfzri/MY09PjqVDvHRtC0D4=" name="authenticity_token"/>
</div>
<p><label for="login">Login</label>
<input type="text" size="30" name="user[login]" id="user_login"/></p>
<p><label for="email">Email</label>
<input type="text" size="30" name="user[email]" id="user_email"/></p>
<p><label for="password">Password</label>
<input type="password" size="30" name="user[password]" id="user_password"/></p>
<p><label for="password_confirmation">Confirm Password</label>
<input type="password" size="30" name="user[password_confirmation]" id="user_password_confirmation"/></p>
<p><input type="submit" value="Sign up" name="commit"/></p>
</form>
Now generating an error by submitting an the empty form my code looks like:
<form method="post" action="/users">
<div style="margin: 0pt; padding: 0pt;"><input type="hidden" value="lDHrKRC2ENhRKcaoBR4XGfzri/MY09PjqVDvHRtC0D4=" name="authenticity_token"/>
</div>
<p><label for="login">Login</label>
</p><div class="fieldWithErrors"><input type="text" value="hg" size="30" name="user[login]" id="user_login"/></div>
<p><label for="email">Email</label>
</p><div class="fieldWithErrors"><input type="text" value="gh" size="30" name="user[email]" id="user_email"/></div>
<p><label for="password">Password</label>
</p><div class="fieldWithErrors"><input type="password" value="gh" size="30" name="user[password]" id="user_password"/></div>
<p><label for="password_confirmation">Confirm Password</label>
<input type="password" value="gh" size="30" name="user[password_confirmation]" id="user_password_confirmation"/></p>
<p><input type="submit" value="Sign up" name="commit"/></p>
</form>
this will destroy my layout.
Is there a solution doing
<input type="text" value="gh" size="30" name="user[email]" class="fieldWithErrors" id="user_email"/>
instead of
<div class="fieldWithErrors"><input type="text" value="gh" size="30" name="user[email]" id="user_email"/></div>
?
Create a new initializer, for example, /config/initializers/field_error.rb:
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag|
if html_tag =~ /<(input|textarea|select)[^>]+class=/
class_attribute = html_tag =~ /class=['"]/
html_tag.insert(class_attribute + 7, "fieldWithErrors ")
elsif html_tag =~ /<(input|textarea|select)/
first_whitespace = html_tag =~ /\s/
html_tag[first_whitespace] = " class='fieldWithErrors' "
end
html_tag
end
Ryan Bates covered this in Railscast episode 39
This is an error in how Rails shows form errors. This is an old and known issue with Rails: https://rails.lighthouseapp.com/projects/8994/tickets/1626-fieldwitherrors-shouldnt-use-a-div
Why it still hasn't been solved is a mystery to me. I guess it's one of those many little things that you need to override manually in Rails. Definitely try John Topley's workaround, though. Or fix it in your local Rails code and submit that as patch to Rails.
Maybe you should consider using formtastic http://github.com/justinfrench/formtastic.
The Railscast is the way to go but you need to update it for Rails 3.x.
add this to the end of your environment.rb
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag|
"<span class='field_error'>#{html_tag}</span>".html_safe
end
Bounce the rails application and you'll be set.

Resources