ruby-on-rails

Is there a shortcut for sending AJAX commands?

So, My app uses a ton of AJAX, and I find myself doing this a lot: respond_to do |format| format.js do render :update do |page| #actual code end end end is there a shortcut? ...

Ruby on Rails: + AJAX: How do I set the value of a field

there is page.replace_html "id", thing to replace it with but is there something where I can just insert a value into a text field? ...

On Ruby on Rails, how do we print debug info inside of a controller?

In PHP, CGI, or in RoR's View, we can easily print out debug information. How about in the Controller, how can we just say, print "hello world" (to the webpage output) and return to continue with the view, or stop the controller right there? ...

Stubbing A Model Attribute

I am writing rspec test for my Cars class, and have a question regarding setting up mocks. I'd like to stub the parts array in Cars, how can i do that? I have the following code: class Cars has_many :parts def heavy_count parts.inject(0) { |sum, v| v.weight > 10 ? sum + 1 : sum } end end With test context ("#heavy_count")...

Understanding assert_difference in ruby-on-rails.

Could anyone please explain what this test code do: assert_difference('Post.count') do post :create, :post => { :title => 'Hi', :body => 'This is my first post.'} end and... assert_difference 'ActionMailer::Base.deliveries.size', +1 do post :invite_friend, :email => '[email protected]' end I cant understand it even though I ...

Clean URLs Using forward slash '/' in to_param with Rails (3)

Is this possible? def to_param "#{id}%2F#{slug}" end This works in Chrome and Safari, but if Firefox you see the "%2F" in the address bar. Is there a cleaner way? ...

rails eager loading :select

I have model called VoteTopic with the following associations belongs_to :user belongs_to :category has_many :comments has_many :vote_items, :dependent => :destroy has_many :votes, :through => :vote_items I use Searchlogic gem to query for @vote_topics in the index action as follows.. scope_procedure :all_approved, lambda {status_equ...

How to store an id in a model class

This should be an easy one. I'm not sure exactly how to title the question though. I have a collection of modules that can be added to my CMS (page, link, form...) they're all associated via a polymorphic association to one table that manages common things like ancestry and whether they're enabled etc... Because I'm making a 'page' for e...

Paperclip with MongoMapper in Rails 3

I'm trying to implement Paperclip in my first rails app, and I happen to be using rails 3 and mongodb with mongomapper. I followed this guide on getting there things to all work together http://www.bencurtis.com/2009/08/paperclip-and-mongomapper/ Just as the blog post suggests, I've put paperclip into the config/initializers directory,...

How do I access form element values using ruby without submitting the form?

Okay I have an extensive ruby application running behind Ruby on Rails right now. This application needs form input but I don't want to refresh the page when I update the text box. What I mean is: I have a form that has two text boxes, one is called "starttime" and one is called "endtime". I somehow need to get the value of these text b...

Setting up actions for the ActionController in Rails

I just issued a [ script/generate scaffold User ] command to generate my files. All the CRUD pages work well so that's good and now I'm trying to create more pages. I create a method in the user_controller.rb file as follows: def login #blahblah end Then created app/views/users/login.html.erb for the view. When I tried accessing it t...

authlogic current_user tracking

I use authlogic for authentication and paperclip for handling user's profile picture attachments. I use the following method to get the current_user def current_user_session return @current_user_session if defined? (@current_user_session) @current_user_session = UserSession.find end def current_user re...

How to make a render :edit call show the /edit in the address bar

Hi, In the update action of my preferences controller in my Rails app, if there are any errors in the validation/save etc there's a call to: format.html { render :edit } Nothing too unusual there - however, when this code is hit, the address in the browser changes and loses the /edit in the URL. For example: To start with, my bro...

How would you store a business's hours in the db/model of a Rails app?

I'm creating a Rails app that will store the opening and closing hours for a business. Originally, I thought of simply using a text data type and letting it be free-form: "Monday to Friday 9am to 5pm Saturday 11am to 4pm Closed Sundays" But, requirements have changed and I need to check the hours against the current date & time and di...

Single page Rails app

I'm writing a 4chan-like imageboard in Rails. It's my first Rails app after going through Michael Hartl's Rails tutorial, and I'm not sure how to organize my controllers. I have a controller for posts, and a controller for pages. Is this how you would organize it? Do I even need a pages controller if I just want everything to happen on ...

Storing dynamic lengths of time on the database in ruby on rails

Okay, so I need something like this: time_span = "1.month" date = DateTime.now date = date + send("#{time_span}") where time_span is actually stored on the database, but that doesn't seem to work.. Here's some console action: $ rails c >> time_span = 1.month => 1 month >> date = DateTime.now + time_span => Fri, 27 Aug 2010 20:51:...

Rails action not rendering

Here is my index.html.erb: <%= render :action => 'new' %> <% unless @posts.empty? %> <%= render @posts %> <% end %> The posts are displaying, but nothing in the new page is. It's not in the log either: Processing PostsController#index (for 127.0.0.1 at 2010-07-27 20:54:28) [GET] Post Load (0.2ms) SELECT * FROM "posts" Renderin...

ruby on rails ajax update css on page

Does anyone know how I can update the style attribute of a division using an Ajax request? For example, I have a division I inline style to display:hidden, and when a user clicks a link it changes the style to display:block ...

using ajax in rails with periodically_call_remote

I am attempting to reload a partial on a page periodically but it does not seem to be working. The partial loads correctly when I first load the page but it will not update when I introduce new info to it without reloadig the page. The view looks like: <div id="menuarea"style="height: 399px; width: 181px"> <%=periodically_call_remot...

Rails join tables or HABTM associations

In the application I'm building, I want to have Events. People can then register for the event by creating a team, by directly registering for an event (no team), or by joining an existing team. I originally set it up as Event has_many teams, Team has_many users, user belongs_to Team and Event. However, I foresee problems if a registe...