Users can send a reply to feedback they received. Here is the form:
<%= form_for :feedback, :url => reply_feedback_path do |f| %>
<%= f.text_area :reply, :size => '66x7' %><br>
<%= f.submit "Reply" %>
<% end %>
Here is the controller:
#reply = params[:feedback]
UserMailer.reply2_comments(#to_whom, #from_whom, #reply).deliver
If someone types in 'yo' into the text box, what is passed to the mailer is ' {"reply"=>"yo"} '
I'm having trouble with the syntax to extract just the content that was typed.
Thanks.
It looks like you're passing a hash to the mailer, and you just want the value for the key "reply". So try:
#reply = params[:feedback] || {}
UserMailer.reply2_comments(#to_whom, #from_whom, #reply['reply']).deliver
The main thing I changed here was changing #reply to #reply['reply'] in the mailer call
(I also added a nil-check to the first line to make sure #reply['reply'] won't cause an error if they don't submit the form by normal means)
Related
I am new to rails and was wondering if someone could show me some light...
I have a simple form with couple of input fields and need to display field validation messages below the field name. Is there is a straightforward way to say display errors below??? or do i have to check for each field error message and create a span tag?
You can specify in your simple_form.rb initializer file with which tag your error message will be wrapped:
b.use :error, :wrap_with => { :tag => :span, :class => :error }
Also you can disable default error component on the input and print it by yourself like this:
<%= simple_form_for #user do |f| %>
<%= f.input :name, error: false %>
<%= f.error :name %>
<%= f.submit %>
<% end %>
and style your error message like you want.
If there are semantic errors in the form (mostly from external API), I'd like to add an explanatory message, like so:
<%= semantic_form_for #order, :url => checkout_purchase_url, :html => {:class => 'payment'}, :wrapper_html => { :class => "field" } do |f| %>
<% if f.has_errors? %>
<p>There were errors that prevented your order from being submitted. If you need assistance, please contact us toll-free at <strong>1-800-555-5555</strong>.</p>
<%= f.semantic_errors %>
<% end %>
<% end %>
However, has_errors? is a protected method. Is there a way that I can do this? Thanks.
If you have nested attributes you won't see any errors associated with them. To ensure you get all base errors and any nested attributes errors. Make sure your model contains:
validates_presence_of :nested_object
validates_associated :nested_object
and in your form:
f.semantic_errors *f.object.errors.keys
Not as hard as I thought. I fixed it by checking for errors on the object instead of the form:
<% if #object.errors.any? %>
<p>There were errors that prevented your order from being submitted. If you need assistance, please contact us toll-free at <strong>1-800-555-5555</strong>.</p>
<%= f.semantic_errors %>
<% end %>
Thanks for those who viewed.
For completeness, here's an alternative approach if you want to show similarly helpful messages on each field:
= f.label :title
- if f.object.errors.any?
.error = f.object.errors[:title].flatten.join(' and ')
= f.text_field :title
This gives a nicely formatted and easily-styled list of errors for each field. (You can use semantic_errors instead of object.errors if you prefer, same result.)
I have a form that takes date range value to generate a report with Jasper Reports
reports/statistic.html.erb
<%= form_tag("/reports/statistic", :method => "post", :target => "_blank") do %>
<%= label_tag(:from_date, "From Date:") %>
<%= text_field_tag :from_date %>
<%= label_tag(:to_date, "To Date:") %>
<%= text_field_tag :to_date %>
<br><br>
<%= submit_tag("Generate Report") %>
<% end %>
and this is reports_controller.rb
def statistic
#details=StatisticTable.where(:dateindb => (params[:from_date])..(params[:to_date])
send_doc(render_to_string(
:template => 'reports/statistic.xml', :layout => false), #source of xml and template
'/statistic/detail', #xml xpath2 query in reports
'statisticreport', #name of .jasper file
'StatisticReport', #name of pdf file
'pdf')
end
When I clicked Generate Report button, the report displays nicely in a pdf viewer in a new window. But when I try to save the pdf file, the pdf is empty and the date value is returned as null.
Also, I followed this tutorial http://oldwiki.rubyonrails.org/rails/pages/HowtoIntegrateJasperReports which explains where the send_doc method comes from.
I don't think the problem is in Jasper because if I replace this
#details=StatisticTable.where(:dateindb => (params[:from_date])..(params[:to_date])
with a pre-defined date value
#details=StatisticTable.where(:dateindb => ('2011-12-01')..('2011-12-31')
the report displays and saves perfectly. So I'm guessing there is something wrong with my Ruby on Rails variables setting?
Thanks!
I solved this by only changing form method to GET
<%= form_tag("/reports/statistic", :method => "get", :target => "_blank") do %>
<%= label_tag(:from_date, "From Date:") %>
<%= text_field_tag :from_date %>
<%= label_tag(:to_date, "To Date:") %>
<%= text_field_tag :to_date %>
<br><br>
<%= submit_tag("Generate Report") %>
<% end %>
and after report is generated in PDF, I clicked Save and the PDF saves the whole data perfectly.
I try to update my settings through a form but the update function is not called when I submit. It redirects to edit_settings_path when I submit and as per serve log update is not called. Why?
<%= form_tag settings_path, :method => :put do %>
<p>
<%= label_tag :"settings[:default_email]", "System Administrator" %>
<%= text_field_tag :"settings[:default_email]", Settings['default_email'] %>
</p>
<span class="submit"><%= submit_tag "Save settings" %></span>
<% end %>
Controller
class SettingsController < ApplicationController
def update
params[:settings].each do |name, value|
Settings[name] = value
end
redirect_to edit_settings_path, :notice => "Settings have been saved." }
end
end
** Update **
Update is now called properly (edited controller). Server log confirms Settings Load (0.2ms) SELECT "settings".* FROM "settings" WHERE "settings"."thing_type" IS NULL AND "settings"."thing_id" IS NULL AND "settings"."var" = ':default_email' LIMIT 1
UPDATE "settings" SET "value" = '--- 1111aaa2222...', "updated_at" = '2011-12-18 21:03:21.782075' WHERE "settings"."id" = 2
However it doesn't save to the Db and have no clue why. I'm using the Rails-settings gem 'git://github.com/100hz/rails-settings.git'
Don't know where to check since it says it updated record but in fact no.
why are you using the form_tag method?
If you are just trying to make a standard update form, use:
<%= form_for(#settings) do |f| %>
FORM CODE
<%= end %>
Your controller uses the edit method to render the view and the update method for the calback (to interact with the model)
If you insist on using
<%= form_tag setting_path, :method => :put do %>
Normally you would use the singular word if you are working on a member and the plural if you are working on an collection.
fyi: I dont know what your design is like, but i would have a model settings and a model settings_item...
I'm new to Rails and I've just spent another hour Googling and not finding an example.
So I have a simple form that I need to submit to an API. So I tried submitting it to the API directly but got advice that I do it in my app so I can do something with the results. Anyway, here's my simple form:
<%= form_tag(:action => 'submit') do |f| %>
<%= f.text_field :email, :value => "Your email address...", :class => "text", :id => "email", :name => 'email',
:onFocus => "change(this,'#222222'); this.value=''; this.onfocus=null;",
:size => "26" %>
<%= f.hidden_field :ref_code, :id => 'ref_code', :name => 'ref_code', :value => #referralid %>
<%= submit_tag "Enter To Win", :class => "button-positive submit" %>
<% end %>
Everything I'm seeing has forms that that use a model, I have no need to persist this data, just pass it on to the API.
So my thought was I just create an action in the home controller, where this page lives and have the action submit to it but I get a RoutingError and it's: No route matches {:action=>"submit", :controller=>"home"}
So what do I need to put in the Routes.rb? I tried:
namespace :home do
resources :submit
end
No Joy... I'm sure it's simple but I just can't find the right example.
I think that you should have a look at the ruby guides, it's very well explained (but I don't think it talks about API) and it will save you a lot of time in the future, I swear.
Not sure what you mean but I see some wired stuff, so I hope to be useful, but if you're following some tutorials from the net let us know the link.
Basically what I do is always to call an action of a controller (MVC), following this way you should have a controller (?? apis_controller ??) and call one action of it.
So you want to use form_tag instead of form_for because you're not addressing a model, therefor you want to get rid of f. and use suffix _tag (api).
<%= form_tag(send_api_path) do %>
<%= text_field_tag :email, "Your email address..." %>
<%= hidden_field_tag :ref_code, #referralid %>
<%= hidden_field_tag :api_name, 'your_api_name' %>
<%= submit_tag "Enter To Win" %>
<% end %>
Then, in your apis_controller.rb you create an action send where you send and manage your request.
#apis_controller.rb
def send
# TODO: your code here
end
Another thing is to set the routes, something like
#routes.rb
match 'apis/send' => 'apis#send', :as => :send_api
I'm sure this is not 100% working code, but it should be useful
How to call the api? I had I fast look and found this.
When you ask for help it's always good to attach the error you get, this makes it easier for people to understand the problem.