ruby-on-rails 如何从frontend rails 7提交命名空间表单

s2j5cfk0  于 4个月前  发布在  Ruby
关注(0)|答案(1)|浏览(66)

嗨,我提交了一个表单从前端到管理命名空间,但我不能得到验证工作。
这是我的模型:

class Collaboration < ApplicationRecord
 validates :first_name, :last_name, :profession, :email, :presentation, :curriculum_vitae, presence: true
 has_one_attached :curriculum_vitae  
end

字符串
我的后端控制器:

class Admin::CollaborationsController < Admin::BaseController
skip_before_action :require_admin, only: [:index, :new, :create, :show]
skip_before_action :require_user, only: [:new, :create]
before_action :set_collaboration, only: %i[ show edit update destroy ]

# GET /admin/people or /admin/people.json
def index
  @collaborations = Collaboration.all
end

# GET /admin/people/1 or /admin/people/1.json
def show
end

# GET /admin/people/new
def new
  @collaboration = Collaboration.new
end

# GET /admin/people/1/edit
def edit
end

# POST /admin/people or /admin/people.json
def create
  @collaboration = Collaboration.new(collaboration_params)

  respond_to do |format|
    if @collaboration.save 
      format.html { redirect_to root_path, notice: "Collaboration was successfully submited." }
    
    else
      format.html { render "pages/work_with_us", status: :unprocessable_entity }
      format.json { render json: @collaboration.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /admin/people/1 or /admin/people/1.json
def update
  respond_to do |format|
    if @collaboration.update(collaboration_params)
      format.html { redirect_to admin_collaboration_url(@collaboration), notice: "Collaboration was successfully updated." }
      format.json { render :show, status: :ok, location: @collaboration }
    else
      format.html { render :edit, status: :unprocessable_entity }
      format.json { render json: @collaboration.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /admin/people/1 or /admin/people/1.json
def destroy
  @collaboration.destroy

  respond_to do |format|
    format.html { redirect_to admin_collaborations_url, notice: "Collaboration was successfully destroyed." }
    format.json { head :no_content }
  end
end

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

  # Only allow a list of trusted parameters through.
  def collaboration_params
    params.require(:collaboration).permit(:first_name, :last_name, :profession, :email, :phone_number, :presentation, :field, :curriculum_vitae)
  end

end


前端控制器:

class PagesController < ApplicationController
  def home
    @title = "AscheriGroup - Home"
  end

  def services
    @title = "AscheriGroup - Servizi"
    @q = Service.ransack(params[:q])
    @services = @q.result(distinct: true).order("service_order asc") 
    
  end

  def team
    @title = "AscheriGroup - Il Team"
    @designated_members = Person.designated_member
    @members = Person.member.or(Person.designated_member).order('person_order asc')
    @associates = Person.associate
    @of_counsels = Person.of_counsel
    @consultants = Person.consultant
  end

  def offices
    @title = "AscheriGroup - Gli Uffici"
  end

  def contact
    @title = "AscheriGroup - Contatti"
  end

  def group
    @title = "AscheriGroup - Il Gruppo "
  end

  def about
    @title = "AscheriGroup - About"
  end

  def work_with_us
    @title = "AscheriGroup - Lavora con noi"
    @collaboration = Collaboration.new
  end

  
  
end


在前端视图中:

<%= form_with(model: [:admin, @collaboration]) do |form| %> 
            <% if @collaboration.errors.any? %>
                <% @collaboration.errors.full_messages.each do |msg| %>
                    <div class="alert alert-danger alert-dismissible fade show" role="alert"><%= msg %>
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">
                    </button>
                    </div>
                <% end %>
            <% end %>
                <div class="row mb-3">
                <div class="col">
                    <%= form.label :first_name, class:"form-label" %>
                    <%= form.text_field :first_name, class:"form-control" %>
                </div>

                <div class="col">
                    <%= form.label :last_name, class:"form-label" %>
                    <%= form.text_field :last_name, class:"form-control" %>
                </div>
                </div>
                <div class="row mb-3">
                <div class="col">
                    <%= form.label :profession, class:"form-label" %>
                    <%= form.text_field :profession, class:"form-control" %>
                </div>

                <div class="col">
                    <%= form.label :email, class:"form-label" %>
                    <%= form.text_field :email, class:"form-control" %>
                </div>

                <div class="col">
                    <%= form.label :phone_number, class:"form-label" %>
                    <%= form.text_field :phone_number, class:"form-control" %>
                </div>
                </div>

                <div class="row mb-3">
                <div class="col">
                    <%= form.label "In che ambito ti interessa collaborare?", class:"form-label" %>
                    <%= form.text_field :field, class:"form-control" %>
                </div>

                <div class="col">
                    <%= form.label :curriculum_vitae, class:"form-label" %>
                    <%= form.file_field :curriculum_vitae, class:"form-control" %>
                </div>
                </div>
                <div class="row mb-3" data-controller="tinymce">
                <div class="col" >
                    <%= form.label :presentation, class:"form-label" %>
                    <%= form.text_area :presentation, class:"form-control tinymce" %>
                </div>      
                </div>    
                <%= hidden_field_tag(:lavora, 'front_end') %>
                <div class="d-flex justify-content-center mb-3">
                <%= form.submit(@collaboration.new_record? ? "Create collaboration" : "Update collaboration", class: " btn btn-outline-success btn-sm") %>
                </div>
            <% end %>

        </div>


“我可以保存for中的数据,但如果我提交空表单,就看不到错误消息。
有人能给我指个路吗?
这是日志

Started POST "/admin/collaborations" for ::1 at 2023-11-22 16:23:28 +0000
Processing by Admin::CollaborationsController#create as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "collaboration"=>{"first_name"=>"", "last_name"=>"", "profession"=>"", "email"=>"", "phone_number"=>"", "field"=>"", "presentation"=>""}, "lavora"=>"update", "commit"=>"Create collaboration"}
  Rendering layout layouts/admin/base.html.erb
  Rendering pages/work_with_us.html.erb within layouts/admin/base
  Rendered partials/_alert_msg.html.erb (Duration: 0.1ms | Allocations: 38)
  Rendered partials/_errors.html.erb (Duration: 1.0ms | Allocations: 2064)
  Rendered admin/collaborations/_form.html.erb (Duration: 3.6ms | Allocations: 7669)
  Rendered pages/work_with_us.html.erb within layouts/admin/base (Duration: 33.3ms | Allocations: 9127)
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/admin/base_controller.rb:10:in `current_user'
  Rendered partials/_navbar_admin.html.erb (Duration: 12.8ms | Allocations: 6061)
  Rendered partials/_alert_msg.html.erb (Duration: 0.0ms | Allocations: 17)
  Rendered layout layouts/admin/base.html.erb (Duration: 88.4ms | Allocations: 19026)
Completed 422 Unprocessable Entity in 116ms (Views: 89.1ms | ActiveRecord: 0.4ms | Allocations: 22984)

Started GET "/pages/work_with_us" for ::1 at 2023-11-22 16:23:28 +0000
Processing by PagesController#work_with_us as HTML
  Rendering layout layouts/application.html.erb
  Rendering pages/work_with_us.html.erb within layouts/application
  Rendered partials/_alert_msg.html.erb (Duration: 0.2ms | Allocations: 287)
  Rendered partials/_errors.html.erb (Duration: 0.0ms | Allocations: 14)
  Rendered admin/collaborations/_form.html.erb (Duration: 1.6ms | Allocations: 2376)
  Rendered pages/work_with_us.html.erb within layouts/application (Duration: 44.8ms | Allocations: 3999)
  Rendered partials/_navbar.html.erb (Duration: 0.3ms | Allocations: 234)
  Rendered partials/_footer.html.erb (Duration: 0.1ms | Allocations: 107)
  Rendered layout layouts/application.html.erb (Duration: 77.9ms | Allocations: 7015)
Completed 200 OK in 97ms (Views: 78.7ms | ActiveRecord: 0.0ms | Allocations: 7350)

sd2nnvve

sd2nnvve1#

感谢Bouaik询问日志,我观察到渲染使用了管理布局。

format.html { render 'pages/work_with_us', layout: "application", status: :unprocessable_entity }

字符串
我只需要指定布局

相关问题