0

当我提交表单时,我收到一个错误:缺少模板letsgos/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby]}.

我不明白为什么当 create 的视图是from _form.html.erb. 我不需要制作 /create 文件。我在这里错过了什么吗?

letsgos_controller.rb:

  before_action :signed_in_user, only: [:create, :destroy]

  def create
    @letsgo = current_user.letsgos.build(letsgo_params)
    if @letsgo.save
      flash[:success] = "Date posted!"
      redirect_to root_url
end

  def destroy
    @letsgo.destroy
    redirect_to root_url
  end

用户控制器:

def show
    @user = User.find(params[:id])
    @letsgos = @user.letsgos.paginate(page: params[:page])
    @letsgo = current_user.letsgos.build if signed_in?'
  end

Letsgo.rb:

  belongs_to :user
  default_scope -> { order('created_at DESC') }
  validates :content, presence: true, length: { maximum: 360 }
  validates :tag, presence: true
  validates :user_id, presence: true
end

/users/show.html.erb:

<% if @user.letsgos.any? %>
    Dates (<%= @user.letsgos.count %>)
    <%= render @letsgos %>
    <%= will_paginate @letsgos %>
    <% end %>

/letsgos/_form.html.erb

<%= form_for(@letsgo) do |f| %>
    <div class="field">
        <%= f.text_area :content, placeholder: "Propose new date..." %>
        </div>
        <%= f.submit "Post", class: "btn btn-large btn-primary" %>
        <% end %>

/letsgos/home.html.erb:

<% if signed_in %>
    <% render 'letsgos/form' %>
4

1 回答 1

2

在里面letsgoes_controller.rb你有以下创建方法:

  def create
    @letsgo = current_user.letsgos.build(letsgo_params)
    if @letsgo.save
      flash[:success] = "Date posted!"
      redirect_to root_url
    end
  end

根据该方法,如果 @letsgo.save 返回 false(由于验证或其他原因),则不会执行该代码,这意味着 rails 将尝试呈现与控制器操作同名的视图文件 -create在这种情况下。所以你需要考虑if. 像这样的东西会起作用:

  def create
    @letsgo = current_user.letsgos.build(letsgo_params)
    if @letsgo.save
      flash[:success] = "Date posted!"
      redirect_to root_url
    else
      flash[:error] = "Date was not posted!"
      redirect_to root_url
    end
  end
于 2013-09-24T15:27:24.250 回答