|
该帖已经被评为良好帖
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2007-06-11
从你这里学到不少东西,感谢
|
|
| 返回顶楼 | |
|
最后更新时间:2007-06-11
yananay 写道 今天来看看如何回复一个 topic。
首先看看 views/topics/show.rhtml, 注意到这段代码: <p><%= link_to_function 'Reply to topic'[], "ReplyForm.init()", :class => "utility" %></p> 这个 javascript 用法我们已经在以前学过了,通过查看 application.js, 我们知道这个js 要控制的是 “reply” 这个 div. 这个 div 是什么内容呢?我们来看一看:
<div id="reply" class="editbox">
<div class="container">
<%= content_tag 'p', h(flash[:bad_reply]), :class => 'notice' if flash[:bad_reply] %>
<% form_for :post, :url => posts_path(:forum_id => @forum, :topic_id => @topic, :page => @topic.last_page) do |f| -%>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td rowspan="2" width="70%">
<%= f.text_area :body, :rows => 8 %>
</td>
<td valign="top">
<h5><%= 'Formatting Help'[] %></h5>
<ul class="help">
<li><%= '*bold*'[:formatting_bold] %>
<%= '_italics_'[:formatting_italics] %>
<br />
<%= 'bq. <span>(quotes)</span>'[:formatting_blockquote] %></li>
<li>"IBM":http://www.ibm.com</li>
<li><%= '* or # <span>(lists)</span>'[:formatting_list] %></li>
</ul>
</td>
</tr>
<tr>
<td valign="bottom" style="padding-bottom:15px;">
<%= submit_tag "Save Reply"[] %><span class="button_or">or <%= link_to_function 'cancel'[], "$('reply').hide()" %></span>
</td>
</tr>
</table>
<% end -%>
</div>
关键的内容就是一个form, 它的形式就是REST的标准提交form的形式,不多说了,直接看 PostsController 的 Create 方法。 这个方法恐怕是目前我们遇见过的最长的一个方法,我们一步一步来。 首先,找到要回复的topic: @topic = Topic.find_by_id_and_forum_id(params[:topic_id],params[:forum_id], :include => :forum) 引用 什么?你问我 :include 是什么意思?快去看 agile dev 1 st edition, 有答案! 接着,有一个判断: if @topic.locked? …. 省略 end 意思很明显了,如果被locked 了,那么是不能回复的。 接下来的代码: @forum = @topic.forum @post = @topic.posts.build(params[:post]) @post.user = current_user @post.save! respond_to do |format| 。。。。 省略 end rescue ActiveRecord::RecordInvalid 。。。 省略 end 这部分代码也不难理解,就是创建相应的 post, 并保存。 不过这里用了 resure, 如果出现异常的话,该如何如何地处理。 好了,明天继续! |
|
| 返回顶楼 | |
|
最后更新时间:2007-06-11
最近研究了一下Restful,遇到了很多困难,但大部分都解决了。感觉Restful是挺好的,基本可以符合大部分的网站功能开发,但对于路由的设置一直找不到很好的方法来满足我的想法。
|
|
| 返回顶楼 | |
|
最后更新时间:2007-08-06
刚下完,楼主,beast中的:login_required方法在哪实现的?找了半天 不知道在哪 |
|
| 返回顶楼 | |
|
最后更新时间:2007-09-08
请教各位 我依照楼主的那个文档操作 当操作到rake db:schema:load这一步的时候出现这样的错误:"undefined method 'view_pathd='foe LoggedExceptionController:Class
<See full trace by running task with --trace>" |
|
| 返回顶楼 | |
|
最后更新时间:2007-09-26
引用 这里出来了一些新玩意:link_to_remote. 这个就是现在特别流行的 AJAX.不太熟悉Ajax?没关系,去google 一下,先了解一下基本内容。不过如果你看了 agile development with rails 1st edition,对这个方法就应该有所了解。 这里简单说说。 :url 意思说调用的 url 地址。 :before 意思是在调用完成之前进行的处理。 :conditions 意思是判断这个调用是不是可以执行。 关键是这个 :href. 如果你安装了 beast, 会发现把这个 :href 去掉,并不影响功能。那么有什么区别呢? 如果有 :href ,那么生成的链接是: href="/forums/1/topics/2/posts/13;edit" onclick=…省略 如果没有 :href, 那么生成的链接是: href="#" onclick=…省略 可见,加了 href, 是为了 REST的风格。 所以,如果我们要用REST的方式作一个站点,而且用到了link_to_remote,应该参考一下beast的方式。 和REST有什么关系啊,href就是为了向后兼容,防止浏览器禁用js的一个方法。 |
|
| 返回顶楼 | |
|
最后更新时间:2007-10-23
gaofan 写道: 刚下完,楼主,beast中的:login_required方法在哪实现的?找了半天 不知道在哪
lib->authentication_system.rb |
|
| 返回顶楼 | |





