Rails关联:create action中的save方法类型无效

时间:2014-09-03 11:49:36

标签: ruby-on-rails angularjs ruby-on-rails-4

我希望使用rails for server并通过httppost方法发布数据,然后将数据保存到数据库。为此,我有2个模型和控制器:bolouk和' radif'。 我有以下关联:

models/bolouk.rb

class Bolouk < ActiveRecord::Base
  has_many :radifs, :dependent => :destroy
end

models/radif.rb

class Radif < ActiveRecord::Base
  belongs_to :bolouk
end

我可以创建新的bolouk,但是当我想创建新的radif时,我会发送我在数据库中创建新radif所需的所有方法(如bolouk_id和radif参数),我在rails服务器和create action中收到了这些数据,但是当我想创建新方法时,我得到500 Internal Error

controller/api/v1/radifs_controller.rb

class Api::V1::RadifsController < Api::V1::BaseController
  def create
    @radif = Radif.create(radif_params)
    if @radif.valid?
      puts "if"
      respond_with @radif, :location => api_bolouk_radifs_path
    else
      puts "else"
      respond_with @radif.errors, :location => api_bolouks_radifs_path
    end
  end
  private
  def radif_params
    params.require(:radif).permit(:bolouk_id, :describe, :price)
  end
end

我在创建操作中添加了一些日志,我将控制器检测@radif定义为invalid,并且不会将@radif保存在数据库中。我把服务器登录到下面:

Started POST "/api/bolouks/23/radifs?describe=110&price=128" for 127.0.0.1 at 2014-09-03 16:05:37 +0430
Processing by Api::V1::RadifsController#create as JSON
  Parameters: {"bolouk_id"=>"23", "describe"=>"110", "price"=>"128", "radif"=>{"bolouk_id"=>"23"}}
  User Load (0.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 7  ORDER BY "users"."id" ASC LIMIT 1
   (0.0ms)  begin transaction
   (0.0ms)  rollback transaction
else
Completed 500 Internal Server Error in 27ms (Views: 1.0ms | ActiveRecord: 1.0ms)

我从angularjs发送数据,我有以下代码: newradif.html

<form ng-submit="saveRadif()" class="form-horizontal" role="form">
                    <div>
                        <ul>
                            <li ng-repeat="err in errors">{{err}}</li>
                        </ul>
                    </div>
                    <div class="form-group">
                        <label for="radifdescribe" class="col-sm-2 control-label"><span>Describe:</span></label>
                        <div class="col-sm-4">
                            <input ng-model='modalRadif.describe' class="form-control" id="radifdescribe" placeholder="describe">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="radifprice" class="col-sm-2 control-label"><span>Price</span></label>
                        <div class="col-sm-4">
                            <input ng-model="modalRadif.price" class="form-control" id="radifprice" placeholder="price">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-success">Create</button>
                        </div>
                    </div>
                </form>

并在angularjs控制器中:

    $scope.saveRadif = function(){
            Radifs.create($scope.modalRadif, {bolouk_id: $scope.saveBolouk.id}).$promise.then(function(data){
                $scope.radifs = Radifs.index({bolouk_id: $scope.saveBolouk.id});
    //            $scope.saveRadif = data;
            })
                .catch(function (err) {
                    console.log(err.data);

                })
        }
app.factory('Radifs', function($resource) {
    return $resource('/api/bolouks/:bolouk_id/radifs', {bolouk_id: '@bolouk_id'}, {
        index: { method: 'GET', isArray: true},
        create: { method: 'POST' }
    });
});

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

在模型类中,您已在Bolouk和Radif模型之间定义了has_many和belongs_to关联。因此,您可以通过以下方式创建属于Bolouk对象的新Radif对象。

def create
  @bolouk = Bolouk.find(params[:bolouk_id])
  @radif = @bolouk.radifs.build(radif_params)
  if @radif.save
    respond_with @radif, :location => api_bolouk_radifs_path
  else
    respond_with @radif.errors, :location => api_bolouk_radifs_path
  end    
end