UrlsController #create中的ActiveModel :: ForbiddenAttributesError

时间:2017-06-04 17:45:26

标签: ruby-on-rails-4

我有以下代码:

@IBOutlet weak var btnPrevious: UIButton!
@IBOutlet weak var btnNext: UIButton!

var nibView: UIView!
var instanceOfQuizControllerTest : QuizCointroller!

func prepareScreenWithView(navigationController: UINavigationController, viewSize: UIView, viewController: UIViewController) {

    nibView = Bundle.main.loadNibNamed("RMK", owner: self, options: nil)?[0] as! UIView
    nibView.tag = 200
    var tempRect = nibView.frame
    print(tempRect)

    if User.quizIndexNo == 1 {
        btnPrevious.isHidden = true //app is crashing here when i am trying to set title Button is showing nil
    }

    if User.quizIndexNo == 1 {
        btnNext.setTitle("Submit", for: .normal)
    }

    instanceOfQuizControllerTest = viewController as! QuizCointroller

    print("Quiz ViewController instatnce \(instanceOfQuizControllerTest)")
    tempRect =  CGRect(x: 0, y: 0, width: viewSize.bounds.width, height: viewSize.bounds.height)
    viewSize.addSubview(self.nibView)
    self.nibView.frame = tempRect

}

@IBAction func btnPrev(_ sender: UIButton) {
    User.quizIndexNo -= 1
    Sequence.instanceOfQuizControllerTest.previousButtonPressed()


}

@IBAction func btnNext(_ sender: UIButton) {

    print(instanceOfQuizControllerTest)

    User.quizIndexNo += 1
    Sequence.instanceOfQuizControllerTest.nextButtonPressed()


   }
}

我收到的错误我知道与所需参数和许可相关。任何人都可以告诉我该如何编写方法?提前坦克。

这是模型:

class UrlsController < ApplicationController
def new
 @shortened_url = Url.new
end
def create
 @shortened_url = Url.new(params[:url])
 if @shortened_url.save
   flash[:shortened_id] = @shortened_url.id
   redirect_to new_url_url
 else
   render :action => "new"
  end
end

def show
     @shortened_url = Url.find(params[:id])
    redirect_to @shortened_url.url
     end

 end

提交表单时出错。我明白了 UrlsController中的ActiveModel :: ForbiddenAttributesError #create

1 个答案:

答案 0 :(得分:0)

需要使用强参数

class UrlsController < ApplicationController
def new
 @shortened_url = Url.new
 end
def create
 @shortened_url = Url.new(shortened_url)
 if @shortened_url.save
   flash[:shortened_id] = @shortened_url.id
   redirect_to new_url_url
 else
   render :action => "new"
 end
end

 def show
 @shortened_url = Url.find(params[:id])
 redirect_to @shortened_url.url
end

private

 def shortened_url
   params.require(:url).permit!
 end

end