ruby-on-rails-4

Select enum from form to set role

心不动则不痛 提交于 2020-01-11 17:56:24
问题 Ruby on Rails 4.1 I am using Devise with enum role. It currently sets a defualt role when the User is created. I want to add a field to the form that creates Users to set the enum role. I read this but it doesn't say how to utilize the new roles. This is the User class devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :trackable, :validatable enum role: [:user, :vip, :admin, :developer, :marketing, :support, :translator] after_initialize :set_default

How to display error messages in a multi-model form with transaction?

偶尔善良 提交于 2020-01-11 13:05:08
问题 Two models, Organization and User, have a 1:many relationship. I have a combined signup form where an organization plus a user for that organization get signed up. The problem I'm experiencing is: When submitting invalid information for the user, it renders the form again, as it should, but the error messages (such as "username can't be blank") for the user are not displayed. The form does work when valid information is submitted and it does display error messages for organization, just not

How to display error messages in a multi-model form with transaction?

时光毁灭记忆、已成空白 提交于 2020-01-11 13:03:57
问题 Two models, Organization and User, have a 1:many relationship. I have a combined signup form where an organization plus a user for that organization get signed up. The problem I'm experiencing is: When submitting invalid information for the user, it renders the form again, as it should, but the error messages (such as "username can't be blank") for the user are not displayed. The form does work when valid information is submitted and it does display error messages for organization, just not

Rails 4: counter_cache in has_many :through association with dependent: :destroy

北战南征 提交于 2020-01-11 09:54:09
问题 Although similar questions have already been asked: counter_cache with has_many :through dependent => destroy on a "has_many through" association has_many :through with counter_cache none of them actually addresses my issue. I have three models, with a has_many :through association : class User < ActiveRecord::Base has_many :administrations has_many :calendars, through: :administrations end class Calendar < ActiveRecord::Base has_many :administrations has_many :users, through:

Why do I need `render layout: false` in my Rails controller action?

余生长醉 提交于 2020-01-11 09:49:07
问题 I'm use the remote: true idiom from the Working with Javascript in Rails guide: # new.html.slim = form_for @thing, remote: true do |f| f.text_field :whatever f.submit 'Submit' # thing_controller.rb layout 'foo' def create end # create.js.erb alert('foobar') This fails, because create.js.erb is for some reason rendered inside the 'foo' layout and returned as html, not javascript, despite the fact that the request is correctly processed as Javascript: Processing by ThingsController#create as JS

Add space after commas only if it doesn't already?

泪湿孤枕 提交于 2020-01-11 07:11:41
问题 Is there a way to add a space after commas in a string only if it doesn't exist. Example: word word,word,word, Would end up as word word, word, word, Is there a function in ruby or rails to do this? This will be used on hundreds of thousands of sentences, so it needs to be fast (performance would matter). 回答1: Using negative lookahead to check no space after comma, then replace with comma and space. print 'word word,word,word,'.gsub(/,(?![ ])/, ', ') 回答2: Just use a regular expression to

Using Delegate With has_many In Rails?

跟風遠走 提交于 2020-01-11 05:13:25
问题 We've got 2 models & a join model: #app/models/message.rb Class Message < ActiveRecord::Base has_many :image_messages has_many :images, through: :image_messages end #app/models/image.rb Class Image < ActiveRecord::Base has_many :image_messages has_many :messages, through: :image_messages end #app/models/image_message.rb Class ImageMessage < ActiveRecord::Base belongs_to :image belongs_to :message end Extra Attributes We're looking to extract the extra attributes from the join model (

Rails render of partial and layout in controller

百般思念 提交于 2020-01-11 04:38:13
问题 I am overriding the create action of the devise Registrations Controller. I have two forms for signup, individual or company, a company has a field called company_form set to true that differentiates the two forms. Upon form validation I would like the correct form to render (previously it was going back to the default form no matter what form i was using). I am having an issue where just the partial is being rendered (obvious as i am only rendering the partial), but I need the layouts

ActionController::UnknownFormat with format.js for ajax implementation (Rail 4)

*爱你&永不变心* 提交于 2020-01-11 04:31:08
问题 i try to use Ajax in my application with Rails 4. To send my js to the client i use : respond_to do |format| format.js end in my controller. But it's generated error "ActionController::UnknownFormat" i my controller. Someone can help me please ? main_controller.rb: class Oweb::MainController < ApplicationController def index ..... end def setmagasinstatus begin @mag = Magasin.find(params[:id]) if @mag.etatmagasin.lib == 'Ouvert' @mag.etatmagasin = Etatmagasin.where(lib: 'Fermé').first else

Accessing Custom Parameters when using Devise and Rails 4

岁酱吖の 提交于 2020-01-11 04:08:05
问题 I am learning rails using the teamtreehouse tutorial. The tutorial uses 3.1 but I am trying to learn 4.0, and as a result I have run into a difficulty presumably because rails 4 forces of the use of strong parameters. I have two models, a users model and a statuses model. I have used devise to create authentication for users, and have included new parameters. They are :first_name, :last_name, and :profile_name. I have created a relationship between users and statuses. Currently the user sign