【问题标题】:Simple Form Not Saving简单表格不保存
【发布时间】:2018-04-07 22:19:10
【问题描述】:

我正在构建一个包含客户、站点和报价的报价应用程序。

我有一个属于客户的模型站点。你可以看到下面的模型。

我想创建一个站点并填写如下表格。

由于某种原因,安装简单表单后没有任何保存,我无法创建新站点。

网站管理员

class SitesController < ApplicationController
  before_action :authenticate_user!
  before_action :set_site, only: [:show, :edit, :update, :destroy]

  # GET /sites
  # GET /sites.json
  def index
    @sites = Site.all
  end

  # GET /sites/1
  # GET /sites/1.json
  def show
  end

  # GET /sites/new
  def new
    @site = Site.new
  end

  # GET /sites/1/edit
  def edit
  end

  # POST /sites
  # POST /sites.json
  def create
    @site = Site.new(site_params)

    respond_to do |format|
      if @site.save!
        format.html { redirect_to @site, notice: 'Site was successfully created.' }
        format.json { render :show, status: :created, location: @site }
      else
        format.html { render :new }
        format.json { render json: @site.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /sites/1
  # PATCH/PUT /sites/1.json
  def update
    respond_to do |format|
      if @site.update(site_params)
        format.html { redirect_to @site, notice: 'Site was successfully updated.' }
        format.json { render :show, status: :ok, location: @site }
      else
        format.html { render :edit }
        format.json { render json: @site.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /sites/1
  # DELETE /sites/1.json
  def destroy
    @site.destroy
    respond_to do |format|
      format.html { redirect_to sites_url, notice: 'Site was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_site
      @site = Site.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def site_params
      params.require(:site).permit(:name, :client_id)
    end
end

网站模型

class Site < ApplicationRecord
  belongs_to :client
  has_many :quotes
end

新网站表单

<div class="row">
  <form class="col s12">
    <div class="row">
      <%= simple_form_for @site do |form| %>
        <% if site.errors.any? %>
          <div id="error_explanation">
            <h2><%= pluralize(site.errors.count, "error") %> prohibited this site from being saved:</h2>
            <ul>
              <% site.errors.full_messages.each do |message| %>
                <li><%= message %></li>
              <% end %>
            </ul>
          </div>
        <% end %>
        <%= form.input :name %>
        <%= form.association :client %>
        </div>
        <div class="row">
        <%= form.button :submit %>
        </div>

      <% end %>
      </form>
</div>

客户端模型

class Client < ApplicationRecord
  has_many :sites
  has_many :quotes
end

客户端控制器

class ClientsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_client, only: [:show, :edit, :update, :destroy]

  # GET /clients
  # GET /clients.json
  def index
    @clients = Client.all
  end

  # GET /clients/1
  # GET /clients/1.json
  def show
  end

  # GET /clients/new
  def new
    @client = Client.new
  end

  # GET /clients/1/edit
  def edit
  end

  # POST /clients
  # POST /clients.json
  def create
    @client = Client.new(client_params)

    respond_to do |format|
      if @client.save
        format.html { redirect_to @client, notice: 'Client was successfully created.' }
        format.json { render :show, status: :created, location: @client }
      else
        format.html { render :new, notice: 'Client was failed to create.' }
        format.json { render json: @client.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /clients/1
  # PATCH/PUT /clients/1.json
  def update
    respond_to do |format|
      if @client.update(client_params)
        format.html { redirect_to @client, notice: 'Client was successfully updated.' }
        format.json { render :show, status: :ok, location: @client }
      else
        format.html { render :edit }
        format.json { render json: @client.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /clients/1
  # DELETE /clients/1.json
  def destroy
    @client.destroy
    respond_to do |format|
      format.html { redirect_to clients_url, notice: 'Client was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_client
      @client = Client.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def client_params
      params.require(:client).permit(:name, :site_id)
    end
end

我没有收到任何错误,我只是无法创建新对象。

Started GET "/sites/new" for 127.0.0.1 at 2018-04-08 08:58:50 +1000
Processing by SitesController#new as HTML
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", "11121895-c5d0-42cc-b92c-4faf088d3f56"], ["LIMIT", 1]]
  Rendering sites/new.html.erb within layouts/application
  Client Load (0.3ms)  SELECT "clients".* FROM "clients"
  Rendered sites/_form.html.erb (56.1ms)
  Rendered sites/new.html.erb within layouts/application (60.0ms)
  Rendered shared/_header.html.erb (5.0ms)
  Rendered shared/_footer.html.erb (0.7ms)
Completed 200 OK in 161ms (Views: 153.0ms | ActiveRecord: 0.8ms)


Started GET "/sites/new?utf8=%E2%9C%93&authenticity_token=YCl94xAEq8Ph%2F9Y5dbBOKoKeZnWVvJdw9khi%2Fotjnta4Qt%2BkJ9kYUlze%2FtaOyIzFG7YvT2MfJEr%2FYWNzrZ5VPw%3D%3D&site%5Bname%5D=hgh&site%5Bclient_id%5D=8276d1d3-9a79-4c27-94f0-346c044c2592&commit=Create+Site" for 127.0.0.1 at 2018-04-08 08:58:58 +1000
Processing by SitesController#new as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"YCl94xAEq8Ph/9Y5dbBOKoKeZnWVvJdw9khi/otjnta4Qt+kJ9kYUlze/taOyIzFG7YvT2MfJEr/YWNzrZ5VPw==", "site"=>{"name"=>"hgh", "client_id"=>"8276d1d3-9a7-4c27-94f0-346c044c2592"}, "commit"=>"Create Site"}
  User Load (0.8ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", "11121895-c5d0-42cc-b92c-4faf088d3f56"], ["LIMIT", 1]]
  Rendering sites/new.html.erb within layouts/application
  Client Load (0.4ms)  SELECT "clients".* FROM "clients"
  Rendered sites/_form.html.erb (11.8ms)
  Rendered sites/new.html.erb within layouts/application (15.1ms)
  Rendered shared/_header.html.erb (2.4ms)
  Rendered shared/_footer.html.erb (0.6ms)
Completed 200 OK in 149ms (Views: 142.4ms | ActiveRecord: 1.2ms)

路线

Prefix Verb   URI Pattern                 Controller#Action
                   sites GET    /sites(.:format)            sites#index
                         POST   /sites(.:format)            sites#create
                new_site GET    /sites/new(.:format)        sites#new
               edit_site GET    /sites/:id/edit(.:format)   sites#edit
                    site GET    /sites/:id(.:format)        sites#show
                         PATCH  /sites/:id(.:format)        sites#update
                         PUT    /sites/:id(.:format)        sites#update
                         DELETE /sites/:id(.:format)        sites#destroy
         pages_dashboard GET    /pages/dashboard(.:format)  pages#dashboard
           import_quotes POST   /quotes/import(.:format)    quotes#import
                  quotes GET    /quotes(.:format)           quotes#index
                         POST   /quotes(.:format)           quotes#create
               new_quote GET    /quotes/new(.:format)       quotes#new
              edit_quote GET    /quotes/:id/edit(.:format)  quotes#edit
                   quote GET    /quotes/:id(.:format)       quotes#show
                         PATCH  /quotes/:id(.:format)       quotes#update
                         PUT    /quotes/:id(.:format)       quotes#update
                         DELETE /quotes/:id(.:format)       quotes#destroy
                 clients GET    /clients(.:format)          clients#index
                         POST   /clients(.:format)          clients#create
              new_client GET    /clients/new(.:format)      clients#new
             edit_client GET    /clients/:id/edit(.:format) clients#edit
                  client GET    /clients/:id(.:format)      clients#show
                         PATCH  /clients/:id(.:format)      clients#update
                         PUT    /clients/:id(.:format)      clients#update
                         DELETE /clients/:id(.:format)      clients#destroy
                   users GET    /users(.:format)            users#index
                    user GET    /users/:id(.:format)        users#show
        new_user_session GET    /login(.:format)            devise/sessions#new
            user_session POST   /login(.:format)            devise/sessions#create
    destroy_user_session DELETE /logout(.:format)           devise/sessions#destroy
       new_user_password GET    /password/new(.:format)     devise/passwords#new
      edit_user_password GET    /password/edit(.:format)    devise/passwords#edit
           user_password PATCH  /password(.:format)         devise/passwords#update
                         PUT    /password(.:format)         devise/passwords#update
                         POST   /password(.:format)         devise/passwords#create
cancel_user_registration GET    /cancel(.:format)           devise/registrations#cancel
   new_user_registration GET    /sign_up(.:format)          devise/registrations#new
  edit_user_registration GET    /profile(.:format)          devise/registrations#edit
       user_registration PATCH  /                           devise/registrations#update
                         PUT    /                           devise/registrations#update
                         DELETE /                           devise/registrations#destroy
                         POST   /                           devise/registrations#create
                    root GET    /                           pages#dashboard

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 simple-form


    【解决方案1】:

    因为您的站点模型属于客户。您必须将站点与客户端关联,它的工作原理如下:

    client = Clinet.find(some_id)
    site = client.sites.new(params)
    site.save
    

    或者你可以build表单的空对象:

    # GET /sites/new
    def new
      client = Clinet.find(some_id) # If you using resources just take id from params  
      @site = client.sites.build
    end
    

    更新

    好的,看起来你的表单使用了 GET 方法和错误的路径:

    发起者GET“/位点/新?UTF8 =%E2%9C%93&authenticity_token = YCl94xAEq8Ph%2F9Y5dbBOKoKeZnWVvJdw9khi%2Fotjnta4Qt%2BkJ9kYUlze%2FtaOyIzFG7YvT2MfJEr%2FYWNzrZ5VPw%3D%3D&站点%5Bname%5D = HGH&站点%5Bclient_id%5D = 8276d1d3-9a79- 4c27-94f0-346c044c2592&commit=Create+Site" for 127.0.0.1 at 2018-04-08 08:58:58 +1000

    尝试在create方法中添加method: :post和path_name:

    <%= simple_form_for @site, url: sites_path, method: :post do |form| %>
    

    【讨论】:

    • 刚刚尝试了您的更改...没有保存。仍然没有错误。我实际上并不认为这是正确的,因为在我能够创建网站的简单表单之前
    • @JeremyBray 没有错误在哪里?我想你可以在 rails 控制台看到ROLLBACK。您不能在没有关联的情况下创建 belongs_to 对象。只需尝试在rails c 中执行此操作
    • 关联的形式是什么?没有回滚,我将更新以显示
    • 为你添加了日志
    • @JeremyBray 对。我的错,没看到。
    猜你喜欢
    • 2021-10-10
    • 2016-01-22
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多