how to use self.response_body in a view - ruby-on-rails-3

I want to stream a large amount of text from a controller to a view in real time and was pointed to using the following code as an example of how to handle the streaming
def home
self.response_body = proc {|resp, out|
10.times do |x|
out.write "count = #{x}"
sleep 1
end
}
Now this code works in so much the content streams to a browser with 1 line appearing every second, however it doesn’t use the home.html.erb view I created instead it only renders a blank page with the streaming data on it
I’ve tried to embed it using various bits of erb code but cant get it to work and I’ve had a hunt around the web and cant see any clue as how to do this
Can anyone help?
I'm using Rails 3.0.7 and ruby 1.9.2, in dev I'm using unicorn as the rails server which handles the streaming
Cheers
Mike

If you set the response_body in the controller it will not render the view. So I'm afraid you have to pick one or the other. In fact, I've received an exception when I tried to send a response to the client via two different methods. In my case it was send_data and self.response_body = ...

Related

Empty admin view in custom objects after adding records using REST with "ääöö" characters

Just learned that Scandinavian characters 'öäå' prevents Admin view to show ANY content of that class.
Clean table
Created a record from Admin panel with 'ööää', works just fine
Added a record from my app with 'ooaa', works just fine
Image: http://www.flickr.com/photos/106346333#N02/10435376765/
Return JSON:
{"CLOSED_BY_USER":false,"FRIENDLY_NAME":"ooaa: Code","LAST_ACTIVITY":1382510280,"PRO_TEAM":null,"SHARED_SECRET":"salaisuus","SHARING_PROFILE":2,"STARTED":1382510281,"_id":"52676ed8535c12d655000b39","_parent_id":null,"created_at":1382510296,"updated_at":1382510296,"user_id":604824,"permissions":{"read":{"access":"open"},"update":{"access":"owner"},"delete":{"access":"owner"}}}
Added a record from my app with 'ööää' successfully, but Admin view is forever corrupted from now on.
Image: http://www.flickr.com/photos/106346333#N02/10435376175/
Note: Bottom Label states: Showing 1 of 3
Return JSON:
{"CLOSED_BY_USER":false,"FRIENDLY_NAME":"ööää: Code","LAST_ACTIVITY":1382510485,"PRO_TEAM":null,"SHARED_SECRET":"salaisuus","SHARING_PROFILE":2,"STARTED":1382510485,"_id":"52676fa0535c12d655000bad","_parent_id":null,"created_at":1382510496,"updated_at":1382510496,"user_id":604824,"permissions":{"read":{"access":"open"},"update":{"access":"owner"},"delete":{"access":"owner"}}}
The behavior is the same with: Opera, Chrome and IE
Please I really hope this issue to be fixed, since hiding complete content of a class makes developing / debugging really hard and only way to fix it is to delete the parent record (user). And that is totally out of the question when the App is live.
Best,
Janne

creating mobile version of rails app

I'm trying to work out how to redirect mobile users to other views in my rails app, but I'm missing something as it's not loading the mobile view from my device
in application_controller.rb, I added:
def check_for_mobile
session[:mobile_override] = params[:mobile] if params[:mobile]
prepare_for_mobile if mobile_device?
end
def prepare_for_mobile
prepend_view_path Rails.root + 'app' + 'views_mobile'
end
def mobile_device?
if session[:mobile_override]
session[:mobile_override] == "1"
else
# Season this regexp to taste. I prefer to treat iPad as non-mobile.
(request.user_agent =~ /(iPhone|iPod|Android|webOS|Mobile)/) && (request.user_agent !~ /iPad/)
end
end
helper_method :mobile_device?
then I have a file app/views/views_mobile/guidelines/index.html.erb
When I go to the index page on my iPhone it doesn't load the mobile index view - I'm not sure which part I'm missing...
Redirection for this kind of issue is a bad idea. Try responsive design. I would start with twitter bootstrap. This will give you a scaffold system to start with that will adjust to different screen sizes.
Also this is not uniquely a ruby on rails issue. This is a UI design issue.
Here's some good ideas.
Dont duplicate your views. it will just be harder to maintain down the road.
Use css media queries in your css to adjust your styles.
Try not to make two sites but one site that can bend and flex as it needs to.
All that being said I did not answer your specific question but instead tried to show you the way your going about it is wrong so I will understand if you dont mark this as correct.
Responsive resources.
http://twitter.github.io/bootstrap/
http://neat.bourbon.io/
http://www.abookapart.com/products/responsive-web-design
http://www.w3.org/TR/css3-mediaqueries/
Unless you left it out of your code snippet, I don't see where you're calling you're calling your check_for_mobile method. You probably need to add a before_filter to application.rb like:
before_filter :check_for_mobile
I also suggest you check out Ryan Bates Railscast on how to incorporate mobile-specific views into your Rails app. His strategy is similar to what you're trying to achieve, but instead of using a subfolder for your views he solves the same problem very elegantly by creating a 'mobile' MIME type. Check it out here:
http://railscasts.com/episodes/199-mobile-devices?view=asciicast

Multi-step form in Rails 3 with Paperclip attachments

I'm creating a multi-part form in the style that Ryan Bates describes here:
http://railscasts.com/episodes/217-multistep-forms
http://asciicasts.com/episodes/217-multistep-forms (text-based version)
To summarize, I have one view (with a bunch of partials for each form step), and the form variables are stored in a session when the user clicks a next button and a different part of the form is displayed.
One of my form steps allows the user to upload several images via the Paperclip gem. The problem with that is that Rails is trying to upload the image data to the session, which is returning TypeError "can't dump File".
What is a good way to go about this?
UPDATE:
I've tried a bunch of gems (wizardly, acts_as_wizard, and some other smaller ones) but none of them seem to work with Rails 3.
I've also tried just storing the data in an array until the form is complete, but that was causing my controller to get huge and messy.
Saving models into the session is working unless you want to save a File into the session. The wizard plugins are using the session to store models between the steps. They do not produce errors on valid models in my case only on invalids.
So clearing out the attached file sounded a good idea, but in my case clearing out the paperclip attachment with Attachment#clear was not enough because it still wanted to save some File.
I've found out that the problem was with the #queued_for_write attribute in Attachment which still contained the data.
So the following two lines solved my problem:
unless #model.valid?
#model.image.clear
#model.image.queued_for_write.clear
end
This was a paperclip bug and was corrected in this commit.

Rails3 UJS response not evaluated

It gives me the creeps,i'm done, i need some help here, i reverted multiple times back but i can't find the error.
Simple controller (customers),a simple form for adding a customer via :remote => true and the controller does respond_to do |format| { format.js } . Works fine, renders my create.js.rjs template.
I work for a few hours without making any javascript changes or changes to my controllers or authorization etc.. and when i try it again it's not working anymore.
What i mean with not working: Controller gets called, record saved, all partials rendered. But no javascript evaluated, not even a simple alert(1) at the beginning of the file.
I tried with different prototype.js versions and different rails.js versions, but nothing helped. I hope someone has a clue about this or already experienced this.
It's not that i don't want to post code. But it won't help. Its basic code that works and, after some changes where i don't know what i really changed (some locales here, some css there, html templates from a completely different controller a bit..)..
Currently developing with: ruby 1.9.2, rails 3.0.3, prototype 1.7 RC3, rails.js from github.
SOLVED
How stupid, I missed the part where the template naming changed. My application templace was named "application.rhtml". It worked until now. As it stopped to work, I changed it to "application.html.erb" and now it's working.

Rails and mongoid: What is going on with RC7 and embedded documents?

Until now I was using rc6 and I decided to upgrade, but it's totally
breaking my app ? Maybe I am doing something wrong, but I believe I
followed the documentation.
I have a model Content that embeds_many Localized_Content.
Once I have a content created and wanted to added a localized content
I would do the following:
#content = Content.find('xxx')
#new_content = #content.localized_contants.build()
#new_content.save
This is working perfectly fine under rc6 and updates correctly all the
timestamps in localized_contant (using include Mongoid::Timestamps)
But doing the same thing in rc7 break with the following error:
"Access to the collection for LocalizedContent is not allowed since it
is an embedded document, please access a collection from the root
document."
Ok, maybe I need to save directly from the parent content then ok.
Doing a
#content.save
works but will not trigger all the timestamping
and this breaks the logic of my apps... what should I do ?
#content.save is the way to go. You should refactor your code to call save() on the parent object instead of the embedded document.