haml 的问题我在 rails 上研究 Ruby 偶然发现了以下内容
problem with haml I study Ruby on rails stumbled where with the following
Posts#index 中的名称错误
显示 /home/alexandr/Huntjob/app/views/posts/index.html.haml 第 4 行出现的位置:
#<#:0x00005621a21d4538> 的未定义局部变量或方法“haml_temp”
你的意思? haml_tag
enter image description here
错误代码
- @posts.each do |post|
=link_to (image_tag post.image.url(:small))
%h2= link_to post.title, post
= link_to "Add New Inspiration", new_post_path
Post控制器
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@posts = Post.all.order("created_at")
end
def show
end
def new
@post = current_user.posts.build
end
def create
@post = current_user.posts.build(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
def edit
end
def update
if @post.update(post_params)
redirect_to @post
else
render 'edit'
end
end
def destroy
@post.destroy
redirect_to root_path
end
private
def find_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :link, :description, :image)
end
end
HAML 对缩进敏感。从屏幕截图中我看到问题出在缩进中。
应该可行:
- @posts.each do |post|
= link_to(image_tag post.image.url(:small))
%h2= link_to post.title, post
= link_to "Add New Inspiration", new_post_path
确保第 2 行和第 3 行之前有两个空格。
Posts#index 中的名称错误 显示 /home/alexandr/Huntjob/app/views/posts/index.html.haml 第 4 行出现的位置:
#<#:0x00005621a21d4538> 的未定义局部变量或方法“haml_temp” 你的意思? haml_tag enter image description here
错误代码
- @posts.each do |post|
=link_to (image_tag post.image.url(:small))
%h2= link_to post.title, post
= link_to "Add New Inspiration", new_post_path
Post控制器
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@posts = Post.all.order("created_at")
end
def show
end
def new
@post = current_user.posts.build
end
def create
@post = current_user.posts.build(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
def edit
end
def update
if @post.update(post_params)
redirect_to @post
else
render 'edit'
end
end
def destroy
@post.destroy
redirect_to root_path
end
private
def find_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :link, :description, :image)
end
end
HAML 对缩进敏感。从屏幕截图中我看到问题出在缩进中。
应该可行:
- @posts.each do |post|
= link_to(image_tag post.image.url(:small))
%h2= link_to post.title, post
= link_to "Add New Inspiration", new_post_path
确保第 2 行和第 3 行之前有两个空格。