"Template missing error " 在 rails
"Template missing error " in rails
我是 rails 的初学者
在 routes.rb
我有
root 'post#index'
resources :posts
当我为 post 的空标题单击 "new post" 时,我收到类似
的错误
Missing template posts/new, application/new with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/supranimbus12/RoR/instagram_app/app/views" * "/home/supranimbus12/.rvm/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/devise-4.4.0/app/views"
我的 posts/new.html.erb
文件类似于
<h2>New Post</h2>
<hr>
<%= form_for @post do |f| %>
<% f.label :description %>
<% f.textarea :description %>
<hr>
<% f.submit %>
<% end %>
1- 将路线更改为:-
root 'post#index'
resources :posts, except: [:index]
因为您已经定义为索引操作的根
2- 现在进行新操作
def new
@post = Post.new
end
3-在你app/view/posts/new.html.erb
<h2>New Post</h2>
<hr>
<%= form_for @post do |f| %>
<% f.label :description %>
<% f.textarea :description %>
<hr>
<% f.submit %>
<% end %>
3 - 点击提交按钮后,它将创建操作,
注意:- 因为如果 object
是新的,form_for object
会自动点击创建动作,否则它会点击 update
动作,这就是为什么我们通常使用这种形式作为部分来使用它编辑和新动作都
def create
@post = Post.new(post_params)
if @post.save
flash[:notice] = "post saved successfully!"
redirect_to @post
else
flash[:error] = @post.errors.full_messages.to_sentence
render 'new'
end
private
def post_params
params.require(:post).permit!
end
允许所有数据为强参数创建私有方法
post_params
如果 post 保存在数据库中,redirect_to @post
将转到 show
操作,因为它会使 url 像 post/:id
否则它将呈现再次到 new
模板。带有错误闪现消息。
我是 rails 的初学者
在 routes.rb
我有
root 'post#index'
resources :posts
当我为 post 的空标题单击 "new post" 时,我收到类似
的错误Missing template posts/new, application/new with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/supranimbus12/RoR/instagram_app/app/views" * "/home/supranimbus12/.rvm/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/devise-4.4.0/app/views"
我的 posts/new.html.erb
文件类似于
<h2>New Post</h2>
<hr>
<%= form_for @post do |f| %>
<% f.label :description %>
<% f.textarea :description %>
<hr>
<% f.submit %>
<% end %>
1- 将路线更改为:-
root 'post#index'
resources :posts, except: [:index]
因为您已经定义为索引操作的根
2- 现在进行新操作
def new
@post = Post.new
end
3-在你app/view/posts/new.html.erb
<h2>New Post</h2>
<hr>
<%= form_for @post do |f| %>
<% f.label :description %>
<% f.textarea :description %>
<hr>
<% f.submit %>
<% end %>
3 - 点击提交按钮后,它将创建操作,
注意:- 因为如果 object
是新的,form_for object
会自动点击创建动作,否则它会点击 update
动作,这就是为什么我们通常使用这种形式作为部分来使用它编辑和新动作都
def create
@post = Post.new(post_params)
if @post.save
flash[:notice] = "post saved successfully!"
redirect_to @post
else
flash[:error] = @post.errors.full_messages.to_sentence
render 'new'
end
private
def post_params
params.require(:post).permit!
end
允许所有数据为强参数创建私有方法
post_params
如果 post 保存在数据库中,redirect_to @post
将转到 show
操作,因为它会使 url 像 post/:id
否则它将呈现再次到 new
模板。带有错误闪现消息。