creating multiple web page in rails 3 project - ruby-on-rails

I am fairly new to Ruby and Rails. I am using rails 3. I am creating a project where people can login and post jobs and people can search jobs etc. I have created my users model and jobs model and everything is working just fine. I have started creating the home page layout and have added links to the other pages post work, find work, contact, about. I have a few questions hopefully someone can help please.
When I get into the rails server and check out the project and my home page it shows the login set up from the users model and it also shows the jobs model where someone can post a job. I am trying to figure out how I can make it so the jobs do not get posted on the home page but the find work page and people can search from that page? Do I need to link the jobs model to the find work page something like that?
How do I create the web pages for these other links i have established. I can not figure out how to build the page layout for the post work link i have or the other links I have for people to view the page layout. Any help is appreciated? Thanks again!

What you ask is mostly the basics of Rails. You can render jobs or any other model in any view you want, you just need to "send" these jobs to the views from you controller.
1.- Configure routes:
#config/routes.rb
get "/jobs" match 'jobs#index' # i.e 'controller#action'
2.- Configure model controller:
#app/controllers/jobs_controller.rb
class JobsController < ApplicationController
def index
#jobs = Jobs.last(10)
render 'index'
end
end
3.- Configure view:
#app/views/jobs/index.html.erb
<% #jobs.each do |job| "do something with each #{job}" end %>
This is just a birds sight overview you should check some Rails tutorials.
About generating pages for content that isn't in the db...
I had this issue once and solved it like this:
#config/routes.rb
get "/:view" match 'home#views' # rails g controller home
#app/controllers/views_controller.rb
class HomeController < ApplicationController
def views
render params[:view]
end
end
Now generate a view for each new page, lets say "About", under /app/views/home and you can access each of them via /view_name.

Related

Ruby on Rails: How to use current_page helper to get subdirectories of a page

So I have a program where a user can create events, and there will be multiple events. So the events index page looks like "/events" on the browser, and it's subdirectory show pages looks like "/events/1 /events/2 ... etc".
In my layout page I am checking the current page in order to decide what background color the page will have. This is easy for the events index page; I just check if it is that page via the command:
if current_page?(events_path) ...
But I want to check if the current page is a specific 'show' page of the index (a standard scaffold generated page). How would I do that? This is my attempt, I am confused about how the routing would work in this case:
if current_page?(events_id_path) ...
So simple, try the below code
if current_page?(event_path(#event))
Assuming you have #event defined in your controller.
or
if current_page?(controller: 'events', action: 'show', :id => id)
For more Info, see this API Doc

Ruby on Rails: How to link/route from one view/page to another view/page with a different controller

I have a view template within the following file in my Rails application:
app/views/static_pages/mission.html.erb
I want to link to that page from a view template in a different folder (corresponding to a different page in the app):
app/views/home/home.html.erb
I don't know what to write in my mission method in the StaticPagesController. I'm also a newbie at Rails. I also don't know how to write the route for it and I suspect I may need to write a get 'something' in my routes.rb file.
Can anybody help with this?
What you are asking can be accomplished in this way:
In your routes.rb:
get "/path/to/your/mission/page", to: "static_pages#mission", as: "mission"
Then in your static_pages_controller.rb:
class StaticPagesController < ApplicationController
# You can leave the mission method blank, it will render
# the corresponding static_pages/mission.html.erb by default
def mission
end
end
This should set up the static page, so that when you visit: localhost:3000/path/to/your/mission/page, you should be able to see the mission.html.erb page rendered.
To link to the mission page from any other template, you can simply point a link to it, like so:
<%= link_to "Mission Page", mission_path %>
Or, since it's a static page, you can just hardcode the path into the markup:
Mission Page
Since you're new to Rails (welcome to the world of Rails btw :D), I hope you find these really great resources useful:
Official Rails Guides
Rails For Zombies
Railscasts by Ryan Bates
Hope this was helpful!

How do I take this from the console to a controller and view

I'm learning Ruby on Rails (my first MVC) and have successfully setup a many-to-many relationship between "Agents" and "Zipcodes." What I'm trying to do currently is to get the associated agent based on the zip code entered by the user. I'm able to so successfully in the console, but am having a difficult time translating it to a controller and view.
What I do in the console:
zip = Zipcode.find_by_zip(gets.chomp)
=> 92562
zip.agents
The hangup I'm having is how to translate this into an action that I can access from a view with a form.
I've started by defining the action (agents#find), but am stumped as to whether this is correct and what comes after it.
def find
#user_zip = Zipcode.find_by_zip(params[:zip])
end
Hopefully someone awesome in here can point a n00b in the right direction.
When just starting with rails, I'd suggest avoiding custom actions like #find as much as possible, and instead sticking to the "Big 7" RESTful routes. Rails is really smooth when you work with it and do what it expects.
It sounds like maybe you're trying to identify a given zipcode, and then list all the agents associated with it. That sounds like a #show on Zipcode.
config/routes.rb
resources :zipcodes
app/controllers/zipcodes_controller.rb
ZipcodesController < ApplicationController
def show
#zipcode = Zipcode.find_by_zip(params[:id])
end
end
app/views/zipcodes/show.html.erb
<div>
<p>This zipcode has the following agents:</p>
<ul>
<%= #zipcode.agents.each do |agent| %>
<li>Agent #<%= agent.id %></li>
<% end %>
</ul>
</div>
You can see this page by browsing to /zipcodes/[zip].
Just put #user_zip = Zipcode.find_by_zip(params[:zip]) in the controller instead of the model.
In the view you will be able to call #user_zip.
Welcome to Rails! I recently started learning Rails as well, but I think I can help: A controller action will redirect by default to a view with the same name. So after assigning the value of #user_zip, the controller will serve up agents/find.html.erb, which will have access to #user_zip. Since #user_zip is an instance of Zipcode, you'd be able to use #user_zip.agents.
Somewhat tangential, but I also suggest considering using search rather than find for the action name, only because find and its variations are used elsewhere in Rails.

RESTful API in rails

I am very new to rails and following a tutorial for RESTful API so let me excuse if it is of not very good quality as I am equally a starter for these kind of terminologies as well.
I created a controller kitten with a command rails g controller kitten index
and in the index method I posted this code -
class KittenController < ApplicationController
def index
require 'open-uri'
kittens = open('http://placekitten.com/')
response_status = kittens.status
response_body = kittens.read[559, 441]
puts response_status
puts response_body
end
end
and un commented match ':controller(/:action(/:id))(.:format)' in routes.rb
When i navigate through this - http://localhost:3000/kitten
this is what i am getting in my browser -
Kitten#index
Find me in app/views/kitten/index.html.erb
and this in my command line -->
Now my question why it so although i am expecting it in my browser but the cat is shown in command prompt instead of browser ..i am new to rest resource so please excuse if it is a bad one :(
I don't know what tutorial you're following, but doing this seems like a very odd thing to do for Rails in general and learning RESTful APIs in particular.
Anyway, the puts in your controller outputs text to Ruby's standard out, which is going to be the terminal where the server started. That's why this is appearing in the console rather than in your browser: puts is putting it there.
If you want this to appear in a web page, you'll need to make a view for that controller action. Perhaps following further along your tutorial will get you there: if not, you might want to find a better one.
You should read the Model-View-Controller rails guide.
Controllers provide the “glue” between models and views. In Rails, controllers are responsible for processing the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation.
Define your variable in the controller and display it in the view:
class KittenController < ApplicationController
def index
#variable = 'Hello World'
end
end
In your view (app/views/kitten/index.html.erb):
<%= #variable %>
Rails controllers setup responses with a render call.
When the call is not performed it instantiates the appropriate view and renders that view. In your case that is index.html.erb
Try this:
render :text => kittens.read[559, 441], :status => kittens.status

How to create view in RoR if skipped during controller generation

When I run this:
rails generate controller hello index
it no doubt generates hello controller, but accidentally when I run another command like this:
rails generate controller world
it creates the world controller successfully, but missed the Route "world/index" like as "hello/index". For this mistake I need to use destroy controller and then generate it once more, is thr some kind of mid way command that I can generate if forgotten something rather than destroying and creating every time.
This command
rails generate controller contact-us index
creates a route as contact_us/index or contact_us after changing routes.rb under config folder. How could I create a more SEO friendly URL in RoR? Like localhost:3000/contact-us?
I am working on some very basic rules to follow RoR..like 3 static pages (Home, About us, Contact Us) Just simple html content to understand more, will certainly add more features to it as well.
localhost:3000/home
localhost:3000/About_us
localhost:3000/contact_us
I created this via creating home, About_us, contact_us controller command and then changed html in views. Since I am on an initial phase, I read somewhere for static pages we can create this in our public like what we have error pages there in the folder or the approach im using is correct?
when you use rails generator it will create controller and view folder for it
rails generate controller test
will create test_controller.rb and view/test folder
rails generate controller test index
will create test_controller.rb and view/test/index.html.erb file as well as define a route for you
However what its sounds like you are trying to do is have single controller with static pages what i would suggest you do is generate home_controller with home, aboutus, and contact actions and than map the routes to them like so
rails generate controller home
controllers/home.rb
HomeController < ApplicationController
def index
end
def about_us
end
def contact
end
end
routes.rb
match '/home', :to => 'home#index'
match '/About_us', :to => 'home#about_us'
match '/Contact_us' , :to=> 'home#contact_us'
and than define your views in
views/home/index.html.erb
views/home/about_us.html.erb
views/home/contact_us.html.erb

Resources