10

Accessibility / Scope Method - NoMethodError

 3 years ago
source link: https://www.codesd.com/item/accessibility-scope-method-nomethoderror.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Accessibility / Scope Method - NoMethodError

advertisements

I'm working on my first rails project and I have a problem that I just cannot figure out.

I generated a Scaffold for an object named Archive

to this object I added the method processfile

when I try to link_to said method from Archives#Index I'm getting this:

undefined method `processfile' for #<Archive:0x702de78>

This is the model archive.rb

class Archive < ActiveRecord::Base
 belongs_to :users
 attr_accessible :file, :user_id
 mount_uploader :file, FileUploader
end

This is the code on the index.html.erb (belonging to archives)

<% @archives.each do |archive| %>
<tr>
<td><%= archive.file%></td>
<td><%= User.find(archive.user_id).name %></td>
<td>
    <%= link_to 'Download', archive.file_url %>
    ::
    <%= link_to 'Show', archive %>
    ::
    <%= link_to 'Edit', edit_archive_path(archive) %>
    ::
    <%= link_to 'Delete', archive, confirm: 'Esta Seguro?', method: :delete %>
    ::
    <%= link_to "Process", archive.processfile  %>

</td>
</tr>
<% end %>

this is the routes.rb line:

match "archives/processfile/:id" => "archives#processfile", :as => :processfile

the processfile method defined whitin archives_controller.rb doesn't have anything on it, i just wanted to test the functionality since I'm having a hard time getting the grip of the "rails way"

archives_controler.rb

def processfile
# @archive = Archive.find(params[:id])
#do something with the archive
end

All in all, what I ultimately want to achieve is to call the processfile method on a given archive(taken from the index table) to do something with it. On the example, I watered down the method call (not passing an archive or archive.file to it) to make it run, to no avail.

I've searched a lot (on google and in here) and haven't found a clear guide that would address my problem, probably because i'm new and can't fully grasp the concepts behind rails MVC.

I've read something about methods only being accessed by same controlers but I've seen sample code when people call methods on controllers from index views without declaring them as helpers. o.0

I know it's probably a silly confusion, but I can't figure it out :(


The way you've structured your route (i.e., match "archives/processfile/:id" => "archives#processfile") means that it's expecting an archive id to be passed. You need to adjust your link_to to pass one:

# app/archives/index.html.erb
<%= link_to "Process", processfile_path(archive.id)  %>

The error you're receiving is because you're trying to call an instance method called processfile on archive, but there's presumably no method by that name. The second parameter of the link_to helper is a path, not an instance method.

EDIT:

If you're looking to make your routes more RESTful (which you should do if you've created an Archive resource), you can generate all your CRUD routes by declaring resource :archives in your routes. Then, within a block, you can declare a block of member routes, all of which will route to the specified action in your archive_controller.rb and enable you to pass an archive id to the action.

# config/routes.rb
resources :archives do
    member do
        get 'processfile'
    end
end




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK