ruby-on-rails-3

JQuery ajax OPTIONS authorisation request not working

為{幸葍}努か 提交于 2019-12-24 20:22:53
问题 I'd like to POST data to another domain and receive the confirmation message returned by the action. So to get CORS to work I've got an options action to handle the OPTION HTTP method (on the same path as the POST) in my rails controller, which currently looks like this: def options headers['Access-Control-Allow-Origin'] = "*" headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS' headers['Access-Control-Max-Age'] = '100' headers['Access-Control-Allow-Headers'] = '*, x-requested

how to create select box from a given list in rails?

て烟熏妆下的殇ゞ 提交于 2019-12-24 20:15:07
问题 I am passing a list from my controller @distinct_grade_list = Student.uniq.pluck(:grade) which creates the distinct list of grades from model Student now in my view page , how can i display it as select box I am using <%= collection_select(A, @distinct_grade_list, B, C, D) %> now what do i have to keep at A,B,C,D 回答1: collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {}) public why not read the api document and sample? sample usage: class

Why don't I understand the Rails validates presence from tutorial?

[亡魂溺海] 提交于 2019-12-24 19:27:32
问题 I am following a Rails tutorial, and we created a user model, then were testing it with RSpec. In the spec/model/user_spec.rb we have: require 'spec_helper' describe User do before do @user = User.new(name: "Example User", email: "user@example.com") end subject { @user } it { should respond_to(:name) } it { should respond_to(:email) } it { should be_valid } describe "when name is not present" do before { @user.name = " " } it { should_not be_valid } end end When running the test it fails.

How can I code if I want to avoid n plus one issue?

与世无争的帅哥 提交于 2019-12-24 19:07:52
问题 I have these 3 models Student Gender BloodType Prefecture Student has these three models such as Gender, BloodType, Prefecture. and each of them belongs to Student. Association is set up already in each model files. In this case, how can I code if I want to avoid N + 1 issue? Is it something like this? @students = Student.find(:all).includes.includes(:gender, :blood_type, :prefecture) 回答1: You only need .includes once, and you can get rid of the find(:all) since Rails 3 does not require it.

Activeresource, updating and merging

半城伤御伤魂 提交于 2019-12-24 19:07:04
问题 I've run into a problem I'm not able to handle right now with an ActiveResource object that looks something like this: #<Settings:0x000000085fff48 @attributes= {"account_id"=>1, "created_at"=>"2012-01-10T14:54:36Z", "id"=>1, "settings_hash"=> #<Settings::SettingsHash:0x000000085ff250 @attributes= {"email_notices"=> #<Settings::SettingsHash::EmailNotices:0x0000000860c860 @attributes= {"none"=> ["none", "none"]}, @persisted=false, @prefix_options={}>, "permissions"=> #<Settings::SettingsHash:

Rails::Engine namespacing controller and models

别等时光非礼了梦想. 提交于 2019-12-24 18:59:53
问题 I followed the following tutorial: http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/ And it all works great. I namespaced the controller using #app/controller/authr/accounts_controller.rb module Authr class AccountsController < ApplicationController unloadable def new @account = Account.new end def create @account = Account.new(params[:account]) if @account.save redirect_to '/' else render :action => :new end end end end And in the tutorial he didn't

How to use nested attributes with belongs_to association on new action?

一笑奈何 提交于 2019-12-24 17:55:55
问题 On the update action the following nested_form works, but on create I get this error: Couldn't find Student with ID=12 for Cv with ID= Controller: def new @cv = Cv.new @cv.student = current_student end def create @cv = Cv.new(params[:cv]) if @cv.save redirect_to student_dashboard_path, notice: t('activerecord.successful.messages.created', model: @cv.class.model_name.human) else render 'new' end end Model: class Cv < ActiveRecord::Base attr_accessible :student_attributes belongs_to :student

How to use nested attributes with belongs_to association on new action?

廉价感情. 提交于 2019-12-24 17:55:00
问题 On the update action the following nested_form works, but on create I get this error: Couldn't find Student with ID=12 for Cv with ID= Controller: def new @cv = Cv.new @cv.student = current_student end def create @cv = Cv.new(params[:cv]) if @cv.save redirect_to student_dashboard_path, notice: t('activerecord.successful.messages.created', model: @cv.class.model_name.human) else render 'new' end end Model: class Cv < ActiveRecord::Base attr_accessible :student_attributes belongs_to :student

Render mime typed templates in Rails that are in a subfolder without explicitly providing the whole path in a render call

寵の児 提交于 2019-12-24 17:40:12
问题 I'm working with a user role based rails application, that will render different views based on a given user role. This is accomplished by using custom mime types. I built it according to this stack overflow post here. It works brilliantly. You don't even have to explicitly render the according templates in a controller: def index @projects = Project.all end instead of: def index @projects = Project.all respond_to do |format| format.html format.admin end end The controller will automatically

include multiple column value in Ruby on rails Collection_select. Also format the date

醉酒当歌 提交于 2019-12-24 17:26:36
问题 Earlier i was using This code <%= select "selected_payment", "id", @shifts.map {|u| [' ' +u.start_time.strftime("%I:%M %p") + '-' + u.end_time.strftime("%I:%M %p")+' ',u.id]} %> to make a dropdown. Now i have to do same thing with collection_select. But i'm not able to figure how to do it. It will be something like the one given below : <%= f.collection_select :shift_id, @shifts,:id, :start_time, :prompt => true %> I can not even format the date and use two values at the same time. Please