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!
Related
I am developing a project in Ruby on rails 5.2, and in this route it tells me that I have an error and that the specified route is not found. but when checking, the route is there or at least I think so.
Here's my routes.rb:
resources :checkin do
post :get_barcode, on: :collection
end
checkin_controller.rb:
class CheckinController < ApplicationController
def index
#checkin = CheckIn.all
end
def show
end
def new
#checkin = CheckIn.new
#checkin.upc = params[:upc]
end
def edit
end
def update
end
def destroy
end
def get_barcode
#checkin = Merchant.find_or_initialize_by(upc: params[:upc])
unless #checkin.new_record?
redirect_to #checkin
else
redirect_to new_product_path(upc: params[:upc])
end
end
end
And my link in my view:
<%= link_to "Check-In", checkin_path, :class => "nav-link" %>
here's a image of my error page:
If you run rake routes in your console, you'll see that your routes are:
get_barcode_checkin_index POST /checkin/get_barcode(.:format) checkin#get_barcode
checkin_index GET /checkin(.:format) checkin#index
POST /checkin(.:format) checkin#create
new_checkin GET /checkin/new(.:format) checkin#new
edit_checkin GET /checkin/:id/edit(.:format) checkin#edit
checkin GET /checkin/:id(.:format) checkin#show
PATCH /checkin/:id(.:format) checkin#update
PUT /checkin/:id(.:format) checkin#update
DELETE /checkin/:id(.:format) checkin#destroy
As you can see, the checkin_path expects an id, which you are not providing here:
<%= link_to "Check-In", checkin_path, :class => "nav-link" %>
Your error probably says something about missing id, but you don't provide the error in your question, so we can't see exactly what it says.
BTW, by convention, CheckinController should probably be CheckinsController. And your routes should probably be:
resources :checkins do
post :get_barcode, on: :collection
end
As you said in one of your comments, you're expecting the URL for the index page?
Then instead of
<%= link_to "Check-In", checkin_path, :class => "nav-link" %>`
you need to use
<%= link_to "Check-In", checkin_index_path, :class => "nav-link" %>
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.
It would be exactly like Wikipedia's "Random Article" button. Takes you to a random webpage on the site.
<%= button_to "Random Article", article_path(random_article) %>
#app/helpers/application_helper.rb
class ApplicationHelper
def random_article
Article.order("RAND()").first.pluck(:id)
end
end
With help from this article: Random record in ActiveRecord
Some more information would make this much easier to answer, but there are a few ways to do it. The best, in my opinion, would be something like this:
routes.rb:
resources :articles do
get :random_article
end
#OR
get 'random_article' => 'some_controller#random_article', as: 'random_article'
articles_controller.rb
def random_article
redirect_to article_path(Article.all.sample.id)
end
And on your page:
<%= link_to 'Random Article', random_article_path %>
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.
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 %>