ActionView :: MissingTemplate用于创建操作

时间:2013-09-24 15:08:21

标签: ruby-on-rails

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

我不理解的是,当创建的视图为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

users_controller:

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' %>

1 个答案:

答案 0 :(得分: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将尝试呈现与控制器操作同名的视图文件 - { {1}}在这种情况下。因此,您需要考虑create的错误情况。像这样的东西会起作用:

if