简体   繁体   中英

Can't call a model method in the controller

im pretty new to ruby-on-rails and hope you can help me. I am trying to write a plugin for Redmine using ruby-on-rails. And I'm having some problems calling a new method from my model in my controller. So I made the Redmine plugin tutorial and had these items afterwards:

Model:

class Poll < ActiveRecord::Base
  def vote(answer)
     increment(answer == 'yes' ? :yes : :no)
  end
end

Controller:

class PollsController < ApplicationController
  def index
     @project = Project.find(params[:project_id])
     @polls = Poll.all
  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

index.html:

<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 %>

Afterwards I wanted to add Wikicontent to the same html just to get used to work with RoR. So my present items look like these:

Model:

    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

Controller:

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

index.html:

<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 %>

Im getting an "internal error" according to my Browser. I tried to find the error and it seems I can't call the new method from my model inside the controller and i can't figure out why. Like I said I'm really new to RoR so I hope you can help me.

This is the error description in the logfile:

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):

Best Regards Arty

you need to return something in your model method instead of assigning a value to @wiki_content as the model does not have any visibility on that variable

it would look like that

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

In your index.html you are using <% @content.each do |cn| %> <% @content.each do |cn| %> , but nowhere in your code snippets you are assigning any values to @content . Since you are assigning @wiki_content in your controller index action, I'm assuming you want to use @wiki_content here:

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

Update:

By the error you've posted and a double look at your query you have a syntax error in your query. You are missing closing parenthesis ) in your inner select. Update your query as follows:

# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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