I'm working with a fairly complex app someone else wrote and its not always entirely clear what variables have/have not been created and made available to the view.
Is there a way to dump all the available view variables to the screen from within the .erb?
It seems like this:
<%= instance_variable_names.inspect %>
Will list all the variables available to the view from within the ERB. 40 minutes of googling and reading paid off.
Building off of Will's answer: you can also do
<% pp instance_variable_names %>
where pp pretty prints the array. If you do it this way, you'll just have to look in the server logs.
Related
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
When I call the $this->renderPartial() from a view it is not working at all.
Code I tried:
$this->renderPartial("_selectedalbums");
the "_selectedalbums" view is in the same directory.
What I am missing, please help.
If you are passing values to the view there might be chances that those variables may not be properly assigned or initialized.
Check if all those passing values are getting values. Also turn your error reporting on and see.
Hope it helped.
Given that the partial view is in the same view folder as the controller you are running you should check your spelling. Case sensitive file systems will require you to be exact (_selectedAlbums vs _selectedalbums).
In my Rails 3 application I'm using Twitter Bootstrap as a frame work for developing an in house project management system. I'm using Formtastic to help me with forms since it save a lot of time and code. My problem is getting the Formtastic code to output the forms in a way that correspond with Bootstrap's conventions. I've read a few items I found on Google suggesting that I should monkey patch Formtastic, but I haven't been able to do this successfully.
How can I customize Formtastic's output to use div's around each field so I can use Bootstrap with it?
Thank you for looking.
Well, today I tried forking formtastic and making it compatible with bootstrap... The markup is incredibly tightly coupled to the code, so I gave up and switched to simple_form instead. Works fine with the advice in Rails: Using simple_form and integrating Twitter Bootstrap
You can use the formtastic-bootstrap gem. You should be able to drop this in and it will generate HTML that will work naturally with Twitter Bootstrap.
If you use the SCSS files from the one of the scss-twitter-bootstrap projects, you can simply comment out or remove the include for the forms part of the CSS.
Simply copy them in to app/stylesheets (Rails 3.0) or app/assets/stylesheets and comment out:
// #import "forms.scss";
Don't forget to add the formtastic CSS back in:
<%= stylesheet_link_tag 'formtastic', 'formtastic_changes' %>
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 = ...
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.