Ruby on rails RubyonRails-未定义的方法名

Ruby on rails RubyonRails-未定义的方法名,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我正在尝试将表单中的信息发送到FRESHDESK API,但很难做到这一点 按我的路线 resource :client, only: [:create, :show] 我设置了一个资源 然后我创建了一个控制器 客户端\u controller.rb class ClientsController < ApplicationController def show end def create client = Freshdesk.new("http://onehous

我正在尝试将表单中的信息发送到FRESHDESK API,但很难做到这一点

按我的路线

 resource :client, only: [:create, :show]
我设置了一个资源

然后我创建了一个控制器

客户端\u controller.rb

 class ClientsController < ApplicationController 
 def show
 end

 def create
   client = Freshdesk.new("http://onehouse.freshdesk.com/", "xs02V99BeqPfDiAlwcL7", "X")
   client.post_tickets(params[:client])
 end
end
所以我猜在我的表演中我遗漏了一些东西,但不确定我该放什么

只是附带说明一下……我首先把所有东西都放在一个ruby文件中,然后在我的终端中执行

 $ ruby freshdesk.rb
它运行良好,并将数据发布到我的FRESHDESK仪表板


因此,现在我只是想让它在我的RAILS应用程序中工作,你应该在你的操作中初始化@client,以便在你的视图中使用它

class ClientsController < ApplicationController 
 def show
   @client = Freshdesk.new("http://onehouse.freshdesk.com/", "xs02V99BeqPfDiAlwcL7", "X")
 end

 def create
   client = Freshdesk.new("http://onehouse.freshdesk.com/", "xs02V99BeqPfDiAlwcL7", "X")
   client.post_tickets(params[:client])
 end
end
class客户端控制器

这应该可以解决你的问题

=谢谢你的回复我尝试过了,我得到了一个未初始化的常量ClientsController::Freshdesk知道如何解决吗?rails不会自动从lib加载类。将此添加到config/application.rb->
config.autoload_路径+=%W(#{config.root}/lib)config.autoload_路径+=Dir[“#{config.root}/lib/**/”]
看看这个链接,它似乎克服了这个障碍,但是@client=Freshdesk.new在我的show action中,我在ClientsController中得到了这个错误#show@RickyAhn查看我对Vimsha答案的编辑。您还需要在
show
操作中将参数传递给
new
。如果这解决了您的问题,您可能希望将其提取到helper方法中,这样您就不会重复代码:)
   NoMethodError in Clients#show
   undefined method `model_name' for NilClass:Class
 $ ruby freshdesk.rb
class ClientsController < ApplicationController 
 def show
   @client = Freshdesk.new("http://onehouse.freshdesk.com/", "xs02V99BeqPfDiAlwcL7", "X")
 end

 def create
   client = Freshdesk.new("http://onehouse.freshdesk.com/", "xs02V99BeqPfDiAlwcL7", "X")
   client.post_tickets(params[:client])
 end
end