未经许可的参数:: email,:address

时间:2018-05-07 04:46:08

标签: ruby-on-rails ruby save simple-form form-for

我需要一个按钮,当我点击它时,必须在用户表中更新name属性,并且地址必须保存在地址表中。地址表属于用户表。现在我的错误是,当我点击提交按钮时,我无法保存地址,但我的名字在用户表中更新。任何人都可以帮忙吗?

控制器代码

class ProfileController < ApplicationController
  before_action :set_user, only: %i[index update_profile]

  def index; end

  def create
      @address = Address.new(address_params)
      respond_to do |format|
        puts'-=-==-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-='
        if @address.save
          format.html { redirect_to profile_index_path, notice: 'Address was successfully created.' }
        else
          format.html { render :new }
        end
      end
    end

  def update_profile
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to profile_index_path, notice: 'Profile was successfully updated.' }
      else
        format.html { render :index }
      end
    end
  end

private

  def set_user
    @user = User.find(current_user.id)
    @user.address || @user.build_address
  end

  def user_params
    params.require(:user).permit(:name, address_attributes: %i[area state country])
  end

  def address_params
     params.require(:address).permit(address: %i[area state country])
   end
end

模型

address.rb

class Address < ApplicationRecord
  belongs_to :user
end

user.rb

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_one :address, dependent: :destroy

  validates :name, presence: true
  def admin?
    true
  end
end

查看代码

<%= form_for(@user, url: { action: 'update_profile' }, html: { class: 'm-form m-form--fit m-form--label-align-right' } ) do |f| %>
  <% if @user.errors.any? %>
    <h4><%= pluralize(@user.errors.count, "error") %>
      prohibited this profile from being saved:</h4>
    <ul>
      <% @user.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
    </ul>
  <% end %>
  <div class="form-group m-form__group row">
    <label for="name" class="col-2 col-form-label">
      Name
    </label>
    <div class="col-7">
      <%= f.text_field :name, class: 'form-control m-input', placeholder: 'Full Name' %>
    </div>
  </div>
  <div class="m-form__seperator m-form__seperator--dashed m-form__seperator--space-2x"></div>
  <%= f.fields_for @user.address do |a| %>

  <div class="form-group m-form__group row">
    <label for="example-text-input" class="col-2 col-form-label">
      Address
    </label>
    <div class="col-7">
      <%= a.text_field :area, class: 'form-control m-input', placeholder: 'Address' %>
    </div>
  </div>
  <div class="form-group m-form__group row">
    <label for="example-text-input" class="col-2 col-form-label">
      City
    </label>
    <div class="col-7">
      <%= a.text_field :city, class: 'form-control m-input', placeholder: 'City' %>
    </div>
  </div>
  <div class="form-group m-form__group row">
    <label for="example-text-input" class="col-2 col-form-label">
      State
    </label>
    <div class="col-7">
      <%= a.text_field :state, class: 'form-control m-input', placeholder: 'State' %>
    </div>
  </div>
<% end %>
<%= f.submit 'Save Changes', class: 'btn btn-accent m-btn m-btn--air m-btn--custom' %>
&nbsp;&nbsp;
<%= link_to 'Back', root_path, class: 'btn btn-secondary m-btn m-btn--air m-btn--custom' %>

的routes.rb

resources :profile do
    collection do
      patch 'update_profile'
      get 'update_profile'
    end
  end

终端日志

Started PATCH "/profile/update_profile" for 127.0.0.1 at 2018-05-07 12:33:15 +0530
Processing by ProfileController#update_profile as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"AJZaELC5GwhOsBCKRZMQMpKySMxNpIIHgNpFBuS1KbBFSs61ByG7RAxVXV8Rd7QXaQ7Htzgyty4Z1uclocXOhQ==", "user"=>{"name"=>"Karthi", "email"=>"suriya@gmail.com", "address"=>{"area"=>"L1, 54th St. & 9th Ave. Ashok Nagar", "city"=>"Chennai", "state"=>"Tamil Nadu", "country"=>"India", "postcode"=>"600083"}}, "commit"=>"Save Changes"}
  User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 7 ORDER BY `users`.`id` ASC LIMIT 1
  User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 7 LIMIT 1
  Address Load (0.3ms)  SELECT  `addresses`.* FROM `addresses` WHERE `addresses`.`user_id` = 7 LIMIT 1
Unpermitted parameter: :address
   (0.1ms)  BEGIN
   (0.2ms)  COMMIT
Redirected to http://localhost:3000/profile
Completed 302 Found in 7ms (ActiveRecord: 1.2ms)


Started GET "/profile" for 127.0.0.1 at 2018-05-07 12:33:15 +0530
Processing by ProfileController#index as HTML
  User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 7 ORDER BY `users`.`id` ASC LIMIT 1
  User Load (0.2ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 7 LIMIT 1
  Address Load (0.2ms)  SELECT  `addresses`.* FROM `addresses` WHERE `addresses`.`user_id` = 7 LIMIT 1
  Rendering profile/index.html.erb within layouts/application
  Rendered profile/_profile_card.html.erb (0.3ms)
  Rendered profile/_profile_detail.html.erb (1.7ms)
  Rendered profile/index.html.erb within layouts/application (3.4ms)
  Rendered layouts/_web_font.html.erb (0.3ms)
  Rendered layouts/_google_analytics.html.erb (0.2ms)
  Rendered layouts/_header.html.erb (1.8ms)
  Rendered layouts/_alerts.html.erb (0.4ms)
Completed 200 OK in 32ms (Views: 28.7ms | ActiveRecord: 0.7ms)

2 个答案:

答案 0 :(得分:1)

我省略了一些与错误无关的方法,并对控制器进行了一些更改:

class ProfileController < ApplicationController
  def create
    @address = Address.new(address_params)
    respond_to do |format|
      if @address.save
        format.html { redirect_to profile_index_path, notice: 'Address was successfully created.' }
      else
        format.html { render :new }
      end
    end
  end

  def update_profile
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to profile_index_path, notice: 'Profile was successfully updated.' }
      else
        format.html { render :index }
      end
    end
  end

  private

  def user_params        
    # changed `address_attributes` to `address` as the params in request contains key `address`
    params.require(:user).permit(:name, address: %i[area state country])
  end

  def address_params
    # permit the attributes directly instead of nesting them under another `address` key
    params.require(:address).permit(*%i[area state country])
  end
end

如果它不起作用,请告诉我。

答案 1 :(得分:0)

请在您的用户模型中添加以下行

user.rb

accepts_nested_attributes_for :address