ruby-on-rails-3

Multi-page Multi-Model Form in Rails

一个人想着一个人 提交于 2019-12-24 00:48:48
问题 I'm trying to create a multi-page form that uses multiple models. I have an applicant and this applicant has more than one address (one to many relationship). I would like the first page to contain information about the applicant, and then the page after that to have the form for the address(es) This is what I have at the moment: applicant.rb has_many :addresses, :dependent => :destroy accepts_nested_attributes_for :addresses address.rb belongs_to :applicant applicants_controller.rb: def new

Is there a way to stop this default insert in rails

两盒软妹~` 提交于 2019-12-24 00:43:59
问题 Ok so i have this data structure class User < ActiveRecord::Base has_many :companies, :through => :positions has_many :positions class Company < ActiveRecord::Base has_many :positions has_many :users, :through => :positions class Position < ActiveRecord::Base belongs_to :company belongs_to :user attr_accessible :company_id, :user_id, :regular_user end And my database structure create_table "positions", :force => true do |t| t.integer "company_id" t.integer "user_id" t.datetime "created_at",

Cannot seed database; not working because of refused connection?

孤人 提交于 2019-12-24 00:43:00
问题 When I try to seed my application I get the error: No connection could be made because the - target machine actively refused it. - connect(2) I believe the reason why is because I was having issues with mysql2 so I uninstalled it along with the MySQL 5.5 Servers and then switched to sqlite3. I think the server for mysql2 is running in the background so this could be the issue. How would I fix this? How would I turn off the Mysql2 local host server or whichever server it is that's causing this

facebox rails app

余生颓废 提交于 2019-12-24 00:36:28
问题 how do I use facebox in a Rails app knowing that I already have the jquery-rails gem installed? Need sample code that goes into the view as well. Thanks. 回答1: Include facebox library in your application.html.erb file. Add this code in a javascript file. jQuery(document).ready(function($) { $('a[rel*=facebox]').facebox() }) View <a href="images/stairs.jpg" rel="facebox">text</a> There are a few simple examples and some documentation on their site https://github.com/defunkt/facebox#usage. 来源:

Rails 3 routing with resources under an optional scope

别来无恙 提交于 2019-12-24 00:25:39
问题 I have setup my versioned API like this with only a small tweak for backwards compatibility. In my routes I have: scope '(api(/:version))', :module => :api, :version => /v\d+?/ do … scope '(categories/:category_id)', :category_id => /\d+/ do … resources :sounds … end end with the successful goal already reached of having the following URL's reach the same place /api/v1/categories/1/sounds/2 /api/categories/1/sounds/2 /categories/1/sounds/2 /sounds/2 My directory structure is like this: The

Can't mass-assign protected attributes when import data from csv file

你离开我真会死。 提交于 2019-12-24 00:25:07
问题 I have a form for import data like this: <%= form_tag({ controller: 'courses', action: :import }, multipart: true) do %> <%= label_tag 'file', 'Import data' %> <%= file_field_tag 'file' %> <%= submit_tag "Import", name: nil, class: 'btn' %> <% end %> This is my import action: def import require 'csv' csv_text = File.read(params[:file].tempfile.to_path.to_s) csv = CSV.parse(csv_text, headers: true ) csv.each do |row| row = row.to_hash.with_indifferent_access Course.create(row.to_hash.symbolize

Rails3 Active Admin: How to display only Open status records when first click on Shipments tag?

此生再无相见时 提交于 2019-12-24 00:21:02
问题 I'm using ActiveAdmin. I have a list of Shipments with status (as a string) of Open and Closed. When the user clicks on the Shipments tab, I want to display only the Open shipments. How can I do that? Of course, the user could later choose to see the Closed shipments by using the filter. But I want the default to initially only show the Open shipments. 回答1: Probably best way will be to create scopes in model. AA automatically gets your scopes and creates tabs above table in index view.

Sort array of strings with special characters

时光怂恿深爱的人放手 提交于 2019-12-24 00:17:29
问题 In Rails 3 how do I sort an array of strings with special characters. I have: [Água, Electricidade, Telefone, Internet, Televisão, Gás, Renda] However when i invoke sort over the array Água gets sent to the end of the array. 回答1: The approach I used when I ran into the same issue (depends on iconv gem): require 'iconv' def sort_alphabetical(words) # caching and api-wrapper transliterations = {} transliterate = lambda do |w| transliterations[w] ||= Iconv.iconv('ascii//ignore//translit', 'utf-8

Rails 3 ActiveRecord sum of a model for each associated model

懵懂的女人 提交于 2019-12-24 00:17:11
问题 I have 2 models Category - id - name and Transaction - id - category_id - amount I want to find the sum of all transactions for each category. I know I can get a list of caterogies and then get the sum for all the transactions with the category_id but it will do 20+ queries. Is there a way to do it all in one query? Edit: I want to end up with a list of [[category1, sum], [category2, sum]]. 回答1: Transaction.group(:category_id).sum(:amount) This will return a hash similar to this: {CATEGORY_ID

Rails 3 “last” method is returning incorrect result from ActiveRecord output

邮差的信 提交于 2019-12-24 00:07:25
问题 I've got the following code in my controller: @items = Item.where(:user_id => 1).order("updated_at DESC").limit(2) @oldest_item = @items.last For some reason, and I'm guessing this has to do with my recent upgrade to Rails 3, @oldest_item is not being set to the last item in @items, but is instead being set to the last item that matches Item.where(:user_id => 1).order("updated_at DESC") . So imagine there are 3 items that match, A, B, and C. @items is being set to [A, B], and then @oldest