2014-02-19 36 views
0

您好我是Ruby on Rails的新手。我的代碼運行良好。我的問題是有一些文字顯示在網頁上,我不知道從哪裏來。 這裏是它的截圖: http://awesomescreenshot.com/0f32dhw7f4使用Ruby on Rails在瀏覽器中打印文本

這裏是我的外接控制器

class AddsController < ApplicationController 

    def new 
    @add = Add.new 
    end 

    def create 
    @add = Add.new(params.require(:add).permit(:first_name, :last_name, :email)) 
    if @add.save 
     redirect_to(:controller=>'home') 
    else 
     render 'new' 
    end 
    end 

    def edit 
    @add = Add.find(params[:id]) 
    end 

    def update 
    @add = Add.find(params[:id]) 
    if @add.update(params.require(:add).permit(:first_name, :last_name, :email)) 
     redirect_to(:controller=>'home') 
    else 
     render 'edit' 
    end 
    end 

    def destroy 
    @add = Add.find(params[:id]) 
    @add.destroy 
    redirect_to(:controller=>'home') 
    end 

end 

這是我的應用程序/視圖/ home文件

<ul id="nav"> 
    <li><%= link_to "Home", controller: "home" %></li> 
    <li><%= link_to "Products", controller: "products" %></li> 
    </ul> 
    <div class="clear"></div> 
    <h1>Lists</h1> 
    <%= link_to "Add", new_add_path, :class=> 'btn btn-success' %> 
    <br /> 
    <br /> 

    <table class="table"> 
    <tr> 
     <th>First Name </th> 
     <th>Last Name </th> 
     <th>Email </th> 
     <th>Options </th> 
    </tr> 
    <%= @adds.each do |adds| %> 
     <tr> 
     <td><%= adds.first_name%></td> 
     <td><%= adds.last_name %></td> 
     <td><%= adds.email %></td> 
     <td><%= link_to "Edit", edit_add_path(adds) %> | 
      <%= link_to "Delete", add_path(adds), 
           method: :delete, data:{confirm: 'Are you sure you want to delete this?'} %> 
     </td> 
     </tr> 
    <% end %> 
    </table> 

我家的控制器

class HomeController < ApplicationController 
    def index 
    @adds = Add.all 
    end 
end 

有人能想出我嗎?

+0

檢查你的應用程序佈局的更多信息, 'app/views/layouts/application.html.erb' – xlembouras

+0

我已經檢查過!那裏增加了什麼? – bobcatnyxz

回答

1

在您的視圖,而迭代陣列,你已經使用<%= @adds%>改變的代碼的迭代部分如下

<% @adds.each do |adds| %> 
     <tr> 
     <td><%= adds.first_name%></td> 
     <td><%= adds.last_name %></td> 
     <td><%= adds.email %></td> 
     <td><%= link_to "Edit", edit_add_path(adds) %> | 
      <%= link_to "Delete", add_path(adds), 
           method: :delete, data:{confirm: 'Are you sure you want to delete this?'} %> 
     </td> 
     </tr> 
<% end %> 
+0

啊好吧謝謝!有效! – bobcatnyxz

0

更改此

<%= @adds.each do |adds| %> 

<% @adds.each do |adds| %> 
0

因爲您使用的是<%= %>而不是<% %>在迭代部分。

變化<%= @adds.each do |adds| %><% @adds.each do |adds| %>

有關Rails的代碼中看到This

,如果你想知道<%之間的差額=和<%的人認爲這SO

+0

非常感謝男士!爲信息:) – bobcatnyxz

相關問題