Rails 5.2 不呈现新视图

Posted

技术标签:

【中文标题】Rails 5.2 不呈现新视图【英文标题】:Rails 5.2 doesn't render new view 【发布时间】:2018-03-24 11:50:20 【问题描述】:

我有一个脚手架控制器。在该状态下,当它被调用时,它不会呈现任何视图。如果我删除@event = Event.new,它会调用我的视图new

  # GET /admin/events/new
  def new
    @event = Event.new
  end

Started GET "/admin/events/new" for 127.0.0.1 at 2018-03-24 12:35:21 +0100
Processing by Admin::EventsController#new as html
  Admin Load (0.5ms)  SELECT  "admins".* FROM "admins" WHERE "admins"."id" = ? ORDER BY "admins"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/florentbeaurain/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/activerecord-5.2.0.rc1/lib/active_record/log_subscriber.rb:98
Completed 200 OK in 6208ms (ActiveRecord: 0.5ms)

没有视图渲染。

  # GET /admin/events/new
  def new
  end

Started GET "/admin/events/new" for 127.0.0.1 at 2018-03-24 12:38:17 +0100
Processing by Admin::EventsController#new as HTML
  Admin Load (0.2ms)  SELECT  "admins".* FROM "admins" WHERE "admins"."id" = ? ORDER BY "admins"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/florentbeaurain/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/activerecord-5.2.0.rc1/lib/active_record/log_subscriber.rb:98
  Rendering admin/events/new.html.erb within layouts/admin
  Rendered admin/events/_form.html.erb (2.9ms)
  Rendered admin/events/new.html.erb within layouts/admin (5.0ms)
Completed 500 Internal Server Error in 27ms (ActiveRecord: 0.8ms)

查看渲染。

更新 1:

class ApplicationController < ActionController::Base
end

class Admin::ApplicationController < ApplicationController
  layout 'admin'

  before_action :authenticate_admin!
end

class Admin::EventsController < Admin::ApplicationController
  before_action :set_event, only: [:show, :edit, :update, :destroy]

  # GET /events
  def index
    @events = Event.includes(:tap).all
  end

  # GET /events/1
  def show
  end

  # GET /events/new
  def new
    @event = Event.new
  end

  # GET /events/1/edit
  def edit
  end

  # POST /events
  def create
    @event = Event.new(event_params)

    if @event.save
      redirect_to admin_event_path(@event), notice: 'Event was successfully created.'
    else
      render :new
    end
  end

  # PATCH/PUT /events/1
  def update
    if @event.update(event_params)
      redirect_to admin_event_path(@event), notice: 'Event was successfully updated.'
    else
      render :edit
    end
  end

  # DELETE /events/1
  def destroy
    @event.destroy
    redirect_to admin_events_url, notice: 'Event was successfully destroyed.'
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def event_params
      params.require(:event).permit(:tap_id, :kind)
    end
end

【问题讨论】:

请分享整个控制器 @cousine 完成! :) 【参考方案1】:

好的。我的事件模型有一个 belongs_to :tap 与 tap() 方法冲突。

解决了。

【讨论】:

以上是关于Rails 5.2 不呈现新视图的主要内容,如果未能解决你的问题,请参考以下文章

尝试在我的产品视图中在 Rails 中呈现类别

在视图中呈现现有类别,rails

Rails 在视图呈现后登录用户

Rails 3.1:视图不在命名空间布局中呈现

如何在 Rails 视图中呈现所有评论?

解决 Rails 3.2 中控制器的视图路径