Routing Error with Ruby on Rails - ruby-on-rails

I'm fairly new to Ruby on Rails and I'm having what seems like an easy problem but I can't seem to figure out what I have done wrong. My homepage has a button and when you click the button, its supposed to create an xml file with information from the database.
Button code:
<%= button_to "Create Google File", :action => :create_google_file %>
Controller code:
class ProductsController < ApplicationController
def new
end
def create_google_file
#products = Product.find(:all)
file = File.new('dir.xml','w')
doc = #products.to_xml
file.puts doc
file.close
end
end
The error I'm getting is
No route matches {:action=>"create_google_file", :controller=>"products"}

Add this to your config/routes.rb file
match "/create_google_file" => "products#create_google_file", :as => :create_google_file
And change the button to this
<%= button_to "Create Google File", create_google_file_path %>

Related

Custom controller route to update column active to false

I am trying to link to a custom controller route action and I'm doing something wrong. I have a Document model that handles uploading documents to my CRUD app. I want users to be able to 'delete' something, but not actually delete it from the system, but rather update the column 'active' to false. Then if someone with admin privileges can go ahead an complete the deletion. This process is needed because the app gets audited and we do not want to accidentally delete uploaded files.
I can't get the custom update action (remove) to work. When I rake routes I see:
remove_documents PUT /documents/remove(.:format) document#remove
In my routes file (I'll a couple similar routes I'll want to add later so I used collection it up this way):
resources :documents do
collection do
put "remove", to: "document#remove", as: :remove
end
end
In the Documents index view:
<%= link_to remove_documents_url(document), :method => :put do %>
<span class="fa fa-trash text-danger"></span>
<% end %>
My Controller:
def remove
#document = Document.find(params[:id])
#document.active = false
#document.save
html { redirect_to(:back, :notice => 'Document was successfully removed.')}
end
The link works, but then I get the following error:
NameError at /documents/remove.75 uninitialized constant DocumentController
raise unless e.missing_name? qualified_name_for(parent, const_name)
end
end
name_error = NameError.new("uninitialized constant #{qualified_name}", const_name)
name_error.set_backtrace(caller.reject {|l| l.starts_with? __FILE__ })
raise name_error
end
# Remove the constants that have been autoloaded, and those that have been
# marked for unloading. Before each constant is removed a callback is sent
If you want the remove action on a specific Document, change the routes to :
resources :documents do
member do
put "remove", to: "documents#remove", as: :remove
end
end
which gives you : remove_document PUT /documents/:id/remove(.:format)
and use it like :
<%= link_to remove_document_path(document), :method => :put do %>
<span class="fa fa-trash text-danger"></span>
<% end %>

Ruby on Rails -- independant controller method for static pages

So basically I want to use a simple controller method with no params:
def create_message
#a = Message.create(:body => "Hello")
#a.save
redirect_to messages_path
end
but i'm routing from the home page, pages controller:
def home
end
I'm having a problem figuring out what to write in the routes file, I've tried almost everything including but not limited to:
resources :pages do
collection do
get :create_message
end
end
Views:
<%= link_to "Create Message", create_message_pages_path, class:"btn btn-primary"%>
Error:
The action 'create_message' could not be found for PagesController
You can create custom route for create_message like this
#routes.rb
get 'create_message' => 'pages#create_message', as: 'create_message'
Then link_to would be:
#view
<%= link_to "Create Message", create_message_path, class:"btn btn-primary"%>
I faced this problem once, in my case it is defining action under private block of controller gave the "The Action could not be found error". From your question I see there is no problem in defining routes and using it in view file. Please check to see if the create_message action is defined under private block in pages_controller.rb file. Hope it helps.

Ruby on Rails send_file doesn't work until i refresh the page?

I am working on a Rails server which I can download my locally stored movies and anime etc from. This is kind of working but when I click the download link I have to refresh the page in order for the download to actually start.
This is the controller that handles the download:
class DownloadController < ApplicationController
def index
#title = params[:title]
#name = params[:name]
#path = '/media/sf_Anime,_VN,_LN/Watching, not watched yet/'+#title+'/'+#name
send_file( #path )
end
end
and this is the link that links to that controller:
<% #episodes.each do |x| %>
<p> <%= x %><%= link_to " Download",
{controller: 'download', action: 'index', title: #title, name: x } %> </p>
<% end %>
edit:
I did some testing today and noticed that the download links work instantly if i try to send a smaller file (text or image). I also noticed that the download links actually works for the movies aswell but it takes 20-30 seconds for the download to start.
Do you have any idea of what would cause this delay?
Are you using turbolinks? Turbolinks apparently doesn't play nicely with send_file (https://github.com/rails/turbolinks/issues/182). Try adding "data: { no-turbolink: true }" (or "'data-no-turbolink'=>true") in the link_to helper, e.g.:
<%= link_to "Downloadable File", downloadable_file, data: { no-turbolink: true } %>
See also: Rails 4, asset pipeline causes user downloadable files to be downloaded twice, rails won't send_data as file, Ruby on Rails send_file, code in controller action running twice
Edited to reflect comment below. I would simple add a concern for handling downloads and then use
include Concerns::Downloads
to handle your download request. the routes.rb would look like this.
resources :movies do
member do
post 'download'
end
and in the view
<%= link_to 'Download', {:controller => 'movies', :action => 'download'}, {:method => :post } %></a>
Move the file to public folder
add only file name into link_to
<%= link_to "Downloadable File", "/"+filename, %>
Try setting the disposition to attachment in send_file:
class DownloadController < ApplicationController
def index
...
send_file( #path, :disposition => 'attachment' )
end
end
The issue may be that your browser is trying to open the file itself - :disposition => 'attachment' prompts the browser to download the file, even if it thinks the file is something that it can open.

Rails4 route not working

I have a route like this.
get 'mypage' => 'mypage#browserUpdate'
I added a controller called:
mypage_controller.rb
class MypageController < ApplicationController
def browserUpdate
puts "browser_update controller working"
end
end
In my views folder I have a folder called.
mypage > browserUpdate.html.erb
When i put a link in the application.html.erb
<div><%= link_to 'UPGRADE PATH', mypage_path %></div>
When I click the link I get an error:
Assertion failed: The URL '/mypage' did not match any routes in your
application
Rake routes says. mypage GET /mypage(.:format) mypage#browserUpdate
Anyone know what I'm doing wrong here.
I think your controller should be plural, i.e. it should be mypagescontroller.
routes.rb
get 'mypage' => 'mypage#browserUpdate', as: 'browser_update'
application.html.erb
<%= link_to 'UPGRADE PATH', browser_update_path %>
This should help.

Rails 3.2.7 and link_to

First of all, pretty new to Rails. I've been following a tutorial on using the 'link_to' command - basically, I have some links with text 'About Us', 'FAQ', 'Contact Us', and I want them to link to their respective pages.
Following the tutorial, the code in my contact_us.html.erb file goes like this:
<%= link_to "About Us", {:controller => ‘static_pages’, :action => ’about_us’} %>
My controller is called static_pages_controller.rb and I have an about_us method in that file, with no code in it:
def about_us
end
My controller code is:
class StaticPagesController < ApplicationController
def about_us
end
def faq
end
def contact_us
end
def t_and_c
end
def t_and_c_competition
end
def show
end
end
I get the error:
NameError in Static_pages#contact_us
undefined local variable or method `‘static_pages'......etc
Any ideas what's wrong? I think it might be because the tutorial is for ruby 1.8.6 and Rails 2.0.2, and I have Ruby 1.8.7 and Rails 3.2.7. I heard Rails is notorious for not being backwards compatible. Should I change my code? To what? Thanks for any help.
C.
Hi I think your problem it's you are using ’ instead of normal single quotes (') or doubled quotes (") when passing the parameters values in the link_to method
Change this:
<%= link_to "About Us", {:controller => ‘static_pages’, :action => ’about_us’} %>
to this:
<%= link_to "About Us", {:controller => 'static_pages', :action => 'about_us'} %>
I added this to my routes.rb:
get "static_pages/about_us"
and now it works. Thanks for your help!

Resources