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.
Related
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
I am trying to call a method in a controller from a button in a Rails view. I followed some other questions on here and got to this point.
routes.rb:
namespace :processing do
resources :applications do
stuff
post :test, :to => 'applications#test', :on => :member
end
end
the controller method is simply called test. Here is the relevant part of the controller:
def test
#application = Application.find_by(record_id: params[:id])
puts 'THIS IS A TEST'
end
Finally, I am calling the route with this code in an .erb file:
<%= button_to 'Send to Processing',
test_processing_application_path(record_id),
method: :post, form_class: 'btn btn-danger' %>
The button renders properly and seems to follow the right route. However, my problem is that the button attempts to redirect and render the route
/processing/applications/715707082/test which doesn't exist and causes a problem.
How do I just make this button not redirect/render something and instead simply call the method from the page it is currently on? Please feel free to link me relevant posts or close this if it duplicates an existing question.
The route in your routes.rb file does define the
test_processing_application_path as a POST to the path
/processing/applications/:id/test(.:format)
expecting to be implemented in the controller
processing/applications#test --
that is a file in app/controllers/processing/applications_controller.rb that defines a controller like:
module Processing
class Applications < ApplicationController
def test
# your code here
end
end
end
Instead of using a method you should use action and remote true will trigger an ajax request and prevent you from redirection. Then create a test.js.erb file for handling ajax request to update your DOM that will be executed after your action call.
<%= button_to 'Send to Processing',test_processing_application_path(record_id),
action: :test, form_class: 'btn btn-danger', :remote=>true %>
Hope this will help.
Cheers.
I am working with rails I have a controller name books and has a user defined method in it .I need to call this method so that i can see the output on console.And I dont want to call this method in helpers.
def approve
#user=current_user.users.find params[:id]
puts '#{#usery}'
end
Also I Have a link
<%= link_to 'approve',users_path,data: { :confirm => 'Are you sure to delete the folder and all of its contents?'} %>
.When i click on this link I want to call the above method on it .
You'll just need to define a route and call it through that:
#config/routes.rb
resources :users do
get :approve, on: :member
end
<%= link_to "Approve", users_approve_path(#user) %>
As #Rich suggested that, you can achieve it by member. Please note that when you'll create a member route in member block
resources :users do
member do
get 'approve'
end
end
then you'll get the params[:id]. Like
def approve
#user = User.find params[:id]
puts '#{#user}'
end
and when create a member route using :on then you'll get params[:user_id]. Like
def approve
#user = User.find params[:user_id]
puts '#{#user}'
end
Path will be same in both cases that is
<%= link_to "Approve", users_approve_path(#user) %>
Source Rails - Adding More RESTful Actions
Happy coding !!!
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.
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.