#Error 500 encountered in a basic Ruby on Rails and AngularJS collaboration

Thanks to everyone for taking the time to assist me with this problem. As a newcomer to Ruby, the solution may be quite simple. I have developed an API that facilitates communication between Ruby and Angularjs. Here is the API:

class EntriesController < ApplicationController
respond_to :json
protect_from_forgery

def index
    respond_with Entry.all
end

def show
    respond_with Entry.find(params[:id])
end

def create
    respond_with Entry.create(params[:entry])
end

def update
    respond_with Entry.update(params[:id], params[entry])
end

def destroy
    respond_with Entry.destroy(params[:id])
end
end

My AngularJS controller code is as follows:

@app = angular.module("angRails",["ngResource", "ngMaterial", "ngAnimate", "ngAria"]);

@mainCTRL = ["$scope", "$resource", ($scope, $resource) ->

  Entry = $resource("/entries/:id", {id: "@id"}, {update: {method: "PUT"}})

  $scope.newName = "";

  $scope.entries = Entry.query();


  $scope.addEntry = ->

unless $scope.newName is ""
  entry = Entry.save($scope.newName)
  console.log(JSON.stringify(entry));
  $scope.entries.push(entry);
  console.log("add Entry function");
  $scope.newName = "";

]


app.controller("mainCTRL", mainCTRL);

The $scope.entries = Entry.query(); line works perfectly fine, but there seems to be an issue with creating entries. The error message received is:

POST http://localhost:3000/entries 500 (Internal Server Error)

ActiveModel::ForbiddenAttributesError in EntriesController#create
ActiveModel::ForbiddenAttributesError

Extracted source (around line #14):
12
13 def create
14 respond_with Entry.create(params[:entry])
15 end

I am unsure of the reason behind this error. Any help provided would be greatly appreciated. Thank you!

Answer №1

It seems like you may be missing the implementation of strong parameters in your controller code.

Ensure that your controller includes a method specifying which parameters are permitted for your model to prevent unauthorized access.

def create
  @entry = Entry.new(entry_params)
  @entry.save
  respond_with(@entry)
end 

private 
def entry_params
  params.require(:entry).permit(:attribute, :another_attribute)
end

Answer №2

It appears that you are facing a ForbiddenAttributesError

To resolve this issue, make sure to explicitly declare which parameters are allowed for mass-updating:

You may find a solution to a similar problem here: ActiveModel::ForbiddenAttributesError when creating new user

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process for creating a personalized module in AngularJS?

Looking to create a bespoke module for AngularJS, but struggling to locate comprehensive documentation. What is the best approach to develop a custom AngularJS module that can be easily shared with others? ...

Tips for utilizing onsubmit following an ajax validation check

How can I implement the onsubmit method after an ajax check? The current onsubmit function is not working. $(document).ready(function(){ $("#p_code").change(function(){ $("#message").html("<img src='ajax_loader.gif' width='26 ...

What is the best way to add a CSS rule to JavaScript?

animation: scaleUp 0.3s linear 0.4s forwards; animation: scaleDown 0.3s linear forwards; Greetings! I'm currently working on adding animations to my content filtering functionality. Specifically, I want to incorporate the aforementioned CSS rules in ...

Arrange an array of objects by making a nested API call in Angular

My task involves sorting an array of objects based on the response from the first API call in ascending order. The initial API call returns a list of arrays which will be used for the subsequent API call. The first API call fetches something like this: [0 ...

Unable to retrieve input field in protractor testing

Struggling with accessing an input field in AngularJS/Protractor for testing purposes. Despite the app setting the input fields to their initial values when running on the browser, I am unable to interact with them or modify their contents during testing. ...

Empty req.params in nested ExpressJS routers

I have a unique routing system that relies on the directory structure of my 'api' folder to automatically configure routes. However, I encountered an issue where the req.params object is undefined in the controller when using a folder name as a r ...

Preventing event propagation in Angular when clicking

What is the best way to call an Angular function within my jQuery code? I attempted to do so with the following: $(document).on('click', '.myBtn', function(e) { angular.element($(".someClass")).scope().clearBtnItems(); e.stop ...

Use the button to trigger the opening of the datepicker in React material UI

Currently, I am incorporating a Datepicker in my project using React Material UI. <TextField id="datetime-local" label="Next appointment" type="datetime-local" defaultValue="2017-05-24T ...

Maintain the expanded sub-menu when the mouse leaves the area, but a sub-option has been

Implementing a side menu with parent and child options that dynamically display content in the main div on the right when a child option is selected. The parent options are initially shown upon page load, and the child options appear when the mouse hovers ...

Using AngularJS to dynamically populate select options from an array of objects within an object

I am trying to display my data in a list format like this: <option value="4351">Atlanta</option> Here is the content of my object. $scope.cityList = { 4351:{name:"Atlanta", short="atlanta"}, 7355:{name:"Baltimore", short="baltimore"} ...

Necessary workaround needed for HTML time limit

Our HTML page has been designed to automatically timeout after 10 minutes of inactivity. This functionality is achieved through the following code: function CheckExpire() { $.ajax({ type:'GET', url:self.location.pathname+"?ch ...

Using Rails AJAX to dynamically load partials without the need to submit

Imagine creating a dynamic page layout with two interactive columns: | Design Your Pizza Form | Suggested Pizzas | As you customize your pizza using the form in the left column, the right column will start suggesting various types of pizzas based on your ...

Troubleshooting: JQuery dropdown selection not updating image display

I am trying to create a select menu that changes the car image when a user selects a different model, but for some reason it is not working. Here is what I have tried: <h2 class="model">A6 <img src="images/3.jpg" id="image" width="544" height="2 ...

Using jQuery and Ajax to fade in content after all images and other assets have finished loading

I've encountered an issue with loading pages via ajax for users with custom URLs. For example, a profile is usually found at http://example.com/users/Dan, but if a user has a custom URL like http://example.com/DansCustomURL, I need to fetch their desi ...

Transferring an array from PHP to jQuery through the use of AJAX

My JavaScript code communicates with a PHP page to retrieve data from a database and store it in an array. Now, I would like to use jQuery to loop through that array. This is how the array is structured: Array ( [0] => Array ( [image] => articl ...

Looping through a JSON array

Currently, I am working with asp.net in Visual Studio and using jQuery to call a web method. In asp.net, I am generating a dynamic datatable and then returning it by using JsonConvert.SerializeObject(dt). $.ajax({ type: 'POST', url: &apo ...

Pnotify encounters a glitch where the buttons are not displayed when utilized with jquery

Using pnotify with jQuery and Bootstrap3, I am facing an issue where the buttons do not appear. In order to address this problem, I have ensured that both pnotify.custom.min.css and pnotify.custom.min.js files are included in the header of the application ...

Running function without the need to click on 'li' element

In the process of developing a simple guessing game, my aim is to allow users to click on a number and have that number stored as userAnswer, while the computerAnswer is a randomly generated number between one and ten. Despite setting the setVariables() f ...

Is there a way for me to expand the dropdown menu?

My current dropdown looks like this. I'd like the dropdown menu to span across the screen, something like this: To create my dropdown, I am using https://github.com/angular-ui/ui-select2. AngularJS JQuery Select2 I'm not sure if there is an ...

A guide on incorporating unique font weights into Material UI

Looking to customize the Material theme by incorporating my own font and adjusting the font weights/sizes for the Typography components. I am attempting to set 100/200/300/400/500/600/700 as options for each specific typography variant, but it seems that o ...