2015-09-17 14 views
-2

我试图让Rails中一个字计数器,文字是一种脚手架的名字,但我 @counter变量似乎并没有被赋予任何输出如何解决这个@counter没有给出输出 - Rails的

class TextsController < ApplicationController 
before_action :set_text, only: [:show, :edit, :update, :destroy] 


def index 
    @texts = Text.all 
end 


def new 
@text = Text.new 
end 

def edit 
end 

def create 

@text = Text.new(text_params) 
@counter = @text.name.split.size 

end 




private 
def set_text 
    @text = Text.find(params[:id]) 
end 


def text_params 
    params.require(:text).permit(:name, :string) 
end 

VIEW

<p id="notice"><%= notice %></p> 

<%= @text.name %> 

<%= @counter %> 

+1

什么在'@ text.name'? –

+0

应该算作课本的文本 –

+0

我的意思是,当你在柜台里什么都没有时。它是什么? –

回答

0

基于您的代码create方法不能显示变量。您必须移动show方法。您可以创建新的show方法包含。

def show 
@counter = @text.name.split.size 
end 

改变你的方法create到:

def create 
    @text = Text.new(text_params) 
    if @text.save 
    redirect_to texts_path 
    else 
    render :new 
    end 
end 

,你可以去展示页面。它会显示@counter

+0

'@ text.name'将被设置为传递的参数,这意味着'@counter = @ text.name.split.size'可以工作。我猜他没有使用'create.html.erb'文件 –

+0

是的,你是对的,它不会创建'create.html.erb'。你想要显示哪个页面的参数? – akbarbin

+0

这不是我的代码 –