FAQ Database Discussion Community


Better way of building urls

ruby-on-rails,ruby,rails-routing
I have a route configured like this: get "calendar/:year/:month" => "calendar#month", as: :calendar_month To create links for "next" month and "previous" month, this is what you need to use in your template: <%= link_to "Next", calendar_month_path(year: @date.next_month.year, month: @date.next_month.month) %> or a bit simpler: <%= link_to "Previous", calendar_month_path(@date.prev_month.year, @date.prev_month.month) %>...

Simple_form path issue

ruby-on-rails,simple-form,rails-routing
Here is what I have in my view: <%= simple_form_for :artist, :url => url_for(:action => 'upvote', :controller => 'artists'), :method => 'post' do |f| %> <%= f.input :choose_an_artist, :selected => "first artist", collection: [["first artist", 1], ["second artist", 2], ["third artist", 3], ["fourth artist", 4]] %> <%= f.submit "Vote" %>...

Can I get a controller to show up in the url under a different name?

ruby-on-rails,ruby,routing,routes,rails-routing
I have a controller called "Pages". Can I make it show up in the url bar under a different name? For example, when I render the 'show' template, it shows up under this url: localhost:3000/pages/:id. Could I make it show up as localhost:3000/people/:id? I only care about the 'show' url;...

why do rails have nested resources?

ruby-on-rails,rails-routing
Learning rails and came across this nested resources in routes.rb resources :posts do resources :comments end Given that there is an association between posts and comments ( a post has many comments, a comment belongs to a post), I can kind of see the association in a similar fashion in...

Why does rails redirect to the show action after creating?

ruby-on-rails,rails-routing
New web developer here, and I think I may be missing some very fundamental knowledge. Given the code def create @post = Post.new(post_params) if @post.save redirect_to @post else render "new" end end why does the view templates redirect to the def show action? If I do not define a def...

Rails routes with optional parameters priorities

ruby-on-rails,ruby-on-rails-3,routing,routes,rails-routing
I have an strange issue in my routes file. This is the part that I need to understand This routes doesn't work # V3 # V3 - Home Page match '/:locale' => 'v3/home#index', :constraints => V3Constraint, :as => :home # V3 - Search match '(/:locale)/products/search' => 'v3/products#search', :constraints => V3Constraint...

Render post request in template Ruby on Rails

ruby-on-rails,ruby,rails-routing
I've been struggling a little getting the content of a form post request to render on a results.erb template. here is my petscontroller: class PetsController < ApplicationController # before_action :set_pet, only: [:show, :edit, :update, :destroy] PETFINDER = Petfinder::Client.new('xxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxx') # GET /pets # GET /pets.json def results @animal_list = Pet.all...

routes with the same resource multiple times

ruby-on-rails,model-view-controller,rails-routing
I have a model Examples which has_many :users but also belongs_to a Group. I want to have an index of both just examples and another index of group examples. like- url.com/examples(should only show examples without a group_id) and url.com/group/1/examples. currently my routes file looks like this: Rails.application.routes.draw do resources :groups...

Rails 3 helper for match route

ruby-on-rails,ruby-on-rails-3,rails-routing
In a rails 3 application following match route is defined in routes.rb match 'accounts/:account_type/:account_id/edit_account' => 'accounts#edit_account' In controller i redirect to this URL redirect_to "/accounts/account/#{account_id.to_s}/edit_account" It works fine but i need a route helper instead of manually building the URL something like this edit_account_accounts_path(account_id: id, account_type: 'some_type') Is there any...

NoMethodError in Blog::PostsController#create

ruby-on-rails,ruby,ruby-on-rails-4,mongoid,rails-routing
Note: Rails newb here. So, I recently created a Rails app with mongoid gem for use of MongoDB. I have a namespace route of :blog with a nest of resource of posts Routes.rb: Rails.application.routes.draw do namespace :blog do resources :posts end end The error comes from app/controllers/blog/posts_controller.rb: Class Blog::PostsController <...

Rails - Cannot find controller_action_path

ruby-on-rails,rails-routing
Hey guys i probably have a simple problem which annoys me for 2 hours now. I try to set up a menu_item_icon which is linked to one of my controller actions. So far every of these menu_items work. But there is one where I always get the failure message 'Controller_Action...

Rails still use :id as resource identifier after changing primary_key

ruby-on-rails,ruby-on-rails-4,rails-routing
In Airport model I'm using primary_key as airport international code, but Rails still use :id in request. I've set in airport.rb self.primary_key = 'code' And in airports_controller.rb def show @airport = Airport.find(params[:code]) end But when I try to get domain.com/airports/led, I receive: Couldn't find Airport without an ID In request...

How to make resource dynamic routes?

ruby-on-rails,ruby-on-rails-4,rails-routing
I need to define route like this. api/:some_entity_id/bla api/:some_entity_id/bla/new I can define this with get for each route. Can I manage to do this with resources because I need all crud actions?...

Change Rails namespaced route to personalized params route

ruby-on-rails,routing,rails-routing
TL;DR: I want to have username621/posts/title-of-post instead of member/posts/1 The changing of post id to post title was easy enough since I used the freindly_id gem to generate the slugs. However, I am having difficulty routing to a personalized params route instead of the current namespaced route. Here is the...

Rails 4.2 Namespace Routes Change after Initial Load

ruby-on-rails,ruby-on-rails-4,rails-routing
Hi I'm having a strange issue with Rails 4.2 namespaces and routing. I'm updating from rails 4.0.13 to Rails 4.2.1 and have a namespace route set up like so: namespace :admin do get '', to: 'dashboard#index', as: '/' get 'denied', to: 'dashboard#denied', as: '/denied' get 'blocked', to: 'dashboard#blocked', as: '/blocked'...

Rails 4 custom named route not calling correct property of model

ruby-on-rails,rails-routing
As per http://edgeguides.rubyonrails.org/routing.html#overriding-named-route-parameters I am defining a route with a custom named route parameter instead of :id to create friendly URLs. routes.rb looks like: resources :spaces, param: :name Running rake routes does indeed give the correct paths with dynamic segments: space GET /spaces/:name(.:format) spaces#show But using space_path still tries to...

Devise conflicting with other routes using delete

ruby-on-rails,ruby-on-rails-4,devise,rails-routing
I have these 2 routes that are conflicting in my application destroy_users DELETE /users/:id(.:format) users#destroy destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy the corresponding part of my routes file is as follows match '/users/:id', to: 'users#destroy', :via => :delete, :as =>:destroy_users devise_for :users resources :users When I try to destroy a session using...

How to properly define Rails routes so that some actions of a resource go to one controller and some go to another?

ruby-on-rails,ruby-on-rails-4,rails-routing
My app have products, categories and subcategries. I have an admin interface at the url /dash to edit these details. I tried to set the urls like this : Products create, update, destroy and index under the url /dash/products Products show under /products/porduct-code Categories show under /category-name Categories edit list...