ruby-on-rails

Why I can't change the link_to become link_to remote directly?

I have a link_to like this: <%= link_to 'X', category, :confirm => 'Are you sure?', :method => :delete %> So, I change it like this: <%=link_to_remote category.name, :confirm => 'Are you sure?', :method => :delete%> it show the confirm box, but after I click "yes", it won't delete the item I want , why? ...

Ruby-on-Rails: Validating uniqueness (or number of) child objects

I have a model, Game, which has_many :piles. In fact, I know that each Game has exactly 4 piles, each of which has a different (at the scope of the Game) contents. My web-form for creating a Game allows the user to pick the four contents (as c_type_#). I'm therefore able to populate the Piles at creation of the Game. However, I can't fig...

Adding tags to posts in Ruby on Rails

I am creating a blog in Rails using the Scaffold feature. I would like to add a 'tags' field to each post like on StackOverflow and Wordpress. I can do this with a string type ( scaffold post title:string body:text tags:string ) and then comment seperated but this isn't a good practice, since I also want the reader to be able to browse b...

Smells like ActiveRecord messes object attributes on save

I have got a bunch of records in locations table: ... *************************** 8. row *************************** id: 8 feed: http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/4564.xml ... *************************** 11. row *************************** id: 11 feed: http://feeds.bbc.co.uk/weather/feeds/rs...

De-capistrano a rails app?

I've inherited a rails site that I need to deploy (quickly!) to our webhost, which is a standard *nix shared server that uses FastCGI for rails apps. I've worked with rails sites on multiple occasions in the past but wouldn't consider myself an expert by any stretch. This particular app was developed using capistrano, with which I've g...

Determine the domain in an ActiveRecord model

I am in the middle of migrating my application from using subdirectories for userspace to subdomains (ie. domain.com/~user to user.domain.com). I've got a method in my user class currently to get the "home" URL for each user: class User def home_url "~#{self.username}" # How I'd like to do it for subdomains: #"http://#{se...

How does RoR scaffolding work with compound words?

This is what I still don't understand (but probably should)... if I run this command: script/generate scaffold FooFoo name:string submitted_on:datetime How do I link to a the page that lets you view the list of FooFoo? This doesn't seem to work, and every variant of it that I've tried doesn't work either: <%= link_to "Mylink", foo_fo...

Extend an existing attribute getter in rails model

Hello all, i have a problem using acts_as_textiled and has_foreign_language plugins together. TextElement my model in my app class TextElement < ActiveRecord::Base has_foreign_language :value acts_as_textiled :value HasForeignLanguage def has_foreign_language(*args) args.each do |field| # Define the Getter ...

Column type of 'set' and Rails

I am building a database of online offers. Each offer can be viewed in one or more countries, for instance an advertiser may be interested in reaching offers in the US & Canada. The list of countries we are covering is about 50 long with each country identified with an ISO standard two letter acronym like US, CA, GB, FR, DE, etc. I cou...

How do I use UTF in a Rails URL?

I have the following route in routes.rb: map.resources 'protégés', :controller => 'Proteges', :only => [:index] # # this version doesn't work any better: # map.resources 'proteges', :as => 'protégés', :only => [:index] When I go to "http://localhost:3000/protégés" I get the following: No route matches "/prot%C3%A9g%C3%A9s" with {:met...

Allow mass asignment in certain contexts

I have several Rails models that I'm trying to expose via a REST api. I'm looking for a simple way to allow mass assignment in certain contexts (through the api or admin interface) but to disallow when populating from user based forms. There are a few catches as well. First, I'm populating a bunch of child objects using accepts_neste...

Default way for passing a URL to link_to in Rails

This is a very basic question with probably an easy answer. Let's say I have a model called Product. When I add map.resources :products to my routes.rb I have access to some default paths new_product_path, edit_product_path and so on. As I understand this should be used when linking to a resource e.g. using the link_to helper method:...

rails has_many :through has_many :through

I'm wondering to what extent I can use associations in Rails. Take into consideration the following: class User < ActiveRecord::Base has_one :provider has_many :businesses, :through => :provider end class Provider < ActiveRecord::Base has_many :businesses has_many :bids, :through => :businesses belongs_to :user end ...

Constructing a has-and-belongs-to-many query

Hi, I have a rails app (running on version 2.2.2) that has a model called Product. Product is in a has-and-belongs-to-many relationship with Feature. The problem is that I need have search functionality for the products. So I need to be able to search for products that have a similar name, and some other attributes. The tricky part i...

Pass method chains to to_json

I know you can pass methods the values of which you want to be available to json objects like so: # user.rb def name first_name + last_name end # some controller render :json => @user.to_json(:methods => :name) But if I want to massage the value returned from the method a bit (with a text helper say) is there a way to do that? I g...

Rails - Passing a collection to the layout for will_paginate?

I have my <%= will_paginate %> code block in the layout of my application. I'd like to pass this block different collections depending on what controller/action I'm in. How can I do this? ...

Simple model of blog post

I want to have a site that is a simple blog so I created a model: class Post < ActiveRecord::Base attr_accessible :title, :body end I want to use Markdown but without HTML tags. I also want always to keep database clean and my idea is to use before_save()/before_update() callbacks to sanitise my input and escape HTML. I do...

Rails routing with requirements

Hello reader, with the following routes I try to achive the goal, that I can present static resources like terms of use, imprint and so on in different languages using different urls. I defined two example routes for my imprint like that: map.imprint ':lang/impressum', :controller => "statics", :action => "imprint", :requirements =...

Rails Date compared to Date.today

Hi I am having what seems like a very simple problem. I just can't seem to pin down what I need to do. I have a birth_date that is in the Date format. I want to compare it to Date.today as shown below. The problem is it is coming back false because it wants to compare the year as well. It is a birthday so I don't care about the year ju...

ActiveRecord Nested Includes producing SQL Error.

paginate :per_page => per_page, :page => page, :include => [{:stores => :locations}, :categories, :kosher], :origin => origin, :conditions => ["products.name LIKE ? #{conditions}", "%#{search}%"], :within => distance, :order => sort_order I am using ruby 1.9.1p243 with Rails 2.3.5 on OSX and this cod...