[英]Implementing a specific action for a model that respond_with(@object)
我有一個用戶可以創建的Subscription
模型。 用戶需要驗證發送給他們的PIN,以在創建后確認訂閱。 我在嘗試找出實現此目的的最佳方法時遇到了一些麻煩。
我實現了帶有兩個new
和create
動作的confirms
控制器。
def new
@confirm = Subscription.new
end
def create
@keyword = Keyword.joins(:shortcode).where("shortcodes.shortcode = ? and shortcodes.country = ?",params[:subscription][:shortcode],params[:subscription] [:country]).find_or_create_by_keyword(params[:subscription][:keyword])
if @confirm = Subscription.where(:phone => params[:subscription][:phone], :country => params[:subscription][:country], :keyword_id => @keyword.id).last
@confirm.check_subscription_pin(params[:subscription][:pin])
respond_with(@confirm)
elsif @confirm && @confirm.errors.any?
flash[:notice] = @confirm.errors
render :action => :new
else
flash[:notice] = "Subscription not found."
render :action => :new
end
end
該解決方案看起來並不十分令人信服,因為我想始終使用respond_with(@confirm)
來允許通過JSON
完成REST POST
。
進行curl調用時,沒有收到params [:subscription]。 Phone和Pin嵌套在訂閱中,因此會出現錯誤。
<pre>undefined method `[]' for nil:NilClass</pre>
我不知道如何使用嵌套參數進行curl調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.