2014-09-03 63 views
0

我想通過httppost方法使用rails for server和post數據,然後將數據保存到數據庫。爲此,我有2個模型和控制器:bolouk和'radif'。 我有關聯如下:Rails關聯:在創建操作中保存方法的類型無效

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 

我把一些日誌中創建行動,我定義控制器檢測@radifinvalid,不保存@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

您的描述和價格超出了參數中的radif散列值。你可以展示你的表單嗎?你也需要調用save方法而不是create方法。 – Mandeep 2014-09-03 11:55:50

+0

我將表單添加到問題中。 – mgh 2014-09-03 12:03:33

+0

@Mandeep你的指導是非常有用的。我檢查表單並編輯數據發送的類型。 – mgh 2014-09-03 20:47:09

回答

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 
+0

我將此代碼添加到'radifs_controller',但數據不再保存到數據庫。 – mgh 2014-09-03 13:56:16

+0

我添加登錄控制器,'@ radif'不保存並運行'else'語句。 – mgh 2014-09-03 14:01:00

+0

'@ radif.save'和'@ radif.save!'有什麼區別嗎?當我使用'@radif.save!'時,出現以下錯誤:'「驗證失敗:描述不能爲空,Price不能爲空」。 – mgh 2014-09-03 14:12:57