我有这个型号
class Book < ActiveRecord::Base
has_many :accountings
has_many :commodities, through: :accountings
end
class Accounting < ActiveRecord::Base
belongs_to :book
belongs_to :commodity
end
class Commodity < ActiveRecord::Base
has_many :accountings
end
Run Code Online (Sandbox Code Playgroud)
我的书形式是:
<%= form_for(book) do |f| %>
<%= render 'shared/error_messages', object: book %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :commodity_ids %>
<%= f.collection_select :commodity_ids, Commodity.all, :id, :name, {}, {multiple: true} %>
<br />
<%= f.submit class: 'btn btn-primary' %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
在BooksController:
def new
@book = Book.new
end …
Run Code Online (Sandbox Code Playgroud) 我发现Vim快捷方式nmap <enter> o<esc>
或者nmap <enter> O<esc>
用enter键插入一个空行非常有用.但是,它们会对插件造成严重破坏; 例如,ag.vim
它使用要跳转到的文件名填充quickfix列表.在此窗口中按Enter键(应该跳转到文件)会给出错误E21: Cannot make changes; modifiable is off
.
为了避免在quickfix缓冲区中应用映射,我可以这样做:
" insert blank lines with <enter>
function! NewlineWithEnter()
if &buftype ==# 'quickfix'
execute "normal! \<CR>"
else
execute "normal! O\<esc>"
endif
endfunction
nnoremap <CR> :call NewlineWithEnter()<CR>
Run Code Online (Sandbox Code Playgroud)
这是有效的,但我真正想要的是避免任何不可修改的缓冲区中的映射,而不仅仅是在quickfix窗口中.例如,映射在位置列表中也没有意义(并且可能会破坏使用它的其他一些插件).如何检查我是否在可修改的缓冲区中?