零、前言
本人不擅长写汇编相关的东西,所以以下内容也是不断摸索(百度 ) 整出来的,和linux的实验报告的质量相比较低。
一、 实验目的
二、 实验设备
三、 实验任务与要求



一定要确认自己使用的 keil 版本,必须是 keil3 for arm
** keil 编辑时可能会出现1**
;3.1_asm1\Asm1_a 中的 asm1_a.s 文件
x EQU 45
y EQU 64
stack_top EQU 0x30200000
export Reset_Handler
AREA text,CODE,READONLY
export
Reset_Handler
ldr sp, =stack_top
mov r0, #x
str r0, [sp]
mov r0, #y
ldr r1, [sp]
add r0, r0, r1
str r0, [sp]
stop
b stop
end
(1) 请按照嵌入式程序调试步骤进行单步执行,并将每一步执行的寄存器值及内存中的结果截图。
asm_1a:

常量定义,类似于C语言中的 #define
x EQU 45 意味着之后程序中 出现的 x 将会在预处理阶段被替换为 45
y EQU 64 意味着之后程序中 出现的 y 将会在预处理阶段被替换为 64
stack_top EQU 0x30200000 意味着之后程序中 出现的 stack_top 将会在预处理阶段被替换为 0x30200000

export Reset_Handler 表示 Reset_Handler 这个子程序可供其他模块调用。

定义一个代码段text,CODE 代表它要定义的是一个代码段,READONLY 指定本段为只读,(代码段默认为READONLY)
此处参考来源:https://www.cnblogs.com/qiyuexin/p/12608776.html
此处代码可以debug
step 0:

此时程序走在第一步(还未执行),上图所示都是初始状态下的情况。
step 1:ldr sp, =stack_top

step 2:mov r0, #x 把x的值给 R0

step 3:str r0, [sp] 将 R0 的值保存到栈中

step 4:mov r0, #y把y的值给 R0

step 5:ldr r1, [sp] 从堆栈中读取数据,并将其放入 R1

step 6:add r0, r0, r1 把 R0 和 R1 的值加起来

step 7:str r0, [sp]

step 8:b stop 代码结束

asm_1b

常量定义,类似于C语言中的 #define
x EQU 45 意味着之后程序中 出现的 x 将会在预处理阶段被替换为 45
y EQU 64 意味着之后程序中 出现的 y 将会在预处理阶段被替换为 64
z EQU 87 意味着之后程序中 出现的 z 将会在预处理阶段被替换为 87
stack_top EQU 0x30200000 意味着之后程序中 出现的 stack_top 将会在预处理阶段被替换为 0x30200000

export Reset_Handler 表示 Reset_Handler 这个子程序可供其他模块调用。

定义一个代码段text,CODE 代表它要定义的是一个代码段,READONLY 指定本段为只读,(代码段默认为READONLY)
此处参考来源:https://www.cnblogs.com/qiyuexin/p/12608776.html
此处代码可以debug
step 0:

step 1:mov r0, #x 把x的值放到 r0

step 2: mov r0, r0, lsl #8 x = x << 8 左移八位

step 3: mov r1, #y 把y的值放入 r1 中

step 4: add r2, r0, r1, lsr #1 R2 = (R1>>1) + R0

step 5: ldr sp, =stack_top

step 6: str r2, [sp]

step 7: mov r0, #z 把z的值放到 r0

step 8: and r0, r0, #0xFF 从 r0 中获得低八位

step 9: mov r1, #y y的值放入r1 中

step 10: add r2, r0, r1, lsr #1 R2 =(R1>>1)+ R0

step 11: ldr r0, [sp]

step 12: mov r1, #0x01

step 13:orr r0, r0, r1

step 14:mov r1, R2 把y的值放入r1 中

step 15: add r2, r0, r1, lsr #1 R2 =(R1>>1) + R0

step 16: b stop 结束代码

(2) 在上面的代码基础上进行实验的拓展,加入ADD、SUB、AND、ORR等指令,并加上寄存器移位寻址方式。
(这里我仅在 asm_1a.s文件里进行了拓展)

; ADD
ldr r0, =0x20
ldr r1, =0x30
add r2, r0, r1
; SUB
ldr r0, =0x3f
ldr r1, =0x1d
sub r2, r0, r1
; AND
MOV R0,#0x02
AND R0,#0x01
; ORR
MOV R0,#0x01
ORR R0,R0,#0x0F
;寄存器移位寻址
;LSL 逻辑左移
MOV R1, #0x02
MOV R2, #0x03
ADD R0,R1,R2,LSL #2
更改完毕之后,点击此处重新Build

然后在下方的控制台查看是否build成功

这时候就可以 Debug -> Start Debug 然后挨个执行指令了。
(3) 在上面的代码基础上进行LDR/STR指令对内存随机访问的指令,需要在程序中利用数据定义伪操作定义字数据并分配内存。

缩进很重要!缩进不对无法编译
;new
ldr R0, =data1
ldr R1, =data2
ldr R3, [R0]
str r4, [R1]
LTORG
data1
DCD 0x123
data2
DCD 0
(4) LDR/STR指令访问内存时分别采用寄存器间接寻址方式、基址变址寻址方式中的前索引偏移和后索引偏移进行设计,并将结果截图。
前索引偏移:
AREA text,CODE,READONLY
Reset_Handler
ldr r4,=0x3c
ldr r0,[r4,#8]
mov r6,#2
ldr r0,[r4, r6]
ldr r0, [r4,r6,LSL #2]
stop
b stop
END

后索引偏移:
AREA text,CODE,READONLY
Reset_Handler
ldr r2, =0x68
str r1, [r2], #4
str r3, [r2], #8
stop
b stop
END

(5) 编写程序循环对 R4~R11 进行累加 8 次赋值,R4~R11 起始值为 1~8,每次加操作后把 R4~R11 的内容放入 SP 栈中。

stack_top EQU 0x00800000
export Reset_Handler
AREA text,CODE,READONLY
Reset_Handler
mov sp,#stack_top
mov r4,#1
mov r5,#2
mov r6,#3
mov r7,#4
mov r8,#5
mov r9,#5
mov r10,#7
mov r11,#8
stmfd sp!,{r4-r11}
mov r3,#0
stop
b stop
END
以下是 ChatGPT给出的解释(一种AI程序):
asm_code1.s:
asm_code2.s:
asm_code1.s:
GLOBAL Reset_Handler
area start,code,readwrite
entry
code32
num EQU 20 ;/* 设置要复制的字数 */
Reset_Handler
ldr r0, =src ;/* R0 指向源数据块首地址 */
ldr r1, =dst ;/* R1 指向目标数据块首地址 */
mov r2, #num ;/* R2 复制的字数 */
ldr sp, =0x30200000 ;/* 设置堆栈指针,用于保存工作寄存器数值 */
blockcopy
movs r3,r2, LSR #3 ;/* 需要以8 个字为单位的数据复制 */
beq copywords ;/* 如果移动的字的个数小于 8 则跳转到 copywords _子函数 */
stmfd sp!, {r4-r11} ;/* 保存工作寄存器 */
octcopy
ldmia r0!, {r4-r11} ;/* 从数据源取出 8 个字数据放到寄存器,更新 RO */
stmia r1!, {r4-r11} ;/*把数据写入目的地址,更新 R1 */
subs r3, r3, #1 ;/* 将块复制制次数减 1 */
bne octcopy ;/* 循环直到完成八个字为单位的人复制 */
ldmfd sp!, {r4-r11} ;/* 恢复工作寄存器 */
copywords
ands r2, r2, #7 ;/*剩余不足 8 个字数据的字数 */
beq stop ;/* 数据复制完成则跳转到 stop */
wordcopy
ldr r3, [r0], #4 ;/* 从数据源取出一个字数据到 R3,且更新 R3 */
str r3, [r1], #4 ;/* 将字数据存储到目标地址,更新 R1 */
subs r2, r2, #1 ;/* 字复制次数减 1 */
bne wordcopy ;/* 循环到复制结束 */
stop
b stop
;/*------------------------------------------------------------------------------------------*/
;/* make a word pool */
;/*------------------------------------------------------------------------------------------*/
ltorg
src
dcd 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4
dst
dcd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
end
asm_code2.s
area start,code,readwrite
entry
code32
num equ 2
export Reset_Handler
Reset_Handler
;/* 设置三个参数 */
mov r0, #0 ;/* 把 0 传送到 R0 寄存器中 */
mov r1, #3 ;/* 把 3 传送到 R1 寄存器中 */
mov r2, #2 ;/* 把 2 传送到 R2 寄存器中 */
bl arithfunc ;/* 调用函数 */
stop
b stop
;# ********************************************************************************************
;# * According R0 valude to execute the code *
;# ********************************************************************************************
arithfunc ;/* 标识函数 */
cmp r0, #num ;/* 将函数代码视为无符号整数,R0 与立即数 NUM 进行比较 */
bhs DoAdd ;/* 如果大于等于 2,则执行操作 0 */
adr r3, JumpTable ;/* 根据参数 R0 的值跳转到对应的子程序 */
ldr pc, [r3,r0,LSL#2] ;/* 跳转到适当的路线 */
JumpTable
dcd DoAdd ;/*占领四个字节,其中存储的是 DoAdd 对应的地址 */
dcd DoSub ;/* 占领四个字节,其中存储的是 DoSub.对应的地址 */
DoAdd
add r0, r1, r2 ;/* R1+R2->R0 */
bx lr ;/* 跳转到 lr 中存放的地址处*/
DoSub
sub r0, r1, r2 ;/* R0-R1->R3 */
bx lr ;/* 跳转到 lr 中存放的地址处 */
end ;/* 结束程序 */
(2) 将运行结果的寄存器值及内存中的查看结果截图;
asm_code1.s:

asm_code2.s:

(3) 将asm_code1.s代码中的进栈、出栈指令STMIA/LDMIA分别改成STMIB/LDMIB、STMDA/LDMDA、STMDB/LDMDB,并将运行结果截图。
提示:这里的改成就是一种简单的文字替换,用CTRL+H逐个替换即可
;#*********************************************************************************************
;# NAME: ARMcode.s *
;# Author: EWUHAN R & D Center, st *
;# Desc: ARMcode examples *
;# copy words from src to dst *
;# History: shw.He 2005.02.22 *
;#*********************************************************************************************
;/*------------------------------------------------------------------------------------------*/
;/* code */
;/*------------------------------------------------------------------------------------------*/
GLOBAL Reset_Handler
area start,code,readwrite
entry
code32
num EQU 20 ;/* Set number of words to be copied */
Reset_Handler
ldr r0, =src ;/* r0 = pointer to source block */
ldr r1, =dst ;/* r1 = pointer to destination block */
mov r2, #num ;/* r2 = number of words to copy */
ldr sp, =0x30200000 ;/* set up stack pointer (r13) */
blockcopy
movs r3,r2, LSR #3 ;/* number of eight word multiples */
beq copywords ;/* less than eight words to move ? */
stmfd sp!, {r4-r11} ;/* save some working registers */
octcopy
LDMIB r0!, {r4-r11} ;/* load 8 words from the source */
STMIB r1!, {r4-r11} ;/* and put them at the destination */
subs r3, r3, #1 ;/* decrement the counter */
bne octcopy ;/* ... copy more */
ldmfd sp!, {r4-r11} ;/* don't need these now - restore originals */
copywords
ands r2, r2, #7 ;/* number of odd words to copy */
beq stop ;/* No words left to copy ? */
wordcopy
ldr r3, [r0], #4 ;/* a word from the source */
str r3, [r1], #4 ;/* store a word to the destination */
subs r2, r2, #1 ;/* decrement the counter */
bne wordcopy ;/* ... copy more */
stop
b stop
;/*------------------------------------------------------------------------------------------*/
;/* make a word pool */
;/*------------------------------------------------------------------------------------------*/
ltorg
src
dcd 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4
dst
dcd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
end

;#*********************************************************************************************
;# NAME: ARMcode.s *
;# Author: EWUHAN R & D Center, st *
;# Desc: ARMcode examples *
;# copy words from src to dst *
;# History: shw.He 2005.02.22 *
;#*********************************************************************************************
;/*------------------------------------------------------------------------------------------*/
;/* code */
;/*------------------------------------------------------------------------------------------*/
GLOBAL Reset_Handler
area start,code,readwrite
entry
code32
num EQU 20 ;/* Set number of words to be copied */
Reset_Handler
ldr r0, =src ;/* r0 = pointer to source block */
ldr r1, =dst ;/* r1 = pointer to destination block */
mov r2, #num ;/* r2 = number of words to copy */
ldr sp, =0x30200000 ;/* set up stack pointer (r13) */
blockcopy
movs r3,r2, LSR #3 ;/* number of eight word multiples */
beq copywords ;/* less than eight words to move ? */
stmfd sp!, {r4-r11} ;/* save some working registers */
octcopy
LDMDA r0!, {r4-r11} ;/* load 8 words from the source */
STMDA r1!, {r4-r11} ;/* and put them at the destination */
subs r3, r3, #1 ;/* decrement the counter */
bne octcopy ;/* ... copy more */
ldmfd sp!, {r4-r11} ;/* don't need these now - restore originals */
copywords
ands r2, r2, #7 ;/* number of odd words to copy */
beq stop ;/* No words left to copy ? */
wordcopy
ldr r3, [r0], #4 ;/* a word from the source */
str r3, [r1], #4 ;/* store a word to the destination */
subs r2, r2, #1 ;/* decrement the counter */
bne wordcopy ;/* ... copy more */
stop
b stop
;/*------------------------------------------------------------------------------------------*/
;/* make a word pool */
;/*------------------------------------------------------------------------------------------*/
ltorg
src
dcd 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4
dst
dcd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
end

;#*********************************************************************************************
;# NAME: ARMcode.s *
;# Author: EWUHAN R & D Center, st *
;# Desc: ARMcode examples *
;# copy words from src to dst *
;# History: shw.He 2005.02.22 *
;#*********************************************************************************************
;/*------------------------------------------------------------------------------------------*/
;/* code */
;/*------------------------------------------------------------------------------------------*/
GLOBAL Reset_Handler
area start,code,readwrite
entry
code32
num EQU 20 ;/* Set number of words to be copied */
Reset_Handler
ldr r0, =src ;/* r0 = pointer to source block */
ldr r1, =dst ;/* r1 = pointer to destination block */
mov r2, #num ;/* r2 = number of words to copy */
ldr sp, =0x30200000 ;/* set up stack pointer (r13) */
blockcopy
movs r3,r2, LSR #3 ;/* number of eight word multiples */
beq copywords ;/* less than eight words to move ? */
stmfd sp!, {r4-r11} ;/* save some working registers */
octcopy
ldmdb r0!, {r4-r11} ;/* load 8 words from the source */
stmdb r1!, {r4-r11} ;/* and put them at the destination */
subs r3, r3, #1 ;/* decrement the counter */
bne octcopy ;/* ... copy more */
ldmfd sp!, {r4-r11} ;/* don't need these now - restore originals */
copywords
ands r2, r2, #7 ;/* number of odd words to copy */
beq stop ;/* No words left to copy ? */
wordcopy
ldr r3, [r0], #4 ;/* a word from the source */
str r3, [r1], #4 ;/* store a word to the destination */
subs r2, r2, #1 ;/* decrement the counter */
bne wordcopy ;/* ... copy more */
stop
b stop
;/*------------------------------------------------------------------------------------------*/
;/* make a word pool */
;/*------------------------------------------------------------------------------------------*/
ltorg
src
dcd 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4
dst
dcd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
end

(4) 将asm_code1.s代码中的进栈、出栈指令STMFD/LDMFD分别改成STMFA/LDMFA、STMEA/LDMEA、STMED/LDMED,并将运行结果截图。
;#*********************************************************************************************
;# NAME: ARMcode.s *
;# Author: EWUHAN R & D Center, st *
;# Desc: ARMcode examples *
;# copy words from src to dst *
;# History: shw.He 2005.02.22 *
;#*********************************************************************************************
;/*------------------------------------------------------------------------------------------*/
;/* code */
;/*------------------------------------------------------------------------------------------*/
GLOBAL Reset_Handler
area start,code,readwrite
entry
code32
num EQU 20 ;/* Set number of words to be copied */
Reset_Handler
ldr r0, =src ;/* r0 = pointer to source block */
ldr r1, =dst ;/* r1 = pointer to destination block */
mov r2, #num ;/* r2 = number of words to copy */
ldr sp, =0x30200000 ;/* set up stack pointer (r13) */
blockcopy
movs r3,r2, LSR #3 ;/* number of eight word multiples */
beq copywords ;/* less than eight words to move ? */
stmfa sp!, {r4-r11} ;/* save some working registers */
octcopy
ldmia r0!, {r4-r11} ;/* load 8 words from the source */
stmia r1!, {r4-r11} ;/* and put them at the destination */
subs r3, r3, #1 ;/* decrement the counter */
bne octcopy ;/* ... copy more */
ldmfa sp!, {r4-r11} ;/* don't need these now - restore originals */
copywords
ands r2, r2, #7 ;/* number of odd words to copy */
beq stop ;/* No words left to copy ? */
wordcopy
ldr r3, [r0], #4 ;/* a word from the source */
str r3, [r1], #4 ;/* store a word to the destination */
subs r2, r2, #1 ;/* decrement the counter */
bne wordcopy ;/* ... copy more */
stop
b stop
;/*------------------------------------------------------------------------------------------*/
;/* make a word pool */
;/*------------------------------------------------------------------------------------------*/
ltorg
src
dcd 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4
dst
dcd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
end

;#*********************************************************************************************
;# NAME: ARMcode.s *
;# Author: EWUHAN R & D Center, st *
;# Desc: ARMcode examples *
;# copy words from src to dst *
;# History: shw.He 2005.02.22 *
;#*********************************************************************************************
;/*------------------------------------------------------------------------------------------*/
;/* code */
;/*------------------------------------------------------------------------------------------*/
GLOBAL Reset_Handler
area start,code,readwrite
entry
code32
num EQU 20 ;/* Set number of words to be copied */
Reset_Handler
ldr r0, =src ;/* r0 = pointer to source block */
ldr r1, =dst ;/* r1 = pointer to destination block */
mov r2, #num ;/* r2 = number of words to copy */
ldr sp, =0x30200000 ;/* set up stack pointer (r13) */
blockcopy
movs r3,r2, LSR #3 ;/* number of eight word multiples */
beq copywords ;/* less than eight words to move ? */
stmea sp!, {r4-r11} ;/* save some working registers */
octcopy
ldmia r0!, {r4-r11} ;/* load 8 words from the source */
stmia r1!, {r4-r11} ;/* and put them at the destination */
subs r3, r3, #1 ;/* decrement the counter */
bne octcopy ;/* ... copy more */
ldmea sp!, {r4-r11} ;/* don't need these now - restore originals */
copywords
ands r2, r2, #7 ;/* number of odd words to copy */
beq stop ;/* No words left to copy ? */
wordcopy
ldr r3, [r0], #4 ;/* a word from the source */
str r3, [r1], #4 ;/* store a word to the destination */
subs r2, r2, #1 ;/* decrement the counter */
bne wordcopy ;/* ... copy more */
stop
b stop
;/*------------------------------------------------------------------------------------------*/
;/* make a word pool */
;/*------------------------------------------------------------------------------------------*/
ltorg
src
dcd 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4
dst
dcd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
end

;#*********************************************************************************************
;# NAME: ARMcode.s *
;# Author: EWUHAN R & D Center, st *
;# Desc: ARMcode examples *
;# copy words from src to dst *
;# History: shw.He 2005.02.22 *
;#*********************************************************************************************
;/*------------------------------------------------------------------------------------------*/
;/* code */
;/*------------------------------------------------------------------------------------------*/
GLOBAL Reset_Handler
area start,code,readwrite
entry
code32
num EQU 20 ;/* Set number of words to be copied */
Reset_Handler
ldr r0, =src ;/* r0 = pointer to source block */
ldr r1, =dst ;/* r1 = pointer to destination block */
mov r2, #num ;/* r2 = number of words to copy */
ldr sp, =0x30200000 ;/* set up stack pointer (r13) */
blockcopy
movs r3,r2, LSR #3 ;/* number of eight word multiples */
beq copywords ;/* less than eight words to move ? */
stmed sp!, {r4-r11} ;/* save some working registers */
octcopy
ldmia r0!, {r4-r11} ;/* load 8 words from the source */
stmia r1!, {r4-r11} ;/* and put them at the destination */
subs r3, r3, #1 ;/* decrement the counter */
bne octcopy ;/* ... copy more */
ldmed sp!, {r4-r11} ;/* don't need these now - restore originals */
copywords
ands r2, r2, #7 ;/* number of odd words to copy */
beq stop ;/* No words left to copy ? */
wordcopy
ldr r3, [r0], #4 ;/* a word from the source */
str r3, [r1], #4 ;/* store a word to the destination */
subs r2, r2, #1 ;/* decrement the counter */
bne wordcopy ;/* ... copy more */
stop
b stop
;/*------------------------------------------------------------------------------------------*/
;/* make a word pool */
;/*------------------------------------------------------------------------------------------*/
ltorg
src
dcd 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4
dst
dcd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
end

;EduKit2410_for_MDK\3.4_armmode中的armmode.s代码
mrs r0,cpsr
bic r0,r0,#0x1f ;/* */
orr r0,r0,#0x11
msr cpsr_cxfs,r0
mov r8, #16
mov r9, #17
mov r10, #18
mov r11, #19
mov r12, #20
mov r13, #21
mov r14, #22
(1) 请解释上述模式切换指令;
很好,我选择求助 ChatGPT。
以下是ChatGPT(一种AI程序)给出的解释:

(2) 将运行结果及寄存器值截图;

(3) 将CPSR的格式写出来,并将程序运行的CPSR值截图。

我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我有一些非常大的模型,我必须将它们迁移到最新版本的Rails。这些模型有相当多的验证(User有大约50个验证)。是否可以将所有这些验证移动到另一个文件中?说app/models/validations/user_validations.rb。如果可以,有人可以提供示例吗? 最佳答案 您可以为此使用关注点:#app/models/validations/user_validations.rbrequire'active_support/concern'moduleUserValidationsextendActiveSupport:
对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs
我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案
ruby如何管理内存。例如:如果我们在执行过程中采用C程序,则以下是内存模型。类似于这个ruby如何处理内存。C:__________________|||stack|||------------------||||------------------|||||Heap|||||__________________|||data|__________________|text|__________________Ruby:? 最佳答案 Ruby中没有“内存”这样的东西。Class#allocate分配一个对象并返回该对象。这就是程序
几个月前,我读了一篇关于rubygem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题: