现在我有一个子来验证一堆文本框和组合框.
我之前使用过很多IF语句来验证和弹出不同的消息框和每个IF语句中的Exit Sub.
但我听说太多的退出会降低效率,不建议使用它们.相反,嵌套的IF更好,因为它会让进程自然地结束.
然后我发现如果我使用嵌套的IF,它将难以阅读,因为消息框都与条件分开.
我没有软件开发经验.所以我的问题是:在真实的项目中,你会选择哪种风格?
多谢你们.我已经解决了这个问题.这是因为当我进入posts/new页面时.新操作会创建一个@post具有nil属性的虚拟对象.由于@post存在,会出现侧栏中的编辑和删除链接.但是,edit_post_path因为@post.id是不起作用nil.然后发生错误.所以我改变<% if @post %>了<% if @post && !@post.id.nil? %>它,它的工作原理. - ps rails错误消息非常混乱.
我是rails的新手,只是构建了一个简单的应用程序,当我点击链接创建新帖子时看到了错误:
No route matches {:action=>"edit", :controller=>"posts"}
该rake routes结果:
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
Run Code Online (Sandbox Code Playgroud)
routes.rb文件包含resources :posts在其中.
链接是: <li><%= link_to "New Post", new_post_path %></li>
新的编辑方法PostsController:
def new
@post …Run Code Online (Sandbox Code Playgroud) 我创建了一个博客。每当我添加帖子时,帖子索引页面 ( ) 的底部都会显示数据库中的记录列表home.html.erb,如下所示:
[#<Post id: 1, title: "hahaha", content: "Because the gravatar_for method is undefined, the u...", public: true, created_at: "2013-03-18 04:00:17", updated_at: "2013-03-18 04:01:09">]
Run Code Online (Sandbox Code Playgroud)
我尝试过删除<%= will_paginate @posts %>,但没有成功。
这是我的home.html.erb:
<%= @posts.each do |post| %>
<article class="posts">
<h2><%= link_to post.title, post_path(post) %></h1>
<h3><%= post.public %></h3>
<p><%= truncate markdown(post.content), length: 400, omission: " ......" %></p>
<span class="continue"><%= link_to "... Continue Reading ...", post_path(post) %></span>
</article>
<% end %>
<%= will_paginate @posts %>
Run Code Online (Sandbox Code Playgroud)
这是我的 Gemfile,以防万一您需要它: …