Ruby On Rails保存到数据库多个表

时间:2014-03-21 05:39:54

标签: mysql ruby-on-rails ruby ruby-on-rails-3

我是Ruby on Rails的新手,并尝试通过一个简单的应用程序来学习它。我要做的是使用嵌套属性将详细信息保存到数据库中的不同表。我得到一个未经合成的参数:终端中的位置错误。

这是我的文件..

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>

和_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>

我的页面中有4个字段。名字和姓氏将保存到用户表。我想要实现的是我想将state_id和city_id保存到位置表。 State和City表有一组变量。州和城市是下拉列表。选择一个州后,相应的城市将被加载到城市下拉列表。

这是终端的输出..

"======================="
{"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

提前谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用用户实例(@user)来构建表单。您正在使用:用户符号。您的构建表单对用户模型一无所知,并将位置参数作为位置而不是 locations_attributes 传递。

User Controller.rb

class UserController < ApplicationController
  def new
    @user = User.new
  end
  ...
end

new.html.erb

<html>
    <head></head>
    <body>

        <%= form_for @user do |j| %>
        ...

解决方案2:

您只需将:locations_attributes 符号传递给fields_for助手,而不是:locations 符号:

<%= j.fields_for :locations_attributes do |jl| %>