我得到了这个非常奇怪的行为,我的应用程序中的基本路由有时候工作正常,有时候没有,在某种程度上在我的视图文件中,<%= ......%&gt里面的ruby代码;根本不渲染。这部分(里面的那个<%=%>)只显示空白。我知道控件来到这里是因为如果我在这些分隔符之外写了任何字符串,那么它就显示在页面上。
在路由方面,我遇到的问题是我的控制器for create将要编制索引。我正在关注http://www.noupe.com/ajax/create-a-simple-twitter-app.html
中的示例教程我正在使用ruby版本1.9.3p194和rails版本3.2.6
我的源文件如下。
routed.rb
resources :posts
match '/create', :to => 'posts#create'
match ':controller(/:action(/:id))(.:format)'
index.html.erb
In posts index file
<% form_tag(:controller => "posts", :action => "create") do %>
<%= label_tag(:message, "What are you doing?") %><br />
<%= text_area_tag(:message, nil, :size => "44x6") %><br />
<%= submit_tag("Update") %>
<% end %>
create.html.erb
<%= debug(params) %>
<%= render :partial => "message_form" %>
<%= render :partial => @posts %>
show.html.erb
buddy, here I am in show now .
posts_controller.rb
class PostsController < ApplicationController
def index
puts "I am in Posts#index"
@posts = Post.all(:order=>"created_at DESC")
respond_to do |format|
format.html
end
end
def show
puts "I am in Posts#show"
end
def edit
puts "I am in Posts#edit"
end
def new
puts "I am in Posts#new"
end
def create
puts "I am in Posts#create"
@post = Post.create(:message=>params[:message])
respond_to do |format|
if @post.save
format.html {redirect_to posts_path}
else
flash[:notice] = "Message failed to save"
format.html {redirect_to posts_path}
end
end
end
end
当我输入http://localhost:3000/posts/create
时,它会显示“伙计,我现在在这里展示。”这是show.html.erb的呈现,并且不执行任何ruby代码
当我输入http://localhost:3000/posts
时,它会显示“在帖子索引文件中”,这是为索引文件呈现但不执行ruby代码的一部分
非常感谢任何帮助!
答案 0 :(得分:3)
您在帖子索引中缺少此行中的=
符号,这就是表单未呈现的原因。
这一行
<% form_tag(:controller => "posts", :action => "create") do %>
应该是
<%= form_tag(:controller => "posts", :action => "create") do %>
至少应该解决第二个问题。