Render page doesn’t exist Like Twitter in rails 3 - ruby-on-rails-3

How do I render a 404 or page doesn't exist like Twitter does?
I use rails 3.2.3
My code
def rescue_with_handler(exception)
redirect_to '/500.html'
end
def method_missing(id, *args)
redirect_to '/404.html'
end
That works, but I want to be like twitter. For example if I access this page
https://twitter.com/abcd/skka/asafaga
and https://twitter.com/abcd/skka

You have to handle Routing Error with rescue_from.
This question was already answered:
Dynamic error pages in Rails 3
Best way to handle 404 in Rails3 controllers with a DataMapper get
rescue_from NoMethodError
...
Also there is solution here:
https://github.com/rails/rails/issues/671
In any case, try to Google it, you will find a lot of different solutions.

Related

Rails 3 + Devise 2 + JsonP / Callbacks

I'm working on a PhoneGap mobile app that communicates with a rails3 server using a REST api and json. Authentication is done using devise 2.0.4.
For my own controllers I can specify that rails should wrap the json with the callback to handle the Cross-Domain problem by the following:
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #books, :callback => params[:callback] }
end
notice the:
:callback => params[:callback]
I'm unable to do that with the devise controllers.
How can I get devise to respond to js requests - i.e. json with a callback?
Thanks a lot,
Ariel
It would be better to override devise controllers providing the :callback parameter to renderer or using before_filter somehow. In order to do that copy devise controller(s) from github/local-gem-directory to your app and then edit. But it would be much easier to define a JS views for each action:
If you ain't done it already start with generating devise views (which is not necessary but would help you to get the idea):
rails g device:views -s
It would generate lots of *.html.erb files inside of your app/views/devise folder. You have to create appropriate *.js.erb next to them with following contents:
<%=render :inline => params[:callback]+'('+resource.to_json+')'%>
PS: Actually you may just put that line into app/views/application.js.erb and skip other steps;)

Rails: how to redirect to a specific page after signing out with devise

I have a question about using Devise in Rails.
How can I redirect to a specific page after signing out (destroying a user session)?
I tried the following in Application Controller, which does not seem to be working:
def after_sign_out_path_for(resource_or_scope)
root_path
end
Thanks in advance!
That should work according to the wiki.
Maybe you've missed the last line:
You should also override method Devise::Controllers::Helpers#stored_location_for in your application controller, to return nil. This applies to after_sign_in_path_for also. YMMV.

Rails is responding with wrong format

I have the following action:
def something
# do something
respond_to do |format|
format.js
end
end
I also have an something.html.erb template in my controller.
When I GET /controller/something, it returns nothing.
When I GET /controller/something.js, it renders the html with no layout.
When I XHR GET /controller/something, it renders the html with no layout.
I would expect it to "return a template not found error" error instead of rendering the html template.
Is this a bug of Rails or am I doing something wrong?
rails v3.0.4
ruby v1.9.3
I think it's because of the “convention over configuration” principle. If you don't specify a render or redirect_to in the action of the controller, rails renders the view with the same name as the method. Look at Rails render guide

Rails 3 assert_select undefined method matches?

Trying to get started on view testing in Rails 3. I want to validate that I have a form getting kicked out in the view that has the right URL for the action. So I am using assert_select. I actually got a failing test first, using this syntax in the spec (using Rspec):
response.should assert_select "form[action=#{my_model_path}]"
Looking at the rendered HTML, sure enough, the view was rendering the 'edit' url, not the 'new' url due to the wrong model being passed down. Groovy, start red.
I make the model a 'new' one, and I look at my rendered output, and it's what I'd expect, BUT the test fails, and the error message says:
NoMethodError:
undefined method `matches?' for #<Array:0x0000012a1a5d58>
I've looked all over the web for this, found one guy that mentioned the error, but got no resolution. Any ideas?
Figured out that I could get this to work by nesting my assert_select calls, like so:
response.should assert_select "form" do
assert_select "[action=?]", my_model_path
end

Render a page as a saved HTML file using Rails 3.0?

I'm building a simple website generator application in Rails 3.0. I'd like a "publish" action in a controller that works just like an ordinary "show" action, but instead, saves a page as an HTML file in the "public" directory instead of displaying it in the browser. That is, I'd like to use the Rails render mechanism to create a file instead of providing an HTTP response.
What's the best way to do this?
Should I simply add caches_page :publish to the controller?
Or use render_to_string and File.new?
You can use render_to_string method: http://apidock.com/rails/AbstractController/Rendering/render_to_string
You still need to respond from the controller though. Maybe redirect to the page you just saved?
I would go with page caching.
Then if you have editable content, the pages should be automatically generated. You could then write a system task which bundles them up as a web site.
see (whatever)/actionpack/lib/action_controller/caching/pages.rb for instructions.
I found that page caching using caches_page won't work for me because there is no way to show a notification or redirect to another page after caching the page. The method render_to_string (suggested by #Grocery) is the way to go. Here's the example controller code:
def publish
#article = Article.find(params[:id])
html = render_to_string(:template => "articles/template.html.haml", :layout => 'article' )
FileUtils.makedirs("#{Rails.root}/public/articles/") unless File.exists?("#{Rails.root}/public/articles/")
File.open("#{Rails.root}/public/articles/#{#article.filename}.html", 'w') {|f| f.write(html) }
respond_to do |format|
format.html # publish.html.erb
end
end