无法让textarea使用rails / angular

时间:2016-01-22 21:30:02

标签: ruby-on-rails angularjs

我试图在我的rails / angular app上创建一个简单的反馈表单。

我已在模板中创建表单

%form
  %input{"ng-model" => "feedbackForm.name", :type => "text", :placeholder => "Name"}
  %textarea{"ng-model" => "feedbackForm.feedback", :type => "text", :placeholder => "Feedback"}
  %button{:value => "send", "ng-click" => "send()"}Send

在我的feedbackCtrl中,我有创建功能,

createFeedback.create({
  name:      $scope.feedbackForm.name,
  feedback:  $scope.feedbackForm.feedback
})

一个简单的feedbackService,

app.factory('createFeedback', ['$http', function($http){
  return{
    create: function(feedback){
      return $http.post('/feedback.json', feedback);
    }
  };
}])

在棱角分明的一面似乎有用。

我在路线中创建了反馈资源,

resources :feedback, only:      [:create, :destroy, :index, :show]

这是轨道反馈控制器,

def create
   @feedback = Feedback.find_or_create_by feedback_params
   redirect_to :root
end

private
def feedback_params
  params.require(:feedback).permit(
    :name,
    :feedback
  )
end

反馈表,

create_table "feedbacks", force: :cascade do |t|
  t.string   "name"
  t.text     "description"
  t.datetime "created_at",  null: false
  t.datetime "updated_at",  null: false
end

当我使用表单发送一些数据时,我收到此错误,

Started POST "/feedback.json" for 127.0.0.1 at 2016-01-22 22:24:39 +0100
Processing by FeedbackController#create as JSON
  Parameters: {"name"=>"Sometext", "feedback"=>"Some more text"}
  User Load (12.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 9]]
Completed 500 Internal Server Error in 15ms (ActiveRecord: 12.5ms)

NoMethodError (undefined method `permit' for "Some more text":String):
  app/controllers/feedback_controller.rb:19:in `feedback_params'
  app/controllers/feedback_controller.rb:13:in `create'

因此我觉得轨道控制器存在问题,但我无法看到它。

1 个答案:

答案 0 :(得分:0)

查看您从客户端发送的参数:

Parameters: {"name"=>"Sometext", "feedback"=>"Some more text"}

反馈只是一个字符串,但您尝试将其作为哈希:

params.require(:feedback).permit(
  :name,
  :feedback
)

可行修复:

params.permit(
  :name,
  :feedback
)