First of all, I'm experimenting with Ruby on Rails for the first time.
I'm doing a simple exercise of designing a form to receive events information and save those events in Google Calendar.
To save the events on Google Calendar I'm using the following gem: http://googlecalendar.rubyforge.org/
I have the following code for the view.
<h1>Welcome to Ruby On Rails Calendar</h1>
<h3>Add Event</h3>
<form action="/google_calendar/create" method="post">
<table>
<tr>
<td>Title</td><td><input type="text" /></td>
</tr>
<tr>
<td>Begin Date</td><td><input type="date" name="begindate" id="bagindate" /><input type="time" name="beginhour" id="beginhour" /></td>
</tr>
<tr>
<td>End Date</td><td><input type="date" name="enddate" id="enddate" /><input type="time" name="endhour" id="endhour" /></td>
</tr>
<tr>
<td>Local</td><td><input type="text" name="local" id="local" /></td>
</tr>
<tr>
<td>Description</td><td><input type="textarea" rows="10" name="description" id="description" /></td>
</tr>
<tr>
<td><input type="Submit" value="Save" /></td>
</tr>
</table>
</form>
<%= #result_message %>
And for the controller I have this code (mostly taken from the gem site I shared previously).
class GoogleCalendarController < ApplicationController
def create
send_event(params[:title],params[:begindate] + " " + params[:beginhour], params[:enddate] + " " + params[:endhour], params[:local], params[:description])
#result_message = 'Event sent successfully'
render :welcome => index
end
private def send_event(title, begin_datetime, end_datetime, local, description)
require 'googlecalendar'
google_calendar_service = GData.new
google_calendar_service.login(Rails.configuration.google_calendar_email, Rails.configuration.google_calendar_password)
event = { :title => title,
:content => description,
:where => local,
:startTime => begin_datetime,
:endTime => end_datetime}
google_calendar_service.new_event(event)
end
end
The thing is that when I try to save an event I get the following error.
uninitialized constant GoogleCalendarController::GData
Supposedly GData is a class defined in the googlecalendar gem, but seems to not being recognized as such.
I have gem 'googlecalendar' on my Gemfile, did bundle install and it appears when I do bundle show googlecalendar.
Does anyone know what can be causing this?
The documentation is little bit wrong
try this
require 'googlecalendar'
google_calendar_service = Googlecalendar::GData.new
Since you didnot specify the namespace so first Ruby searched it in the ::global namespace and didnot find, so expected it to be GoogleCalendarController::GData
References
Code from the gem's lib/googlecalendar.rb, you can see the namespace is Googlecalendar
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
module Googlecalendar
# List of lib files to include
FILES = %w{calendar.rb dsl.rb event.rb gcalendar.rb gdata.rb ical.rb net.rb version.rb}
end
# Add all FILES as require
Googlecalendar::FILES.each { |f| require "googlecalendar/#{f}"}
Related
I use Dojo and want to send two files and one String to a REST Service. The HTML for that is the following:
//...
<script>
dojo.require("dijit.form.Button");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.CheckBox");
dojo.require("dojox.form.Uploader");
dojo.require("dojox.embed.Flash");
if(dojox.embed.Flash.available){
dojo.require("dojox.form.uploader.plugins.Flash");
}else{
dojo.require("dojox.form.uploader.plugins.IFrame");
}
</script>
//..
<form action="http://localhost:8080/service" enctype="multipart/form-data" method="POST" name="inputDataForm" id="inputDataForm">
<table border="1" cellpadding="1" cellspacing="1" height="88" width="925">
<tbody>
<tr>
<td>Dataset1</td>
<td><input name="train" type="file" value="Browse" data-dojo-type="dojox/form/Uploader"/></td>
</tr>
<tr>
<td>Dataset2</td>
<td><input name="test" type="file" value="Browse" data-dojo-type="dojox/form/Uploader"/></td>
</tr>
<tr>
<td>Number of Customers</td>
<td><input name="numberCustomers" size="40" type="text" data-dojo-type="dijit/form/TextBox" /></td>
</tr>
</tbody>
</table>
<p><input name="runButton" type="submit" value="Run" data-dojo-type="dijit/form/Button" id="submitButton" /></p>
</form>
However: If I press the submit button, I can see via Firebug that there are sent TWO POST-Requests. One with the first file and the "Number of Customers" field and one with the second file and the "Number of Customers" field. However, my REST Service wants to have both files and the "Number of Customers" fields in one POST Request. How can i do that? What am I doing wrong?
Thanks a lot in advance
Natan
I actually want to my database entries to be empty each time the app is accessed. But i am not getting any idea regarding this. Below is my code which takes 3 inputs from user and displays them in tabular form but each time I display the table old entries are also getting displayed. I want that once all the entries are displayed the database should get cleared.
<p>enter the name with registration number of students</p>
<form accept-charset="UTF-8" action="/pages/add" method="post">
<input id="nam" name="nam" type="text">
<input id="reg" name="reg" type="text">
<input id="cls" name="cls" type="text">
<input name="commit" type="submit" class="btn" value="Add todo">
</form>
<table width="100%">
<tr>
<th>NAME</th>
<th>REGISTRATION NUMBER</th>
<th>CLASSES ATTENDED</th>
</tr>
<tr>
<% #pages.each do |t| %>
<td><%= t.name_student %></td>
<td><%= t.reg_no %></td>
<td><%= t.classes_at %></td>
</tr>
<% end %>
</table>
I have a "search" page that have some controls and below is the search page code:
<%= form_for :search, :url => { :method => :get, :action => :search } do |f| %>
<table>
<tr>
<td align="center" style="vertical-align:top;">
<h2 style="color:Black; font-size: x-large;">Specs</h2>
<table>
<tr>
<td align="center">
<input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
</td>
</tr>
</table>
<table>
<tr>
<td>
<div class="button">
<input type="submit" name="search" value="Search" class="buttonSearch">
</div>
</td>
</tr>
</table>
</td>
<td align="center" style="vertical-align:top;">
<h2 style="color:Black; font-size: x-large;">
Result
</h2>
<% #user_zip.each do |uzr_zip| %>
<h1><%= uzr_zip.First_Name %></h1>
<% end %>
<table id="searchResult" width="100%" runat="server">
<tr>
<td bgcolor="#CCDBE0">
Image:
</td>
<td bgcolor="#CCDBE0">
<%= f.label(:zip, "Mentor") %>
</td>
</tr>
</table>
</td>
</tr>
</table>
<% end %>
And when I am trying to get the textbox value into the controllers page like below
def search
#students=Students.all
#blah = params[:search][:tf_Zip]
end
render 'search'
end
Then it gave me an error below, at this line #blah = params[:search][:tf_Zip]
undefined method `[]' for nil:NilClass
Kindle help me. Thanks
I think your params[:search] is nil?
so for this you can use
#blah = params[:tf_Zip]
or change your input field like this
<%= f.text_field :tf_Zip %>
or you can use like this
<input type="text" name="search[tf_Zip]" Style="text-align: left;"
BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
Look at your log: you will see what is coming through in params, then you'll see why params[:search][:tf_Zip] doesn't work.
The error is telling you, effectively, that params[:search] is nil, and that you can't call [tf_Zip] on nil.
Your problem is this line:
<input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
It will populate params[:tf_Zip] because it's name is "tf_Zip". If you want it to populate params[:search][:tf_Zip] then you should set the name attribute to search[tf_Zip].
What would be nicer though is to use the rails form field helpers. I don't know why you have so much raw html inside a form_for.
<input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
can be replaced with
<%= f.text_field :tf_Zip %>
which will populate params[:search][:tf_Zip]
For the other attributes (Style etc) you should set these with css. Rails will probably put a class on the field automatically which you can use to do this. The "style" (note lowercase) attribute can be used instead but it's clumsy as it doesn't allow you to restyle the field (and more generally, your site) with css.
Have you checked to see if params[:search] is nil? If it is, then trying to pull [:tf_Zip] will cause that error.
Usually you'd submit the form from one controller action and handle the result in another action.
And your statement end looks misplaced.
I currently have a very simple model "Coupon". I suddenly am interested to add two more fields, "redeem_code" and "redeemed" to the model for obvious reasons.
I would like to make a view for a merchant to redeem a coupon based on a code.
The simplest approach that I can think of to just do this would be something like this:
def redeem
#renders the page
end
def redeem_post
redeem_code = params[:redeem_code]
deal = Deal.find_by_redeem_code(redeem_code)
if deal.nil?
# BUG: Somehow here you will be logged out
redirect_to deals_path
else
if deal.update_attribute(:redeemed, true)
redirect_to deals_path
end
end
end
As far as I am concerned, rails doesn't really support non restful interfaces. I tried digging up form helpers last night but I was not able to find anything so I just put togehter something real fast:
<form action="<%= deals_redeem_post_path %>" method="post">
<table>
<tr>
<th> Code: </th>
<td> <input type="text" name="redeem_code" /> </td>
</tr>
<tr>
<th></th>
<td> <input type="submit" text="Submit" /> </td>
</tr>
</table>
</form>
I am using devise. The bug that I am experiencing right now is when my user submits this form, he/she will be automatically logged out.
Any ideas how I can fix this or implement this more elegantly?
My guess is that you are missing the authenticity token (which prevents Cross Site Request Forgery (CSRF)) which is automatically generated. You want to use the form for helper for that generates this automatically.
<%= form_for(:coupon, :url => deals_redeem_post_path) do |f| %>
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for
I have a view model that has a property that looks like this
Property SelectedGroups() as List(of string)
In the view I have something like this
<table>
<tr>
<th>Description</th>
</tr>
<tr>
<td>
<input type="hidden" name="SelectedGroups" value="one" />
description one
</td>
</tr>
<tr>
<td>
<input type="hidden" name="SelectedGroups" value="two" />
description two
</td>
</tr>
<tr>
<td>
<input type="hidden" name="SelectedGroups" value="three" />
description three
</td>
</tr>
</table>
The table rows are added and removed with jquery. Is there a way to create a validation attribute on SelectedGroups property that will require a minimum number of items for the list? This can be done with javascript but I would like it to work with
<% Html.EnableClientValidation()%>
<%: Html.ValidationSummary(False)%>
You would have to write a custom validator. The built-in validators aren't that sophisticated.
ScottGu has written a good article on custom validators: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx