English 中文(简体)
铁路友谊的未开端不变
原标题:uninitialized constant with rails friendships

我是Rails上的新手,遇到了以下错误:

NameError in FriendshipsController#create  
uninitialized constant FriendshipsController

这还表明:

{"authenticity_token"=>"eQvv3flATE+P1TEErOWP/6fM8dEOIBxltobCxtM/F18=",
 "friend_id"=>"32"}

当我在我的用户展示页面中点击“添加朋友”链接时。我完全按照自我参考关联的railscast,但我一直收到这个错误,而且我找不到任何信息,甚至不知道“未初始化的常量”的意思。从互联网上收集的信息表明它可能与acts_as_authenticated插件有关,但我按照我找到的一个修复方法,但它没有起作用。

这是我 user/show.html.erb 页面的代码:

<%= link_to "Add Friend", friendships_path(:friend_id => @user.id), :method => :post %> 

以及我的友谊控制器代码:

def create
  @friendship = current_user.friendships.build(:friend_id => params[:friend_id])
  if @friendship.save
    flash[:notice] = "Added friend."
    redirect_to root_url
  else
    flash[:error] = "Unable to add friend."
    redirect_to root_url
  end
end

我在这里做错了什么?我一点头绪都没有,不知道是什么原因导致的。如果我漏掉了必要的代码,请告诉我。

最佳回答

很难说。您应该发布课程的顶部部分...要求,课程定义,包括以及您的方法之外的任何其他内容,以及create方法。

问题回答

Rails提示你在初始化之前使用了一个常量。

puts SomeConstant
# before
SomeConstant = 10

在这种情况下,常数是控制器类名 - FriendshipsController。

检查类名是否正确,即您的appcontroller目录中是否有一个具有该名称的控制器。

I think you run rails g controller Friendship

while you should have used rails g controller Friendships

thats why all files are now singular You can still go through and change all files though





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签