简体   繁体   English

Ruby on Rails控制器语法

[英]Ruby on Rails controller syntax

I am new to Rails, and just working my way through my first solo project, but I seem to be running into a syntax error with a constant not being initialized (Ive gotten several of these, but each seems to have a different cause.....not quite sure how i keep getting the same error with different causes :)): 我是Rails的新手,只是在第一个个人项目中工作,但是我似乎遇到了语法错误,并且没有初始化常量(我已经得到了其中的几个,但是每个似乎都有不同的原因。 ...不太确定我如何因不同原因而不断收到相同的错误:)):

uninitialized constant DatastringsController::Datastrings

DatastringsController: DatastringsController:

class DatastringsController < ApplicationController

  def new

  end

  def create
    @datastrings = Datastrings.new(datastrings_params) #ERROR returned on this line
    @datastrings.save
    redirect_to @datastrings
  end

  def show
    @datastrings = Datastrings.find(params[:id])
  end

  private

  def datastrings_params
    params.require(:datastrings).permit(:title, :text)
  end

end

I believe my form is correct: 我相信我的表格是正确的:

<%= form_for :datastrings, url: datastrings_path do |f| %>
  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </p>

  <p>
    <%= f.label :text %><br>
    <%= f.text_area :text %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>

The main problem you have is here: 您遇到的主要问题是:

<%= form_for :datastrings, url: datastrings_path do |f| %>

form_for really should be populated with an ActiveRecord object, as this allows Rails to build the relative paths it requires correctly. 实际上, form_for应该填充一个ActiveRecord对象,因为它允许Rails正确构建其所需的相对路径。

Although I don't know why this is the case, your current setup is basically trying to render DatastringsController::Datastrings -- primarily because you've not set up your form_for correctly 尽管我不知道为什么会这样,但是您当前的设置基本上是在尝试呈现DatastringsController::Datastrings ,主要是因为您没有正确设置form_for

-- -

Fix 固定

If you want to create a datastring object, I'd follow convention and do this: 如果要创建数据datastring对象,请遵循约定并执行以下操作:

#config/routes.rb
resources :datastrings

#app/controllers/datastrings_controller.rb
Class DatastringsController < ApplicationController
   def new
      @datastring = Datastring.new
   end

   def create
      @datastring = Datastring.new(datastring_params)
      @datastring.save
   end

   private

   def datastring_params
      params.require(:datastring).permit(:title, :text)
   end
end

#app/views/datastrings/new.html.erb
<%= form_for @datastring do |f| %>
   <%= f.text_field :name %>
   <%= f.submit %>
<% end %>

-- -

YOU ALSO NEED TO NAME YOUR MODELS IN SINGULAR 您还需要用单数命名您的模型

Looking at it now, it seems that your main issue is likely that you've named your model as a plural . 现在来看,看来您的主要问题可能是您将模型命名为plural

The reason this will be bad is that when you load Rails, it will load all your models, and consequently, allow you to call them by referencing their class name. 之所以不好,是因为在加载Rails时,它将加载所有模型,因此,允许您通过引用它们的类名来调用它们。 If a model is plural, I don't think it will load it correctly, causing the error you've highlighted 如果模型是复数形式,我认为它不会正确加载,从而导致您突出显示错误

If you name your model to the following, it should be better: 如果将模型命名为以下名称,则效果会更好:

#app/models/datastring.rb
Class Datastring < ActiveRecord::Base
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM