Ruby On Rails保存到数据库多表



我是Ruby on Rails的新手,试图通过做一个简单的应用程序来学习它。我要做的是将详细信息保存到使用嵌套属性的DB中的不同表。我的终端出现了一个不允许的参数:位置错误。

这是我的文件。

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
用户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个字段在我的页面。名字和姓氏被保存到User表中。我想要实现的是,我想保存state_id和city_id的位置表。州和城市表有一组变量。州和城市是下拉列表。选择一个州后,相应的城市将被加载到cities下拉列表中。

下面是终端的输出。

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

提前感谢。

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

用户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 helper而不是:locations symbol:

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

相关内容

  • 没有找到相关文章

最新更新