Passing parameters in rails redirect_to
Just append them to the options: redirect_to controller: ‘thing’, action: ‘edit’, id: 3, something: ‘else’ Would yield /thing/3/edit?something=else
Just append them to the options: redirect_to controller: ‘thing’, action: ‘edit’, id: 3, something: ‘else’ Would yield /thing/3/edit?something=else
if @players.exclude?(p.name) … end ActiveSupport adds the exclude? method to Array, Hash, and String. This is not pure Ruby, but is used by a LOT of rubyists. Source: Active Support Core Extensions (Rails Guides)
Within Rails’ implementation of REST new and create are treated differently. An HTTP GET to /resources/new is intended to render a form suitable for creating a new resource, which it does by calling the new action within the controller, which creates a new unsaved record and renders the form. An HTTP POST to /resources takes … Read more
Update 4 – Rails 6.1 Thanks to Tim Park for pointing out that in the upcoming 6.1 you can do this: Person.where.missing(:contacts) Thanks to the post he linked to too. Update 3 – Rails 5 Thanks to @Anson for the excellent Rails 5 solution (give him some +1s for his answer below), you can use … Read more
Give this a shot: has_many :jobs, foreign_key: ‘user_id’, class_name: ‘Task’ Note, that :as is used for polymorphic associations. Also, foreign_key option for has_many.
For starters rake db:rollback will get you back one step then rake db:rollback STEP=n Will roll you back n migrations where n is the number of recent migrations you want to rollback. More references here.
If you do it in a migration then you could probably do it like this: # Make sure no null value exist MyModel.where(date_column: nil).update_all(date_column: Time.now) # Change the column to not allow null change_column :my_models, :date_column, :datetime, null: false
Just to follow up on this old topic; here’s what I think a current Rakefile (since a long ago) should do there. It’s an upgraded and bugfixed version of the current winning answer (hgimenez): desc “Testing environment and variables” task :hello, [:message] => :environment do |t, args| args.with_defaults(:message => “Thanks for logging on”) puts “Hello … Read more
The method helper_method is to explicitly share some methods defined in the controller to make them available for the view. This is used for any method that you need to access from both controllers and helpers/views (standard helper methods are not available in controllers). e.g. common use case: #application_controller.rb def current_user @current_user ||= User.find_by_id!(session[:user_id]) end … Read more
You need to add the following line at every environment: config.action_mailer.default_url_options = { :host => “yourhost” } That way, it can work in all environments and could be different from environment to environment. For example: development.rb config.action_mailer.default_url_options = { :host => “dev.yourhost.com” } test.rb config.action_mailer.default_url_options = { :host => “test.yourhost.com” } production.rb config.action_mailer.default_url_options = { … Read more