Ruby on rails Can';t调用控制器中的模型方法

Ruby on rails Can';t调用控制器中的模型方法,ruby-on-rails,Ruby On Rails,我是RubyonRails的新手,希望你能帮助我。我正在尝试使用RubyonRails为Redmine编写一个插件。我在控制器中从模型调用新方法时遇到了一些问题。因此,我制作了Redmine插件教程,并在之后制作了以下项目: 型号: class Poll < ActiveRecord::Base def vote(answer) increment(answer == 'yes' ? :yes : :no) end end class Poll < Act

我是RubyonRails的新手,希望你能帮助我。我正在尝试使用RubyonRails为Redmine编写一个插件。我在控制器中从模型调用新方法时遇到了一些问题。因此,我制作了Redmine插件教程,并在之后制作了以下项目:

型号:

class Poll < ActiveRecord::Base
  def vote(answer)
     increment(answer == 'yes' ? :yes : :no)
  end
end
    class Poll < ActiveRecord::Base
  def vote(answer)
     increment(answer == 'yes' ? :yes : :no)
  end
  def self.load_content
      @wiki_content = Poll.find_by_sql ("select wc.text
                                 , wc.comments
                                 , wc.version
                                from wiki_contents wc 
                                where wc.page_id = (select min(id) 
                                      from wiki_pages
                                      where wiki_id = 3")
  end
end
致意
Arty

您需要在模型方法中返回一些内容,而不是为
@wiki\u content
赋值,因为模型对该变量没有任何可见性

看起来是这样的

def self.load_content
      return Poll.find_by_sql ("select wc.text
                                 , wc.comments
                                 , wc.version
                                from wiki_contents wc 
                                where wc.page_id = (select min(id) 
                                      from wiki_pages
                                      where wiki_id = 3")
  end

index.html
中,您使用的是
,但在代码片段中,您没有为
@content
指定任何值。由于您正在控制器
索引
操作中分配
@wiki\u内容
,因此我假设您希望在此处使用
@wiki\u内容

# app/views/polls/index.html
...
<% @wiki_content.each do |cn| %>
    <p>
        <%= cn.text %>
    </p>
<% end %>

您可以发布您收到的错误吗?“内部错误您试图访问的页面上发生错误。如果您继续遇到问题,请与您的Redmine管理员联系以获得帮助。如果您是Redmine管理员,请查看日志文件以了解有关错误的详细信息。”我指的是您的错误日志,不是浏览器/web服务器显示的通用5xx。关于新方法的错误(我猜你试图在nil上调用某些东西)@Arty,你能从Rails服务器发布日志文件吗?这可能有助于缩小真正的问题所在。这样做了,还从Workbench中尝试了相同的SQL语句,它工作了。是的,你是对的。我自己也看到了,已经修复了,但是错误仍然出现。@Arty,当您看到此错误时,请发布日志文件的内容,以便更快地发现问题。请用日志更新您的问题。完成后,还尝试了Workbench中的同一SQL语句,结果正常。@Arty,请查看我的更新。SQL代码中缺少右括号。@amine:最后执行的语句的值将在ruby中自动返回。你不必显式地返回它,没错。最重要的部分是删除
@wiki\u content=
class PollsController < ApplicationController
  def index
     @project = Project.find(params[:project_id])
     @polls = Poll.all
     @wiki_content = Poll.load_content
  end

  def vote
    poll = Poll.find(params[:id])
    poll.vote(params[:answer])
    if poll.save
      flash[:notice] = 'Vote saved.'
    end
    redirect_to :action => 'index'
  end
end
<h2>Polls</h2>

<% @polls.each do |poll| %>
  <p>
  <%= poll.question %>?
  <%= link_to 'Yes', { :action => 'vote', :id => poll[:id], :answer => 'yes' }, :method => :post %> (<%= poll.yes %>) /
  <%= link_to 'No', { :action => 'vote', :id => poll[:id], :answer => 'no' }, :method => :post %> (<%= poll.no %>)
  </p>
<% end %>

<% @content.each do |cn| %>
    <p>
        <%= cn.text %>
    </p>
<% end %>
ActiveRecord::StatementInvalid (Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 7: select wc.text
                                 , wc.comments
                                 , wc.version
                                from wiki_contents wc 
                                where wc.page_id = (select min(id) 
                                      from wiki_pages
                                      where wiki_id = 3):
def self.load_content
      return Poll.find_by_sql ("select wc.text
                                 , wc.comments
                                 , wc.version
                                from wiki_contents wc 
                                where wc.page_id = (select min(id) 
                                      from wiki_pages
                                      where wiki_id = 3")
  end
# app/views/polls/index.html
...
<% @wiki_content.each do |cn| %>
    <p>
        <%= cn.text %>
    </p>
<% end %>
# app/model/poll.rb
def self.load_content
      @wiki_content = Poll.find_by_sql ("select wc.text
                                 , wc.comments
                                 , wc.version
                                from wiki_contents wc 
                                where wc.page_id = (select min(id) 
                                      from wiki_pages
                                      where wiki_id = 3)")
end