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.
Related
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.
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.
I'm somewhat new to rails. I'm going through making the classic twitter clone right now. I want to have a search bar on my homepage that allows the user to search for a twitter handle, and if the handle exists, it will send the user to the show page for that twitter handle.
I've been following a RailsCast on how to implement a simple search, but instead of doing it on the index like the video, I want to do it on the show action. I've run into some problems though. The form sits on my user index view.
Here is the error:
ActionController::UrlGenerationError in Users#index
Showing c:/Sites/Projects/twitterapp/twitter/app/views/users/index.html.erb where line #2 raised:
No route matches {:action=>"show", :controller=>"users"} missing required keys: [:id]
Here is the form:
<%= form_tag(user_path, method: 'get') do %>
<%= text_field_tag(:search, params[:search]) %>
<%= submit_tag("Search", name: nil) %>
<% end %>
Here is my show action:
def show
#user = User.search(params[:search])
end
And here is my search method in my user model:
def self.search(search)
if search
find(:all, conditions:['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
Actually you cannot use the show method as a search result finder. Because according to the rails convention:
For any resource like users, rails scaffold generates index,new, show, create, update, delete methods based on your routes files.
Thus based on the conventional way, show method always asks for an object. Lets say you are using UserContoller show method. It asks for a user object. Which you haven't provide in the form. that's why :id missing error is given.
I would tell you to do some more learning. And for searching create a different method in a different controller and define that controller method to the routes.rb file. This is the best way to do.
If you still want to use the show method, then change the show methods routing from the routes.rb file. You've to manually declare the show action on routes file.
you are using user_path and path need to inform id from present user
you can do this in action :index but I recommend you to create a action to this
view
<%= form_tag(search_users_path, method: 'get') do %>
<%= text_field_tag(:search, params[:search]) %>
<%= submit_tag("Search", name: nil) %>
<% end %>
routes.rb
resources :users do
post 'search', :on => :collection
end
users_controller.rb
def search
#user = User.search(params[:search])
end
You should to create a view search.html.erb similar as index.html.erb
As Emu and Breno pointed what causing the problem user_path requires an user id
Solution idea:
Why not just point to users index action? like this:
<%= form_tag(users_path, method: 'get') do %>
<%= text_field_tag(:search, params[:search]) %>
<%= submit_tag("Search", name: nil) %>
<% end %>
users_controller.rb:
def index
if params[:search]
#user = User.search(params[:search])
end
end
and you can use ajax remote: true to handle the returned user object
Found your question via Google, but the responses and suggestions didn't work for me. Found another solution that did, so seems worth posting here.
"Search and Filter Rails Models Without Bloating Your Controller":
http://www.justinweiss.com/articles/search-and-filter-rails-models-without-bloating-your-controller/
If I have the following code in my controller:
# encoding: utf-8
module Admin
class SylabusController < BaseController
def show_all
#questions = #topic.questions.all
end
And I have the index where I would like to "call" the show_all in order that appears a new web page with all the questions. How does be the link?
<%= link_to 'All the questions'.html_safe, #sylabus.show_all, class: 'btn' %>
With the following error.
NoMethodError in Admin/mupets#index
Showing app/views/admin/sylabus/index.html.erb where line #41 raised:
undefined method `show_all' for nil:NilClass
Is it my error in the link code? or Do I have to define something in the routes?
Thank you for your time and help
You cannot link directly to actions on controllers, you can only make requests which are connected to a controller/action via your routing table.
You need a route which will reach that action, and then you need a view which will render output to the user.
In your SylabusController:
def index
# are you sure that #topic is not null?
#questions = #topic.questions
end
In your view
<%= link_to 'All the questions', #questions, class: 'btn' %>
Just get class variable associateded with controller action :)
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.