I have my search set up like so.
# Viewings controller
def index
#rmquery = params[:search]
if params[:search]
#viewings = Viewing.search(params[:search])
else
#viewings = Viewing.all
end
end
and
class Viewing < ActiveRecord::Base
belongs_to :user
belongs_to :room
def self.search(query)
self.where("room_id like ?", "%#{query}%")
end
end
and I have my (viewings) index.jbuilder as follows:
json.array!(#viewings) do |viewing|
json.title "viewing"
json.start viewing.start_time
json.end viewing.end_time
json.url viewing_url(viewing, format: :html)
end
I would like my viewings.json to reflect the search results, but it only seems to display all viewings, even though the search is working under where it says "Listing Viewings"
Ive tried something along the lines of:
#viewings = Viewing.where(room_id: #rmquery)
json.array!(#viewings) do |viewing|
json.title "viewing"
json.start viewing.start_time
json.end viewing.end_time
json.url viewing_url(viewing, format: :html)
end
but it doesn't seem to pass the query through.
oh, and my index view:
<p id="notice"><%= notice %></p>
<%= form_tag(viewings_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search rooms" %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<div id="calendar"> </div>
<h1>Listing Viewings</h1>
<table>
<thead>
<tr>
<th>Room</th>
<th>Customer</th>
<th>Start time</th>
<th>End time</th>
<th>Notes</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #viewings.each do |viewing| %>
<tr>
<td><%= viewing.room_id %></td>
<td><%= viewing.customer_id %></td>
<td><%= viewing.start_time %></td>
<td><%= viewing.end_time %></td>
<td><%= viewing.notes %></td>
<td><%= link_to 'Show', viewing %></td>
<td><%= link_to 'Edit', edit_viewing_path(viewing) %></td>
<td><%= link_to 'Destroy', viewing, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Viewing', new_viewing_path %>
Code for calendar:
$(document).ready(function() {
// page is now ready, initialize the calendar...
$('#calendar').fullCalendar({
events: '/viewings.json',
defaultView: 'agendaWeek',
slotMinutes: 60,
})
});
server log for search
C:\Sites\weddings
=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2015-02-24 22:40:04] INFO WEBrick 1.3.1
[2015-02-24 22:40:04] INFO ruby 1.9.3 (2014-02-24) [i386-mingw32]
[2015-02-24 22:40:04] INFO WEBrick::HTTPServer#start: pid=8260 port=3000
Started GET "/viewings?utf8=%E2%9C%93&search=1" for ::1 at 2015-02-24 22:40:14 +
0000
←[1m←[36mActiveRecord::SchemaMigration Load (1.0ms)←[0m ←[1mSELECT "schema_mi
grations".* FROM "schema_migrations"←[0m
Processing by ViewingsController#index as HTML
Parameters: {"utf8"=>"V", "search"=>"1"}
←[1m←[35mViewing Load (1.0ms)←[0m SELECT "viewings".* FROM "viewings" WHERE (
room_id like '%1%')
Rendered viewings/index.html.erb within layouts/application (52.0ms)
←[1m←[36mUser Load (1.0ms)←[0m ←[1mSELECT "users".* FROM "users" WHERE "user
s"."id" = ? ORDER BY "users"."id" ASC LIMIT 1←[0m [["id", 1]]
Completed 200 OK in 1216ms (Views: 1100.8ms | ActiveRecord: 4.0ms)
Started GET "/assets/fullcalendar-6bee23c458d42683dba9159c94f7547f.css?body=1" f
or ::1 at 2015-02-24 22:40:15 +0000
[2015-02-24 22:40:15] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/application-4a810b319e4a8061f2f2b27a579eadc1.css?body=1" fo
r ::1 at 2015-02-24 22:40:15 +0000
[2015-02-24 22:40:15] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/forms-2477fa28d6be1997f320e98a5ceee0f6.css?body=1" for ::1
at 2015-02-24 22:40:15 +0000
[2015-02-24 22:40:15] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/index-divs-e5867ca540c81afa8a01e2daca2920c5.css?body=1" for
::1 at 2015-02-24 22:40:15 +0000
[2015-02-24 22:40:15] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/owl.carousel-688ba12698d3c02b724b8a26bc3b1bd0.css?body=1" f
or ::1 at 2015-02-24 22:40:15 +0000
[2015-02-24 22:40:15] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/owl.theme-f91781dace0c67e4d6a689ddd73d65b7.css?body=1" for
::1 at 2015-02-24 22:40:15 +0000
[2015-02-24 22:40:15] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/owl.transitions-1b9d47a45c85e9fa3f2f990b6d59519a.css?body=1
" for ::1 at 2015-02-24 22:40:15 +0000
[2015-02-24 22:40:15] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/pages-8450695447adc4c25ef72b73f84e12b5.css?body=1" for ::1
at 2015-02-24 22:40:15 +0000
[2015-02-24 22:40:15] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/show-bd6702d041298d2548881c1db5e4e9a7.css?body=1" for ::1 a
t 2015-02-24 22:40:15 +0000
[2015-02-24 22:40:16] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/viewings-64e62ddc273c2f5847f30d698ca14b67.css?body=1" for :
:1 at 2015-02-24 22:40:16 +0000
[2015-02-24 22:40:16] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/moment-66988874bf5714cb1e4a6f53d733b6e1.js?body=1" for ::1
at 2015-02-24 22:40:16 +0000
[2015-02-24 22:40:16] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/jquery-87424c3c19e96d4fb033c10ebe21ec40.js?body=1" for ::1
at 2015-02-24 22:40:16 +0000
[2015-02-24 22:40:16] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/jquery_ujs-e27bd20a10d28155845a22d71ef94f2f.js?body=1" for
::1 at 2015-02-24 22:40:16 +0000
[2015-02-24 22:40:16] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/turbolinks-da8ddad9c2c5483d4c5c30c1ce325253.js?body=1" for
::1 at 2015-02-24 22:40:16 +0000
[2015-02-24 22:40:16] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/fullcalendar-f09a2cee6bd3f7655c47cf7eab2b52b9.js?body=1" fo
r ::1 at 2015-02-24 22:40:16 +0000
[2015-02-24 22:40:16] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/owl.carousel.min-7972660fd0b56886b322fbde7ad2f1f7.js?body=1
" for ::1 at 2015-02-24 22:40:16 +0000
[2015-02-24 22:40:16] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/pages-fcec5b5a277ac7c20cc9f45a209a3bcd.js?body=1" for ::1 a
t 2015-02-24 22:40:16 +0000
[2015-02-24 22:40:16] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/rmcats-fcec5b5a277ac7c20cc9f45a209a3bcd.js?body=1" for ::1
at 2015-02-24 22:40:16 +0000
[2015-02-24 22:40:16] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/rooms-fcec5b5a277ac7c20cc9f45a209a3bcd.js?body=1" for ::1 a
t 2015-02-24 22:40:16 +0000
[2015-02-24 22:40:16] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/viewings-fcec5b5a277ac7c20cc9f45a209a3bcd.js?body=1" for ::
1 at 2015-02-24 22:40:16 +0000
[2015-02-24 22:40:16] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/assets/application-5fac6df9a34c6823e27885b0701b0037.js?body=1" for
::1 at 2015-02-24 22:40:16 +0000
[2015-02-24 22:40:16] WARN Could not determine content-length of response body.
Set content-length of the response or set Response#chunked = true
Started GET "/viewings?start=2015-02-22&end=2015-03-01&_=1424817616261" for ::1
at 2015-02-24 22:40:17 +0000
Processing by ViewingsController#index as JSON
Parameters: {"start"=>"2015-02-22", "end"=>"2015-03-01", "_"=>"1424817616261"}
←[1m←[35mViewing Load (1.0ms)←[0m SELECT "viewings".* FROM "viewings"
Rendered viewings/index.json.jbuilder (30.1ms)
Completed 200 OK in 65ms (Views: 59.3ms | ActiveRecord: 1.0ms)
I'm not sure what all those "Could not determine content-length of response body" messages are, but I think the last 3 lines above seem to be whats causing it to show all viewings in the calendar.
You probably need to tie the search function to calling the calendar function in events.js.coffee:
$(document).ready ->
$("#calendar").fullCalendar(
events: '/events.json'
)
Not sure how to do this, but you want to trigger this function after each search, because you are getting the correctly filtered list but the calendar is not reloading with the current list of events.
I'm starting to think maybe you would be better off just having a main calendar that shows everything, and instead of a search function, use a dropdown menu of links that has every room listed. You could then just nest your routes:
in config/routes.rb
...
resources rooms do
resources events
end
That way your links to each room would go to that room's calendar in a route like
localhost:3000/room1/events
since Events already belong to rooms. In your rooms controller you can have the #show action use the room id to find the events:
def show
#room_events = Room.where(id: room_params[:id]).events
end
Then in /views/rooms/show you would put the calendar code.
The dropdown for choosing rooms would be built on the fly from a query
#rooms = Room.all
and then just use a dropdown menu in .erb to get the links for each room.
EDIT:
I am not familiar with jbuilder, so my answer may not be any help. Is your intention to update the index page after it has already loaded once?
I still think your problem is that you are defining the variable #viewings twice, once in the partial and once in the index. The last call is to index so that is the final value when it displays.
Ok, so looking at the code for your calendar:
$(document).ready(function() {
// page is now ready, initialize the calendar...
$('#calendar').fullCalendar({
events: '/viewings.json',
defaultView: 'agendaWeek',
slotMinutes: 60,
})
});
I'm wondering if this is where your issue is, maybe you need to load the updated events from /viewings.json. So either use AJAX to request the filtered events query, then update the calendar from an .js.erb partial and an .html.erb partial? If the calendar is based on a table and you are using Jquery you could use the filter functions built into Jquery.
Or...
You could use CSS. I'm not good at CSS but I'm pretty sure you could set all the events as visible and use the search function as a filter to change the visibility of everything to invisible except for the ones that go to the room you are filtering for.
This is a stab in the dark with the limited info to go on but try changing your instance variable for the room-limited viewings from
#viewings
to something like:
#room_viewings
That way you are not stomping on it when you call
render index
Related
I have 2 Rails apps, App1 and App2.
App1 contains links to controller actions in App2 which in turn returns files (downloads), e.g.:
<%= link_to "Download Video", "http://app2/downloads/download_video", :target => "_bkank" %>
with "downloads" being and controller within App2 and "download_video" being an action of the "downloads" controller:
def download_video
send_file '/path/to/video/video.mp4', type: "video/mp4", :disposition => "inline"
end
Upon clicking the link, a new window (tab) is opened and the video is played in the window - this is a desired behavior .... my problem is, when looking t the logs, I see that the same GET request is sent 3 times - the first as HTML and then as / twice:
Started GET "/downloads/download_video" for ::1 at 2018-04-19 16:26:45 +0200
Processing by DownloadsController#download_video as HTML
Sent file /path/to/video/video.mp4 (0.2ms)
Completed 200 OK in 1ms
Started GET "/downloads/download_video" for ::1 at 2018-04-19 16:26:46 +0200
Processing by DownloadsController#download_video as */*
Sent file /path/to/video/video.mp4 (0.1ms)
Completed 200 OK in 1ms
Started GET "/downloads/download_video" for ::1 at 2018-04-19 16:26:46 +0200
Processing by DownloadsController#download_video as */*
Sent file /path/to/video/video.mp4 (0.1ms)
Completed 200 OK in 0ms
Could anyone let me know why this is happening and how I can prevent it?
Thanks,
Jon.
#praga2050,
can you please post entire controller code ...
yes:
class DownloadsController < ApplicationController
def download_video
send_file '/var/www/videos/video.mp4', type: "video/mp4", :disposition => "inline"
end
end
#praga2050,
... try putting Rails.logger.debug with time stamp and see
yes:
Started GET "/downloads/download_video" for ::1 at 2018-04-20 13:59:42 +0200
Processing by DownloadsController#download_video as HTML
Log Date:2018-04-20T13:59:42+02:00
Sent file /path/to/video/video.mp4 (0.2ms)
Completed 200 OK in 1ms
Started GET "/downloads/download_video" for ::1 at 2018-04-20 13:59:42 +0200
Processing by DownloadsController#download_video as */*
Log Date:2018-04-20T13:59:42+02:00
Sent file /path/to/video/video.mp4 (0.2ms)
Completed 200 OK in 1ms
Started GET "/downloads/download_video" for ::1 at 2018-04-20 13:59:42 +0200
Processing by DownloadsController#download_video as */*
Log Date:2018-04-20T13:59:42+02:00
Sent file /path/to/video/video.mp4 (0.1ms)
Completed 200 OK in 1ms
When these partials are loaded in the browser I am unable to navigate away from the page via the browser back button. The page stays the same but the url changes.
What could be causing this?
match '/signup', to: 'users#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'get'
# /users/new
def new
#user = User.new
render partial: "new", layout: false
end
# /sessions/new
def new
render partial: "new", layout: false
end
Here's my stack. This is from just clicking the signin link once.
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Processing by SessionsController#new as */*
Processing by SessionsController#new as */*
Rendered sessions/_new.html.erb (0.8ms)
Rendered sessions/_new.html.erb (0.8ms)
Completed 200 OK in 4ms (Views: 3.3ms | ActiveRecord: 0.0ms)
Completed 200 OK in 4ms (Views: 3.3ms | ActiveRecord: 0.0ms)
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Processing by SessionsController#new as HTML
Processing by SessionsController#new as HTML
Rendered sessions/_new.html.erb (0.8ms)
Rendered sessions/_new.html.erb (0.8ms)
Completed 200 OK in 3ms (Views: 1.9ms | ActiveRecord: 0.0ms)
Completed 200 OK in 3ms (Views: 1.9ms | ActiveRecord: 0.0ms)
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Started GET "/signin" for 127.0.0.1 at 2014-03-26 19:57:46 -0700
Processing by SessionsController#new as HTML
Processing by SessionsController#new as HTML
Rendered sessions/_new.html.erb (0.8ms)
Rendered sessions/_new.html.erb (0.8ms)
Completed 200 OK in 2ms (Views: 1.6ms | ActiveRecord: 0.0ms)
Completed 200 OK in 2ms (Views: 1.6ms | ActiveRecord: 0.0ms)
Do you have a before filter redirecting public access to sessions#new? Have you defined /signin as a route?
I might be wrong, but as I wrote in the comment while ago I had a customer that used Turbolinks on Rails 3.2.12 and a group of customized JS libraries. The Turbolinks was causing a lot of issues such as double submitting and one of them was a similar thing to what you have mentioned. I had 3 choices:
1- Take out Turbolinks (my temp solution, until I showed the team TL was causing it)
2- Change the JS lib dependencies
3- Update the Rails App to 4.x latest. (I did this finally, it wasn't too bad)
In case if I was wrong and your case and setup was not similar to mine, here is a good thread on Github that some guy tries to solve it and still seems to be open.
https://github.com/rails/turbolinks/issues/256
Check your parameters :
Processing SESSION::SESSION#new (for 127.0.0.1 at 2014-04-04 13:50:03) [POST]
Parameters: {"authenticity_token"=>"XXXXX=", "param1"=>"1"}
Well in your log you will see what params you are sending if it is not sending what you want create params according to your columns or what you want to keep.
If you want create a back button you could try this:
link_to_function "Back", "history.back()"
Check your action form maybe you are sending other url
<% form_tag :controller=>"sessions",:action=>"new" do %>
<% #user.each do |user| %>
<%= user.name %>
<%= user.lastname %>
<% end %>
<% end %>
It seems that my "create" action isn't sending the information that I am entering in my "new" view. I have a route called
localhost:3000/zips
I have a view for the new page
<h1>
Zip Code Eligibility <font color="green">Add Zip Code</font>
</h1>
<p>
To <span class="hi">ADD</span> a zip code from the local delivery service, please enter the 5 digit zip code (6 in Canada) and click submit.
</p>
<%= form_for #zip do |z| %>
<p><%= z.label :zip %> <%= z.text_field :zip %> |
<%= z.label :state %> <%= z.text_field :state %>
</p>
<p><%= z.submit "Add Zip Code" %></p>
<% end %>
This is my zips_copntroller
class ZipsController < ApplicationController
def index
#zips = Zip.all
end
def show
#zip = Zip.find(params[:id])
end
def new
#zip = Zip.new
end
def create
#zip = Zip.new(params[:post])
if #zip.save
redirect_to zips_path, :notice => "Zipcode was saved sucessfully"
else
render "new"
end
end
def edit
end
def update
end
def destroy
end
end
Now when I enter "91202' and then 'CA' I click submit...it submits and redirects to the index view
<h1>
Pick-up/Delivery Eligibility
</h1>
<h3>
Based on U.S. Zipcode, Canadian Postal Code
</h3>
<p class="el">
To find out if you qualify for 4overs local delivery service,
please enter your <nobr>U.S. 5 digit zip code, or</nobr> your
<nobr>Canadian 6 character Postal Code,</nobr> then click submit.
<h4>Current Eligible Zip Codes</h4>
<!-- Possible sample code for the display page when I get around to the search function -->
<% #zips.each do |zip| %>
<!--"link_to" creates a link...first param is what you want to "display"...the second parameter
is what you want to link to here it's a shortcut called "zip" (which passes the entrie POST and Rails will create the page with it's contents)
-->
<p><%= link_to zip.zip, zip %>|<%= zip.state %></p>
<hr />
<% end %>
<p><%= link_to "Add a New Zip", new_zip_path %></p>
But it doesn't display the zip and state I have entered ... it displays this (image attached)
Here is the dump of the log...
Started POST "/zips" for 192.168.188.50 at 2013-08-03 12:51:42 -0700
Processing by ZipsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"wP8eOqMzOjLfLZjmq62EXdO0C59fHyRk3C/Y4b+96mA=", "zip"=>{"zip"=>"91202", "state"=>"CA"}, "commit"=>"Add Zip Code"}
(0.3ms) BEGIN
SQL (0.7ms) INSERT INTO "zips" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", Sat, 03 Aug 2013 19:51:42 UTC +00:00], ["updated_at", Sat, 03 Aug 2013 19:51:42 UTC +00:00]]
(2.6ms) COMMIT
Redirected to http://chrish.sbx.4over.com:3000/zips
Completed 302 Found in 8ms (ActiveRecord: 3.6ms)
Started GET "/zips" for 192.168.188.50 at 2013-08-03 12:51:42 -0700
Processing by ZipsController#index as HTML
Zip Load (0.5ms) SELECT "zips".* FROM "zips"
Rendered zips/index.html.erb within layouts/application (2.7ms)
Completed 200 OK in 8ms (Views: 6.5ms | ActiveRecord: 0.5ms)
Started GET "/assets/application.css?body=1" for 192.168.188.50 at 2013-08-03 12:51:42 -0700
Started GET "/assets/zips.css?body=1" for 192.168.188.50 at 2013-08-03 12:51:42 -0700
Started GET "/assets/jquery.js?body=1" for 192.168.188.50 at 2013-08-03 12:51:42 -0700
Started GET "/assets/zips.js?body=1" for 192.168.188.50 at 2013-08-03 12:51:42 -0700
Started GET "/assets/turbolinks.js?body=1" for 192.168.188.50 at 2013-08-03 12:51:42 -0700
Started GET "/assets/jquery_ujs.js?body=1" for 192.168.188.50 at 2013-08-03 12:51:42 -0700
Started GET "/assets/application.js?body=1" for 192.168.188.50 at 2013-08-03 12:51:42 -0700
Any reason why the :post didn't send the POST information?
I am using Rails 4.0.0, and Ruby 1.9.3p448:
[freedel#chrish.sbx.4over.com localdel]$ rails --version
Rails 4.0.0
[freedel#chrish.sbx.4over.com localdel]$ ruby --version
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-linux]
As log is saying, you will be able to receive form parameters as params[:zip]. So replace params[:post] to params[:zip].
And if you are using rails4, you will have to permit the parameters as you are mass assigning the attributes.
In your controller:
def create
#zip = Zip.new(zip_params)
if #zip.save
redirect_to zips_path, :notice => "Zipcode was saved sucessfully"
else
render "new"
end
end
...
private
def zip_params
params.require(:zip).permit(:zip, :state)
end
Since these are just two attributes, You can change your create method to this & it will work:
def create
#zip = Zip.new(zip: params[:zip][:zip], state: params[:zip][:state] )
if #zip.save
redirect_to zips_path, :notice => "Zipcode was saved sucessfully"
else
render "new"
end
end
But if you have more attibutes use Strong Params
Very simple process I'm trying to implement.
On a home page or landing page I want to capture emails for an email list.
If the email passes validation, it gets saved, Great! Everything works fine when there are no errors.
When there is an error like only inputting an 'a' character as shown in the log below. Or even just an empty string I continually get the same TypeError (can't convert nil to String)
Here is the log:
Started GET "/" for 127.0.0.1 at 2013-06-13 13:48:56 -0400
Connecting to database specified by database.yml
Processing by HighVoltage::PagesController#show as HTML
Parameters: {"id"=>"home"}
Rendered shared/_error_messages.html.erb (0.4ms)
Rendered layouts/_visitor_form.html.erb (7.9ms)
Rendered pages/home.html.erb within layouts/application (13.1ms)
Completed 200 OK in 82ms (Views: 39.6ms | ActiveRecord: 1.8ms)
[2013-06-13 13:48:57] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started POST "/visitors" for 127.0.0.1 at 2013-06-13 13:48:59 -0400
Processing by VisitorsController#create as HTML
Parameters: {"utf8"=>"✓",authenticity_token"=>"esTPNzNtkmNPTe7Jh+E2aDHNrgocU5Z8g49Nj0QiOhQ=", "visitor"=>{"email"=>""}, "commit"=>"Add to newsletter list"}
(0.1ms) begin transaction
Visitor Exists (0.2ms) SELECT 1 AS one FROM "visitors" WHERE LOWER("visitors"."email") = LOWER('') LIMIT 1
(0.1ms) rollback transaction
Completed 500 Internal Server Error in 39ms
TypeError (can't convert nil into String):
app/controllers/visitors_controller.rb:12:in `create'
Here is the visitors_controller.rb
def create
#visitor = Visitor.new(params[:visitor])
if #visitor.save
flash[:success] = "Your email has been added!"
redirect_to '/'
else
render '/'
end
end
I've also tried
#visitor = Visitor.new(:visitor => params[:visitor][:email])
and a few other sequences with no success.
How can I get the unsuccessful saves to gracefully show the validation errors and allow the user to resubmit.
Here is the layout:
<%= form_for Visitor.new do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.submit "Add to newsletter list", class: "btn btn-large btn-primary" %>
<% end %>
Maybe the high_voltage gem is screwing me up. I don't have enough experience to know.
UPDATE:
rake routes
visitors POST /visitors(.:format) visitors#create
home /home(.:format) pages#home
root / high_voltage/pages#show {:id=>"home"}
page GET /pages/*id high_voltage/pages#show
The problem is, render '/' does not render index page. You have to specify which action page you want to be rendered. According to your routes, this line should look like this:
render 'pages/home'
I followed this Geocoder Railscast & I'm playing around with it to see if I can add validation to the longitude & latitude coordinates returned by the Geocoder Gem incase the user inputs a faulty address that does not return any longitude & latitude coordinates.
I was able to get this working to a point with the code below but when I enter a correct address it gives the user a validation error the first time they submit the form, even though longitude & latitude coordinates where returned. The second time the user submits the form it works.
I suspect this is happening as when the user submits the form there are no longitude & latitude coordinates but there are the second time round as the geocoder gem returned the coordinates.
Is there something wrong with my code or should I be approaching this differently?
Model
class Location < ActiveRecord::Base
attr_accessible :address1,
:country,
:latitude,
:longitude,
:name
validates :latitude, :presence => {message: "Not a valid location on Google Maps, please check name address & country fields" }
geocoded_by :address
after_validation :geocode, :if => :address_changed?
def address
[name, address1, country].compact.join(' ')
end
def address_changed?
attrs = %w(name address1 country)
attrs.any?{|a| send "#{a}_changed?"}
end
end
Terminal OutPut
=> Booting WEBrick
=> Rails 3.2.12 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2013-04-02 02:03:41] INFO WEBrick 1.3.1
[2013-04-02 02:03:41] INFO ruby 1.9.3 (2013-02-22) [x86_64-linux]
[2013-04-02 02:03:41] INFO WEBrick::HTTPServer#start: pid=4316 port=3000
Started POST "/locations" for 127.0.0.1 at 2013-04-02 02:04:04 +0100
Processing by LocationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"4HUay+IOgA7JnCSg8ZZ0zVMcHpj7djUPlfqe1emTMSY=", "location"=>{"name"=>"Louch Dan", "address1"=>"Roundwood", "town"=>"", "county"=>"", "state"=>"", "country"=>"Ireland", "latitude"=>"", "longitude"=>""}, "commit"=>"Create Location"}
(0.1ms) begin transaction
(0.1ms) rollback transaction
Rendered locations/_form.html.erb (8.9ms)
Rendered locations/new.html.erb within layouts/application (63.4ms)
Completed 200 OK in 410ms (Views: 156.1ms | ActiveRecord: 6.0ms)
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-04-02 02:04:06 +0100
Served asset /application.css - 304 Not Modified (11ms)
[2013-04-02 02:04:06] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-04-02 02:04:06 +0100
Served asset /jquery_ujs.js - 304 Not Modified (1ms)
[2013-04-02 02:04:06] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-04-02 02:04:06 +0100
Served asset /jquery.js - 304 Not Modified (7ms)
Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at 2013-04-02 02:04:06 +0100
Served asset /scaffolds.css - 304 Not Modified (1ms)
Started GET "/assets/locations.css?body=1" for 127.0.0.1 at 2013-04-02 02:04:06 +0100
Served asset /locations.css - 304 Not Modified (1ms)
[2013-04-02 02:04:06] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
[2013-04-02 02:04:06] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started GET "/assets/locations.js?body=1" for 127.0.0.1 at 2013-04-02 02:04:06 +0100
Served asset /locations.js - 304 Not Modified (4ms)
[2013-04-02 02:04:06] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
[2013-04-02 02:04:06] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-04-02 02:04:06 +0100
Served asset /application.js - 304 Not Modified (7ms)
[2013-04-02 02:04:06] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started POST "/locations" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Processing by LocationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"4HUay+IOgA7JnCSg8ZZ0zVMcHpj7djUPlfqe1emTMSY=", "location"=>{"name"=>"Louch Dan", "address1"=>"Roundwood", "town"=>"", "county"=>"", "state"=>"", "country"=>"Ireland", "latitude"=>"53.07004130000001", "longitude"=>"-6.2804327"}, "commit"=>"Create Location"}
(0.1ms) begin transaction
SQL (15.3ms) INSERT INTO "locations" ("address1", "country", "county", "created_at", "latitude", "longitude", "name", "state", "town", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["address1", "Roundwood"], ["country", "Ireland"], ["county", ""], ["created_at", Tue, 02 Apr 2013 01:04:22 UTC +00:00], ["latitude", 53.07004130000001], ["longitude", -6.2804327], ["name", "Louch Dan"], ["state", ""], ["town", ""], ["updated_at", Tue, 02 Apr 2013 01:04:22 UTC +00:00]]
(71.9ms) commit transaction
Redirected to http://0.0.0.0:3000/locations/10
Completed 302 Found in 236ms (ActiveRecord: 87.3ms)
Started GET "/locations/10" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Processing by LocationsController#show as HTML
Parameters: {"id"=>"10"}
Location Load (3.8ms) SELECT "locations".* FROM "locations" WHERE "locations"."id" = ? LIMIT 1 [["id", "10"]]
Rendered locations/show.html.erb within layouts/application (9.8ms)
Completed 200 OK in 25ms (Views: 15.5ms | ActiveRecord: 3.8ms)
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Served asset /application.css - 304 Not Modified (0ms)
[2013-04-02 02:04:22] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started GET "/assets/locations.css?body=1" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Served asset /locations.css - 304 Not Modified (0ms)
[2013-04-02 02:04:22] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Served asset /jquery_ujs.js - 304 Not Modified (0ms)
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Served asset /jquery.js - 304 Not Modified (6ms)
[2013-04-02 02:04:22] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
[2013-04-02 02:04:22] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Served asset /scaffolds.css - 304 Not Modified (0ms)
[2013-04-02 02:04:22] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Served asset /application.js - 304 Not Modified (43ms)
Started GET "/assets/locations.js?body=1" for 127.0.0.1 at 2013-04-02 02:04:22 +0100
Served asset /locations.js - 304 Not Modified (0ms)
[2013-04-02 02:04:22] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
[2013-04-02 02:04:22] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Why can't you :geocode the Location before validation? :address_changed? will return true even for new records if any of those fields have changed. Validate it and if it returns invalid lat/lng values then your :latitude presence validation will fail.
Just change that after_validation to before_validation.