ruby-on-rails - Ruby 代码在路由工作时没有得到解释

标签 ruby-on-rails ruby ruby-on-rails-3

我遇到了这种非常奇怪的行为,我的应用程序中的基本路由有时工作正常,有时工作不正常,而且在我的 View 文件中,<%= ...... %> 中的 ruby​​ 代码是根本不渲染。这部分( <%= %> 里面的那个)只显示空白。我知道控件位于此处,因为如果我在这些定界符之外写入任何字符串,它就会显示在页面上。

在路由方面,我遇到的问题是我的创建 Controller 将要索引。我正在关注 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 时,它会显示“In posts index file”,这是为索引文件呈现的,但没有执行任何部分的 ruby​​ 代码

非常感谢任何帮助!

最佳答案

您在帖子索引中缺少 = sign in this line,这就是表单未呈现的原因。

这一行

<% form_tag(:controller => "posts", :action => "create") do %>

应该是

<%= form_tag(:controller => "posts", :action => "create") do %>

至少应该可以解决第二个问题。

关于ruby-on-rails - Ruby 代码在路由工作时没有得到解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11488882/

相关文章:

css - 无法使用 rails helpers image_tag 或 image_path 在 Google map 上显示本地镜像图标

ruby-on-rails - Rails 控制台 'y' 助手返回 NameError 而不是 yaml 格式输出

ruby-on-rails - git push heroku 挂起

ruby-on-rails - ExceptionNotifier.notify_exception 不起作用

ruby-on-rails - Phusion Passenger Error : You have activated rack 1. 2.1,但是你的 Gemfile 需要 rack 1.2.2

ruby-on-rails - 如何在 "assigns"之后访问 "render :template => ..."?

ruby-on-rails - 如何使用Resque和Redis对队列进行优先级排序

ruby-on-rails-3 - 参数哈希键作为符号与字符串

ruby-on-rails - 生成电子邮件地址图像或其他不易被抓取的内容

ruby-on-rails - 无法使用 mongoid 和 MongoDB 将新记录保存到我的 Rails 模型中