I'm new to Ruby on Rails and trying to learn it by doing one simple application. What i'm trying to do is to save the details to different tables in a DB using nested attributes. Im getting an unpermited parameters :locations error in my terminal.
here are my files..
State.rb
class State < ActiveRecord::Base
has_many :locations
end
City.rb
class City < ActiveRecord::Base
has_many :locations
end
Location.rb
class Location < ActiveRecord::Base
belongs_to :user
belongs_to :state
belongs_to :city
end
User.rb
class User < ActiveRecord::Base
has_many :locations
accepts_nested_attributes_for :locations
end
User Controller.rb
class UserController < ApplicationController
def new
end
def create
p "======================="
p params
p "======================="
p user_params
p "-----------------------"
@user = User.new(user_params)
@user.save
redirect_to @user
end
def update_city_select
@cities = City.where(state_id: params[:state_id]).order(:name) unless params[:state_id].blank?
respond_to do |format|
format.html { render layout: false }
format.js
end
end
def show
# @user = User.find(params[:id])
# @location = Location.find(params[:id])
end
private
def user_params
params.require(:user).permit(:id,:firstname, :lastname,
:locations_attributes=>[:id,:user_id,:state_id,:city_id]
)
end
end
new.html.erb
<html>
<head></head>
<body>
<%= form_for :user, url: users_path do |j| %>
<div id="first_name">
<label>First Name</label>
<div>
<%= j.text_field :firstname %>
</div>
</div>
<div id="last_name">
<label>Last Name</label>
<div>
<%= j.text_field :lastname %>
</div>
</div>
<%= j.fields_for :locations do |jl| %>
<%= render 'location_fields', :j => jl %>
<% end %>
<div>
<div id="submit">
<%= j.submit %>
</div>
</div>
<%end%>
<script type="text/javascript">
$("#state_name").change(function() {
var state = $('#state_name :selected').val();
$.ajax({
url: '/student_profile/update_city_select',
type: 'GET',
data: {
state_id: state
},
success: function(response) {
$('#city_name').html(response);
}
});
});
</script>
</body>
</html>
and _location_fields.html.erb
<div id="state_name">
<label>Location[State]</label>
<div>
<%= j.select :state_id, State.all.map{|j| [j[:name], j[:id]]} %>
</div>
</div>
<div>
<label>City</label>
<div id="city_name">
<%= j.select :city_id, {} ,{} %>
</div>
</div>
I have this 4 fields in my page. First Name and Last Name gets saved to the User table. What im trying to achieve is that i want to save the state_id and city_id to locations table. State and City tables have a set of Variables. State anD City are dropdown list. Upon selecting one state corresponding cities will be loaded to the Cities Dropdown.
Here is the output from terminal..
"======================="
{"utf8"=>"✓", "authenticity_token"=>"7zmznmSLQ+D/089mZLjrT0H784lsX1ERilyvP68jG4I=", "user"=>{"firstname"=>"Nidhin", "lastname"=>"Besole", "locations"=>{"state_id"=>"3"}}, "city_id"=>"28516", "commit"=>"Save User", "controller"=>"user", "action"=>"create"}
"======================="
Unpermitted parameters: locations
{"firstname"=>"Nidhin", "lastname"=>"Besole"}
"-----------------------"
Unpermitted parameters: locations
THanks in advance.