I am a beginner when it comes to Ruby on Rails, so I need a little bit of help. I started reading a basic tutorial recently, which was taught using Scaffolding. I made a "Clients" model: script/generate scaffold clients name:string ip_address:string speed:integer ... Inside the clients_controller.rb file, there is a method called show:
# GET /clients/1
# GET /clients/1.xml
def show
#client = Client.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #client }
end
end
For queries, I'd go to localhost:3000/clients/{Enter the ID here}. Instead of searching with the ID are the argument, I'd like to search with another value, like ip_address or speed, so I thought all I would have to do is change :id to :ip_address in "#client = Client.find(params[:id])". However, that does not work, so would someone please tell me how I can achieve a search with another parameter. Thanks!
This doesn't work because of the way things are routed
When you do something like
map.resources :client (See config/routes.rb)
This happens automatically when you use scaffold.
It sets up routes based on the assumption you're using an id.
One of these routes is something like
map.connect 'clients/:id', :controller => 'client', :action => 'show'
So :id is passed as a parameter as part of the URL.
You shouldn't have the IP be the primary identifier unless they're distinct - and even then it kind of messes with the RESTful routing.
If you want to have the ability to search by IP, modify your index action for the clients
def index
if params[:ip].present?
#clients = Client.find_by_ip_address(params[:ip]);
else
#clients = Client.all
end
end
Then you can search by ip by going to clients?ip=###.###.###
This line in your routes.rb file
map.connect 'clients/:id', :controller => 'client', :action => 'show'
implies that when the dispatcher receives an URI in the format "clients/abcdxyz' with GET Method, it will redirect it to show method with the value "abcdxyz" available in params hash with key :id.
EDIT
Since you have used scaffold, the clients resource will be RESTful. That means that when you send a GET request to "/clients/:id' URI, you will be redirected to show page of that particular client.
In your controller code you can access it as
params[:id] # which will be "abcdxyz"
The find method that is generated by scaffold searches on the primary key i.e 'id' column. You need to change that statement to either
#client = Client.find_by_ip_address(params[:id]) #find_by_column_name
OR
#client = Client.find(:first, :conditions => [":ip_address = ?", params[:id]])
:-)
Related
In my Rails routes.rb file I'm wanting to do something like the following.
get '/:id' => 'pages#show'
get '/:id' => 'articles#show'
So that if a visitor types in
http://www.example.com/about-this-site
The pages controller in the above example would get first shot at handling it. Then if not, the next controller in line would get a shot.
REASONs for wanting to do this:
1) I'm trying to port my Wordpress site over without establishing new urls for all my pages and blog posts. As it stands, all of my blog post files and pages are accessed directly off the root uri '/' folder.
2) Because I'm not able to, it's a learning thing for me. But, I want to do it without a hack.
How about redirecting to the second controller from your first controller?
in PagesController
def show
unless Page.find_by(id: params[:id])
redirect_to controller: :articles, action: :show, id: params[:id]
end
end
in ArticlesController
def show
# Handle whatever logic here...
end
Edit
If you really don't want to redirect then you can consolidate the logic into a single action:
def show
if Page.find_by(id: params[:id])
render :show
elsif Article.find_by(id: params[:id])
render controller: :articles, action: :show
else
# Handle missing case, perhaps a 404?
end
end
However, I'd recommend using a redirect if possible. It's a cleaner solution and keeps your controller code isolated.
I am wanting to expand the URLs associated with the contents of a model called Product, at the moment, I can view a specific product by going to products/ID.
I would like to extend the product URL so it includes some more descriptive information, such as the product name.
I have previously been advised to adjust the to_param function (in Product.rb) as below:
def to_param
"#{id}-#{product_name.parameterize}"
end
However, this doesn't currently work. The URL associated with each product appears correctly when you hover over it / click it, but there is no matching product found. I get the error no match for ID=ID-specific-product-name
If i visit /products/id i can still successfully view the specific item
Can anyone guide me as to how I could generate this longer URL containing the product name (:product_name)?
EDIT
The show controller action in my controller is:
def show
#uniqueturbo = Uniqueturbo.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #uniqueturbo }
end
end
If you're trying to make some SEO friendly urls
http://www.yourdomain.com/products/123123-My-Little-PonyBook
I think that the easiest way is to change the routes, like this
get '/products/:title/:id' => "products#show"
and then you'll get seo-friendly url's like:
http://www.yourdomain.com/products/My-Little-PonyBook/123123
To generate this url, create helper
def url_for_product(product)
"/products/#{product.title}/#{product.id}"
end
The other way is to leave the normal RESTful route, and reparse 'id' parameter, like:
def show
product_id = params[:id].split('_')[0] # :-)
# ...
end
and still you need the helper method, this time, sth like:
def url_for_product(product)
product_path(product) + "_#{product.title.tableize}"
end
I'm working on implementing boards.
Now I have BoardsController and PostsController.
By default, posts are nested by boards.
I want all board's post list have their special route using same PostsController
so I did this in route.rb
resources :notice, :controller => "posts", :board_id => 1
resources :faq, :controller => "posts", :board_id => 2
resources :qna, :controller => "posts", :board_id => 3
At first, it seems to work. But I realized a problem.
because i used same 'PostsController' in these resources.
Codes related to path are same when doing controller's action
like,
posts_controller
def create
#post = Board.find(params[:board_id]).posts.build(params[:post])
if #post.save
redirect_to board_posts_path(#post.board_id)
else
render 'new'
end
end
when I go to localhost:3000/notice/new, it works fine
but when I submitted the new post, controller redirects to localhost:3000/boards/1/posts/
because of redirect_to board_posts_path(#post.board_id)
and that's not what I want.
I could handle this using if statements, but it seems messy.
Is there any proper solution to this?
You can use the self.send on the controller to dynamically resolve the path by the post type. Assuming you have the type of the created post in a string ( I didn't understand from your question if Notice < Post and if you use Single Table Inheritance):
post_type = # Get the specific post type ( "notice, faq ...")
redirect_to self.send("#{post_type}_path", #post.board_id)
I'd like to create a rails route for editing a user's profile.
Instead of having to use /users/:id/edit, I'd like to have a url like /edit_profile
Is it possible to create a dynamic route that turns /edit_profile into /users/{user's id}/edit, or should I do thing in a controller or?
You might want to create a separate controller for this task but you could also continue using users_controller and just check whether there is a params[:id] set:
def edit
if params[:id]
#user = User.find(params[:id])
else
#user = current_user
end
end
But you should note that /users normally routes to the index action and not show if you still have the map.resources :users route. But you could set up a differently called singular route for that:
map.resources :users
map.resource :profile, :controller => "users"
This way /users would list all the users, /users/:id would show any user and /profile would show the show the currently logged in users page. To edit you own profile you would call '/profile/edit'.
Since a route and controller serve two different purposes, you will need both.
For the controller, assuming you're storing the user id in a session, you could just have your edit method do something like:
def edit
#user = User.find(session[:user_id])
end
Then have a route that looks something like:
map.edit_profile "edit_profile", :controller => "users", :action => "edit"
This route would give you a named route called edit_profile_path
Tomas Markauskas's answer could work, but here's the answer to your question from the Rails Guide:
get 'edit_profile', to: 'users#edit'
So, when someone goes to www.yoursite.com/edit_profile, it will route to www.yoursite.com/users/edit.
Then, in your controller you can access the user with
#user = User.find(session[:current_user_id])
Assuming you set that session variable when someone logs in. Also, don't forget to check if they're logged in. This will work if your using Resourceful Routing (the Rails default) or not.
Source: http://guides.rubyonrails.org/routing.html
make the route as
get '/users/:id/edit', to: 'users#edit', as: 'edit_profile'
As explained in this link section 'The hard way' :
http://augustl.com/blog/2009/styling_rails_urls/
The url will be
/users/edit_profile
Because the ID is no longer in the URL, we have to change the code a bit.
class User < ActiveRecord::Base
before_create :create_slug
def to_param
slug
end
def create_slug
self.slug = self.title.parameterize
end
end
When a user is created, the URL friendly version of the title is stored in the database, in the slug column.
For better understanding read the link below
http://blog.teamtreehouse.com/creating-vanity-urls-in-rails
write it in any home controler.
def set_roots
if current_user
redirect_to dashboard_home_index_path
else
redirect_to home_index_path
end
end
in routes.rb file
root :to => 'home#set_roots'
match "/find_roots" => "home#set_roots"
In Ruby on Rails, is it possible to change a default action for a RESTful resource, so than when someone, for example, goes to /books it gets :new instead of the listing (I don't care if that means not being able to show the listing anymore)?
I'd point out that if you are pointing /books to /books/new, you are going to be confusing anyone who is expecting REST. If you aren't working alone, or if you are and have other come on board later, or if you expect to expose an API to the outside, the REST convention is that /books takes you to a listing, /books/new is where you create a new record.
Not sure why would you do such a thing, but just add this
map.connect "/books", :controller => "books", :action => "new", :conditions => { :method => :get}
to your config/routes.rb before the
map.resources :books
and it should work.
Yes. You should be able to replace your index method in your controller...
def index
#resource = Resource.new
# have your index template with they proper form
end
In the same vein, you can just do
def index
show
end
def index
redirect_to new_book_path
end
I think would be the simplest way.