ruby-on-rails-3

How to make Rails do not ignore trailing slashes in the routes?

妖精的绣舞 提交于 2019-12-24 01:24:15
问题 For example, I have the rote in routes.rb: get 'companies/' => 'companies#index', :as => :companies There is the way to generate link with the trailing slash (like "http://website.com/companies/"): link_to 'Compaines', companies_path(:trailing_slash => true) But I want Rails to generate link with trailing slash if it is present in the route by default: link_to 'Companies', companies_path Now it generates something like "http://website.com/companies". How to fix it? 回答1: I'm not sure why you

Ruby on Rails two create actions to two different controllers simultaneously

旧巷老猫 提交于 2019-12-24 01:18:58
问题 I have a Threads controller and Messages controller. Threads has_many Messages Once a user clicks send, I send data into Threads controller to create the thread. I want to make it to so that threads_controller.rb def create ... if @thread.save #send data into messages_controller #to create the corresponding table for a message with this thread_id end So, essentially I am trying to do two POSTS one after another if the first one succeeds. I think redirect_to is what I am supposed to use. Is it

How to sort results based on join table in Rails?

女生的网名这么多〃 提交于 2019-12-24 01:18:37
问题 I have Account model, class Account < ActiveRecord::Base has_many :account_games def score account_games.map(&:score).sum end end And AccountGame : class AccountGame < ActiveRecord::Base belongs_to :account end What is the best way to get accounts with the highest score? as you can see, score is the summation of related account_games score field. Thanks 回答1: try Account.joins(:account_games).sum('account_games.score', group: :account_id, order: 'sum_account_games_score') which will give you a

Rails 3, radiobutton group in fields_for?

♀尐吖头ヾ 提交于 2019-12-24 01:12:25
问题 I think this should be kinda trivial, but somehow I must be doing something wrong.. And the fact that I can't find any google results proving me any good, I might be missing some point. My subject almost says it all: How can I make a radiobutton group that is generated in a fields_for iteration. E.g.: form_for @team do |t| f.fields_for :players |p| p.radio_button :is_captain, "is_captain_group" Where is_captain is a boolean field on the player model. The above method does not work... EDIT: As

how to make Javascript alert box display a variable from Rails controller

北城以北 提交于 2019-12-24 01:06:54
问题 I have this piece of Javascript codes in my view file: <script> function hint() { var str = @user.name; var name = str.toLowerCase(); alert(name); } </script> I want the alert box to display the name of the user so I use the variable @user that was defined in my controller. However, when I click the button to active the alert box, nothing shows up. I also tried current_user.name, but it didn't work either. How can I display the value of a variable in the alert box? 回答1: Just keep it like this

How to give ActiveAdmin its own application layout? — Rails newbie

若如初见. 提交于 2019-12-24 01:01:59
问题 It's probably a dead simple question. Right now it uses the default application layout file. How can I create one just for AdminAdmin in order keep it separate from the one used on the main site? 回答1: Read layouts and rendering guide, there is paragraph about :layout option. You can specify layout, or set it to false, to render without layout at all. render :layout => 'special_layout' render :layout => false 回答2: If my memory does not fail me again !! ;-) this rails cast talk about this. But

Rename image using associated model - Paperclip

三世轮回 提交于 2019-12-24 00:59:09
问题 Code In my image model: has_attached_file :pic before_post_process :rename_pic before_save ->{ p 'before_save ----------------' } after_post_process ->{ p 'after_post_process --------------' } def rename_pic p 'en imagen' p self p 'en imagen' end In service that has many images: # don't use accepts_nested_attributes_for before_save :create_images attr_accessor :images_attributes def create_images # images_attributes example value: { "0"=> {img_attrs}, "1" => {img_attrs1} } images_attributes

mongoid scope with referenced object as criteria

柔情痞子 提交于 2019-12-24 00:52:12
问题 I have following scope for Mongoid model in Rails 3: class Expert include Mongoid::Document referenced_in :category scope :currently_available, lambda { |category, limit| limit ||= 5 { :where => {:category => category, :state => 'available'}, :limit => limit } } category here is an instance of referenced model: class Category include Mongoid::Document references_many :experts, :inverse_of => :category When I call the scope as Expert.currently_available(Category.first, 5) , I got a Criteria

Requiring gem in Rails 3 Controller failing with “Constant Missing”

我只是一个虾纸丫 提交于 2019-12-24 00:49:37
问题 I've seen this asked a few times in other threads, but none of the answers seem to apply. Environment: Rails 3 amazon/ecs gem from jugend. The lone file is here: http://github.com/jugend/amazon-ecs/blob/master/lib/amazon/ecs.rb my gemfile has: gem 'amazon-ecs', :git => 'git://github.com/jugend/amazon-ecs.git' Everything works in irb. I can run: bundle console require 'amazon/ecs' and then go to town when I try to use it from the controller though, like so: require 'amazon/ecs' require 'amazon

Installing libCurl - ruby 1.9.3 .. rails 3.2.12 .. Windows

六眼飞鱼酱① 提交于 2019-12-24 00:49:32
问题 So, I'm trying to get feedzirra up and running... ruby 1.9.3p448 Rails 3.2.12 I went to http://curl.haxx.se/download.html#Win32 and downloaded Win32 2000/XP 7.32.0 libcurl SSL . It may be obvious, but this is libcurl -v 7.32 . I unzipped the aforementioned file into c:\libcurl ( so the bin, lib, and whatnot are directly in libcurl ). I added c:\libcurl\bin to the PATH . While in my app's working directory, I told bundler where to find the cURL dependencies with bundle config build.curb --with