I'm trying to understand why my Rails action caching isn't caching based on proper URL requests & params.
For example, when I request page/2, the cache system returns page.
This is a sample of the logs for the odd response:
Started GET "/clips/page/2" for 127.0.0.1 at 2012-08-11 16:46:42 -0400
Processing by ClipsController#index as HTML
Parameters: {"page"=>"2"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Cache read: views/localhost:3000/clips/page
Read fragment views/localhost:3000/clips/page (0.3ms)
Rendered text template within layouts/application (0.0ms)
Completed 200 OK in 50ms (ActiveRecord: 0.2ms)
What I think is strange is that clearly the URL being asked for is /clips/page/2, whereas the response is reading "fragment" Read fragment views/localhost:3000/clips/page (0.3ms).
In my controller I have:
caches_action :index, :layout => false
and a very simple index action:
def index
#clips = current_user.clips.page(params[:page]).order('created_at DESC')
respond_to do |format|
format.html # index.html.erb
format.json { render json: #clips }
end
end
My development.rb config file, where I'm testing this has cachine enabled like so:
# caching
config.perform_caching = true # cache test
config.action_controller.perform_caching = true # cache test
# config.action_controller.perform_caching = false # default
config.cache_store = :dalli_store, 'localhost:11211'
I'm reading through this tutorial on caching: http://broadcastingadam.com/2012/07/advanced_caching_part_1-caching_strategies/
And the example there, almost identical to my situation gives the proper response:
Started GET "/posts/2" for 127.0.0.1 at 2011-05-01 16:54:43 -0700
Processing by PostsController#show as HTML
Parameters: {"id"=>"2"}
Read fragment views/localhost:3000/posts/2 (0.5ms)
Rendered posts/show.html.erb within layouts/application (6.1ms)
Write fragment views/localhost:3000/posts/2 (0.5ms)
Completed 200 OK in 16ms (Views: 8.6ms | ActiveRecord: 0.1ms)
OK, I just tested a change to my routes, and realized that the problem was due to the way the routes asked for an optional page id!
So this will not work:
get "/clips/page(/:page)", :controller => "clips", :action => "index"
While this will work:
get "/clips/page/:page", :controller => "clips", :action => "index"
Related
I have a link to logout.
<a href="http://mydomain.lvh.me:3000/users/sign_out" data-method="delete" rel="nofollow">
Sign Out
</a>
Which correctly executes the sign out action:
Started DELETE "/users/sign_out" for 127.0.0.1 at 2013-02-08 15:04:06 -0200
Processing by DeviseCustom::SessionsController#destroy as HTML
Parameters: {"authenticity_token"=>"WYMm9e4VMNgdDFXbl59TKyylhX+rZlWbWMF8lVEeves="}
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 150 LIMIT 1
(0.1ms) BEGIN
(0.2ms) COMMIT
Redirected to http://mydomain.lvh.me:3000/
Completed 302 Found in 4ms (ActiveRecord: 0.5ms)
I am customizing SessionsController but I am not redefining the destroy action.
class DeviseCustom::SessionsController < Devise::SessionsController
respond_to :html, :js
layout :false
def create
<MYCUSTOMCONTENT>
end
end
But after doing all this, current_user still holds the information of the logged user.
Any help?
Ensure that you've included the default javascript files in your layout. There should be a line like:
<%= javascript_include_tag :defaults %>
in your layout.
need a logout via GET then add this to your
config/initializers/devise.rb:
config.sign_out_via = :get
In a Rails app I have Users who can have notes created under them in each user show view.
From the show view notes can be added and edited. The edit link routes properly to the edit path for each note. Clicking save to update the note redirects back to the user show view.
Here is my notes controller:
def update
#note = Note.find(params[:id])
redirect_to user_path(#note.user)
end
However, I am trying to update a note entry and in the console I am seeing that it is not updating for some reason. There should be UPDATE step between BEGIN and COMMIT, but it seems to be missing here.
Started PUT "/notes/2" for 127.0.0.1 at 2013-02-01 01:50:25 -0800
Processing by NotesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Wr+urG+6QvsbPuFpVGUEEqc8QVYiu5q8j389YmOi6Zg=", "note"=>{"author"=>"MZ", "note"=>"Test"}, "id"=>"2"}
Note Load (0.2ms) SELECT "notes".* FROM "notes" WHERE "notes"."id" = $1 LIMIT 1 [["id", "2"]]
(0.2ms) BEGIN
(0.1ms) COMMIT
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Redirected to http://localhost:3000/users/1
Completed 302 Found in 37ms (ActiveRecord: 3.8ms)
What is the reason for why there is no UPDATE step?
You are not updating the attributes. You must call update_attributes and pass the params to be updated.
def update
#note = Note.find(params[:id]) #find the note
if #note.update_attributes(params[:note]) #update the note
redirect_to #note.user #if attributes updated redirect to #note.user
else
render :edit #if not, render the form
end
end
Try this, You missing the update_attributes. This is the proper way to call the update method. You will get a flash message if the update was successful.
def update
#note = Note.find(params[:id])
respond_to do |format|
if #note.update_attributes(params[:note]) # you need to make sure about the :note
format.html { redirect_to user_path(#note.user), notice: 'Notes was successfully updated.' }
else
format.html { render actino: "edit" }
end
end
end
I used the jQuery fileupload plugin to upload multiple pictures. I set up the plugin as:
$(function(){
$('#fileupload').fileupload({
url: "/polls",
dataType: "html"
});
})
The PollsController(in short):
def new
authenticate
#poll = Poll.new
respond_to do |format|
format.html
end
end
def create
#poll.nil? ? #poll = Poll.create(params[:poll]) : #poll.update_attributes(params[:poll])
if #poll.errors.full_messages.empty?
respond_to do |format|
format.html{ redirect_to #poll }
end
else
flash[:"alert-error"] = #poll.errors.full_messages
respond_to do |format|
format.html{ redirect_to '/polls/new' }
end
end
end
I was testing the validation, so I got after submit the form:
...
(0.3ms) BEGIN
(0.3ms) ROLLBACK
Redirected to http://localhost:3000/polls/new
Completed 302 Found in 644ms (ActiveRecord: 1.1ms)
Started GET "/polls/new" for 127.0.0.1 at 2012-11-20 16:27:31 -0800
Processing by PollsController#new as HTML
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 18 LIMIT 1
Rendered polls/_template.haml (0.1ms)
Rendered polls/_jQuery_fileupload_plugin.haml (5.9ms)
Rendered polls/new.haml within layouts/application (14.3ms)
Rendered sessions/_signin.haml (3.9ms)
Rendered layouts/_header.haml (5.3ms)
Rendered layouts/_footer.haml (0.6ms)
Completed 200 OK in 42ms (Views: 38.0ms | ActiveRecord: 0.6ms)
But the page was not actually reloaded.Why did it happen?
Thanks
I'm trying to save records in a database. Many of the column values will be taken from a http request to an api.
I have a text field and some check boxes along with the session_id in the form which will be persisted. this is posted from another controller (form_tag('results#store'))to an action called store in the results controller.
Store action:
def store
query = query_preprocesser(params[:query]) # this is in a helper
## example of resArray ##
#resArray = [[{:engine => "bing, :results => [{:Description => "abc", :Title => "def"}, {:Description => "ghi", :Title => "jkl"}]}, {etc},{etc} ],[{:eng...}]]
resArray = getResults(query) # Also a helper method returning an array of up to 3 arrays
resArray.each do |engine|
db_name = engine[:engine]
engine[:results].each do |set|
res = Result.new(
:session_id => params[:session_id],
:db_name => db_name,
:query => query,
:rank => set[:Rank],
:description => set[:Description],
:title => set[:Title],
:url => set[:Url] )
res.save!
end
end
res.each do |res|
res.save
end
#Result.new(:session_id => params[:session_id], :db_name => "Bing", :query => "Whats the story", :query_rank => 1, :title => "The Title", :description => "descript", :url => "www.google.ie",:query_number => 1)
respond_to do |format|
format.html { redirect_to pages_path }
format.json { head :no_content }
end
end
My routes are resources :results
Server logs:
[2012-07-10 23:30:48] INFO WEBrick 1.3.1
[2012-07-10 23:30:48] INFO ruby 1.9.3 (2012-04-20) [x86_64-linux]
[2012-07-10 23:30:48] INFO WEBrick::HTTPServer#start: pid=15584 port=3000
Started POST "/results" for 127.0.0.1 at 2012-07-10 23:31:36 +0100
Connecting to database specified by database.yml
Processing by ResultsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/nTJOF5Ab+XnL+jxRBHnTJz45YRVmgbf55bmn5/iz8E=", "query"=>"search term", "button"=>"", "searchType"=>"Seperate", "bing"=>"1", "session_id"=>"e08c13a99f21a91520fcc393e0860c94"}
(0.2ms) begin transaction
(0.2ms) rollback transaction
Rendered results/_form.html.erb (13.9ms)
Rendered results/new.html.erb within layouts/application (23.6ms)
Completed 200 OK in 213ms (Views: 78.4ms | ActiveRecord: 2.9ms)
Form:
<%= form_tag('results#store') do %>
Rake routes:
store POST /pages(.:format) results#store
results GET /results(.:format) results#index
POST /results(.:format) results#create
new_result GET /results/new(.:format) results#new
edit_result GET /results/:id/edit(.:format) results#edit
result GET /results/:id(.:format) results#show
PUT /results/:id(.:format) results#update
DELETE /results/:id(.:format) results#destroy
store POST /pages(.:format) results#store
pages GET /pages(.:format) pages#index
I keep getting redirected to the new action in Results controller. The helper methods aren't even being executed. I'm a past master at over complicating things, can anyone help unravel this for me?
Do you have a route set up for the store action? If you do then you should use the url helper for it in your form tag:
form_tag( results_store_url )
I'm trying to understand why respond_with/to is rendering the wrong view...
controller
respond_to :html, :js
def get_numbers
Rails.logger.info request.format
#numbers = Number.all
respond_with #numbers
end
when making an ajax request, the rails log shows the format is JS, and the request format is text/javascript, but it renders the html view.
log
Started GET "/numbers/get_numbers?_=1333564838110" for 127.0.0.1 at 2012-04-04 11:40:38 -0700
Processing by NumbersController#get_numbers as JS
...
text/javascript
Rendered numbers/get_numbers.html.haml within layouts/application (106.4ms)
and i have both a get_numbers.html.haml & get_numbers.js.coffee view in views/numbers
i could render the correct view by doing:
respond_with #numbers do |format|
format.js {}
end
but shouldn't it be rendering the js view with just respond_with #numbers
If you want the response to be JavaScript when no format is explicitly specified (meaning no .html or .js in the URL), you can set the default format parameter in your route:
match '/numbers/get_numbers(.:format)' => 'numbers#get_numbers', :defaults => { :format => :js }
You might also get your desired results by switching the order of the formats in your call to respond_to, since it seems to default to the first one, but I haven't tested that.