Question

I ran

rails g scaffold Freqs description:string --skip-stylesheets

and migrated my database and restarted the server.

When I create a new Freq within my application, it is successfully created, but without any of the text I type into the description box.

I have checked the code in the Freq model, and the description is, indeed, being put out. For some reason it just isn't being displayed.

Any idea why this could be happening? Please request specific code if needed.

Here is the show.html.erb view that corresponds to Freqs:

<p id="notice"><%= notice %></p>

<p>
  <strong>Description:</strong>
  <%= @freq.description %>
</p>

<%= link_to 'Edit', edit_freq_path(@freq) %> |
<%= link_to 'Back', freqs_path %>

The problem lies within the controller. Here is the error I receive when I try to save a description:

WARNING: Can't mass-assign protected attributes for Freq: description app/controllers/freqs_controller.rb:27:in `create'

This is on line 27:

@freq = Freq.new(freq_params)

I solved the problem by adding

attr_accessible :description

to the Freq model.

Was it helpful?

Solution

In Rails 4, the parameter validation was removed from the Model, and now lives in the Controller. So, the "attr_accessible" line isn't used in the model anymore.

From the line you gave from the controller, there is a freq_params function in your controller, that should be like this:

# Never trust parameters from the scary internet, only allow the white list through.
def freq_params
  params.require(:freq).permit(:description)
end

If it's not, change it to this, and remove the attr_accessible line from the Freq Model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top