Gerald Gerald - 1 year ago 78
AngularJS Question

rails 5 - ActionController::UnknownFormat when sending data from angular

I started to study angular with rails. This is my problem, when I make a $http.get() request it returns and error, I think rails don't want to accept the data. this is my code:


<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="signup_user_name">User name: </label>
<input type="text" name="instructor[user_name]" id="signup_user_name" class="form-control" required="true" ng-model="instructor.username"/>
<button type="button" class="form-control btn btn-default" ng-click="validate()">validate username</button>


signup.controller('signupController', function($scope, $http){

$scope.validate = function() {

var username = $scope.instructor.username

var sent_data = { "username" : username };

$http.get('/home/signup', sent_data).success(
function(data, status, headers, config) {
// write code later
function(data, status, headers, config) {
// write code later

rails method:

def signup
params[:username] // trying to print the paramater sent by angular via $http.get()

This is the result:

Link to image result
Please help me... thanks! :)

Answer Source

This is not the way you pass your params to rails by a get request from angular

here's an example of how you should pass your data

$http.get('/home/signup', {
  params: {
    username: 'yourusername'

Another thing, you should render something in your rails api, and since you're using angular as your forntend framework then maybe you should render json or file

Try to use this line your rails api

render json: params[:username]

to check the data send to your rails api, you can check your console, use puts or byebug