Rails4 route not working - ruby-on-rails

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.

Related

ActionView::Template::Error - No route matches action in button_to \ link_to calling controller action

I'm trying to create a view with a button calling a controller action for a Redmine Plugin (unfortunately, I'm quite new at Rails). I'd need to create a method for my controller (here A Controller) so i can update some Active Records.
I created a controller:
redmine_a_controller.rb
class RedmineAController < ApplicationController
def prova(input)
puts input
return input
end
def p
puts 'vuoto'
returns 'vuoto'
end
end
The actions work correctly when called from the console. Unfortunately, when called from my view _a.html.erb:
<%=
button_to l(:imp_update), {
:controller => 'RedmineAController',
:action => 'p',
method: :post
}
%>
It returns this error:
ActionView::Template::Error (No route matches {:action=>"p", :controller=>"RedmineAController", :id=>"prova", :method=>:post, :tab=>"important_users"}):
I tried by calling the action on a new instance:
<%=
button_to l(:imp_update), {
:action => RedmineImportantUsersController.new.prova(1),
method: :post
}
%>
but it looks for the action in the Projects controller
ActionView::Template::Error (No route matches {:action=>"1", :controller=>"projects", :id=>"prova", :method=>:post}):
How can I make button_to call the correct prova method in the RedmineAController controller?
Thanks in advance for any help.
Add routes in routes.rb file
resources :redmine_as do
collections do
post :a
end
end
After that, Please fine the Routes url by typing on terminal below command
rake routes | grep p
Set the url in Link or button
link_to "Your Link Name", p_redmine_as_path
p_redmines_path it will be return by that "rake routes
| grep p" command.
As Ketan indicated, I was missing a route!
I put in my routes.rb:
resources :important_user do
collection do
post :u_update
end
end
and in my controller:
def u_update
puts params[:p_id]
puts "update!"
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.

Routing error in edit method

I'm writing a little Rails CMS and I'm a little stuck with a routing error. To begin with, I have a basic model called Entry, which other models are inheriting from. When I try to edit an existing model, it returns me an error
No route matches [PATCH] "/admin/posts/entries"
In my routes.rb in CMS plugin I have the following:
Multiflora::Engine.routes.draw do
root "dashboard#index"
scope "/:content_class" do
resources :entries
end
end
and in test app's routes.rb I have
mount Multiflora::Engine, at: '/admin'
In application_controller.rb I also tweaked routes a little:
def content_entries_path
entries_path(content_class: content_class.tableize)
end
helper_method :content_entries_path
def content_entry_path(entry)
entry_path(entry, content_class: content_class.tableize)
end
helper_method :content_entry_path
def new_content_entry_path
new_entry_path(content_class: content_class.tableize)
end
helper_method :new_content_entry_path
def edit_content_entry_path(entry)
edit_entry_path(entry, content_class: content_class.tableize)
end
helper_method :edit_content_entry_path
And in my show.html.erb I have this:
<%= link_to 'Edit', edit_content_entry_path(#entry) %>
When I navigate to edit_content_entry_path, it shows me edit page correctly, but when I try to save edited entry, it returns me an error stated above. When I run rake routes, it returns me the following:
entries GET /:content_class/entries(.:format) multiflora/entries#index
POST /:content_class/entries(.:format) multiflora/entries#create
new_entry GET /:content_class/entries/new(.:format) multiflora/entries#new
edit_entry GET /:content_class/entries/:id/edit(.:format) multiflora/entries#edit
entry GET /:content_class/entries/:id(.:format) multiflora/entries#show
PATCH /:content_class/entries/:id(.:format) multiflora/entries#update
PUT /:content_class/entries/:id(.:format) multiflora/entries#update
DELETE /:content_class/entries/:id(.:format) multiflora/entries#destroy
So, the error was in my edit.html.erb view -- instead of
<%= form_for(#entry, as: :entry, url: content_entry_path(#entry)) do |f| %>
I had
<%= form_for(#entry, as: :entry, url: entries_path do |f| %>
When I rewrote it, everything worked!

Link to other pages wih same controller using Ruby on Rails

I am new to ruby on rails and stuck to very basic problem.I have created a controller named as custom_hello and define 2 methods. what i want is when i click on the link it will take me to next page which is under the same controller.i just don't know how to configure the routes properly.Any help would be appreciated...
Here it is:
app/controllers/custom_hello_controller.rb
class CustomHelloController < ApplicationController
def method1
end
def method2
end
end
config/routes.rb
get 'custom_hello/method1'
get 'custom_hello/method2'
Create 2 files in your views:
app/views/custom_hello/method1.html.erb
app/views/custom_hello/method2.html.erb
You can create links with:
<%= link_to 'Method 1', custom_hello_method1_path %>
<%= link_to 'Method 2', custom_hello_method2_path %>
However, you may consider creating RESTful controllers and routes. Please read here
<%= link_to 'link_name', :action => 'Your_method_name', :controller => 'custom_hello' %>
now you need to write 'custome_hello/Your_method_name' in your routes.rb and create Your_method_name.html.erb page in that controller when you click on link you will navigate to Your_method_name.html.erb page try it, this will work.

Routing Error with 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 %>

Resources