基本的に、このコードは目的の url_type で URL を作成できるようにする必要がありますが、ユーザーは UrlType を読み取ることはできますが、更新アクションを介して設定することはできません。エラーは「URL タイプが無効です」です
class UrlsController < ApplicationController
before_filter :authenticate_user!
before_action :set_url, only: [:show, :edit, :update, :destroy]
def update
respond_to do |format|
if @url.update(url_params)
format.html { redirect_to @url, notice: 'Url was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @url.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_url
@url = Url.restrict!(current_user).find(params[:id])
end
end
class Url < ActiveRecord::Base
belongs_to :url_type
protect do |user|
if user.admin?
scope {all}
can :read
can :create
can :update
can :destroy
else
scope {all}
can :read
can :create
can :create, workflow_state: -> (state) { state == 'new'}
can :update, %w(address contractor_id destination domain url_type_id user_id)
#cannot :update, 'workflow_state'
end
end
end
class UrlType < ActiveRecord::Base
has_many :urls
protect do |user|
if user.admin?
scope {all}
can :read
can :create
can :update
can :destroy
else
scope {all}
can :read
cannot :update
end
end
end
サンプルデータ - url_params は
=> {"address"=>
"http://stackoverflow.com/questions/10016195/displaying-errors-when-a-twitter- bootstrap-modal-window-contains-a-rails-form-he",
"user_id"=>1,
"destination"=>"http://boscogreenholt.org/katelin.pouros",
"domain"=>"renner.org",
"url_type_id"=>"1",
"contractor_id"=>"1"}