ruby-on-rails,ruby,ruby-on-rails-3,routing,refactoring , Refactoring nested routes, how do I address this redirect_to?

Refactoring nested routes, how do I address this redirect_to?


Tag: ruby-on-rails,ruby,ruby-on-rails-3,routing,refactoring

I am working on an assignment for a project I have been working on. I was just introduced to refactoring my nested routes. Here are the changes.


   resources :topics do 
     resources :posts, except: [:index] do
       resources :comments, only: [:create, :destroy]


  resources :topics do
    resources :posts, except: [:index]

  resources :posts, only: [] do
    resources :comments, only: [:create, :destroy]

After this my instructions are as follows:

Run rake routes to see how this changes routing. Then make the following changes to refactor with these new, shallower routes:

Change the comment paths, in both the comments/_comment.html.erb and the comments/_form.html.erb partials.

Change the CommentsController actions so that they no longer initialize @topic.

Derive @topic from @post because we still want redirect_to the @post page after creating or destroying a comment. Furthermore, @post is still nested under @topic.

Visit posts, then delete and create comments to test this new shallow nesting.

rake routes

▶ rake routes
                  Prefix Verb   URI Pattern                                Controller#Action
         comments_create GET    /comments/create(.:format)                 comments#create
        new_user_session GET    /users/sign_in(.:format)                   devise/sessions#new
            user_session POST   /users/sign_in(.:format)                   devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                  devise/sessions#destroy
           user_password POST   /users/password(.:format)                  devise/passwords#create
       new_user_password GET    /users/password/new(.:format)              devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)             devise/passwords#edit
                         PATCH  /users/password(.:format)                  devise/passwords#update
                         PUT    /users/password(.:format)                  devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                    devise/registrations#cancel
       user_registration POST   /users(.:format)                           devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)                   devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                      devise/registrations#edit
                         PATCH  /users(.:format)                           devise/registrations#update
                         PUT    /users(.:format)                           devise/registrations#update
                         DELETE /users(.:format)                           devise/registrations#destroy
       user_confirmation POST   /users/confirmation(.:format)              devise/confirmations#create
   new_user_confirmation GET    /users/confirmation/new(.:format)          devise/confirmations#new
                         GET    /users/confirmation(.:format)              devise/confirmations#show
                    user PATCH  /users/:id(.:format)                       users#update
                         PUT    /users/:id(.:format)                       users#update
             topic_posts POST   /topics/:topic_id/posts(.:format)          posts#create
          new_topic_post GET    /topics/:topic_id/posts/new(.:format)      posts#new
         edit_topic_post GET    /topics/:topic_id/posts/:id/edit(.:format) posts#edit
              topic_post GET    /topics/:topic_id/posts/:id(.:format)      posts#show
                         PATCH  /topics/:topic_id/posts/:id(.:format)      posts#update
                         PUT    /topics/:topic_id/posts/:id(.:format)      posts#update
                         DELETE /topics/:topic_id/posts/:id(.:format)      posts#destroy
                  topics GET    /topics(.:format)                          topics#index
                         POST   /topics(.:format)                          topics#create
               new_topic GET    /topics/new(.:format)                      topics#new
              edit_topic GET    /topics/:id/edit(.:format)                 topics#edit
                   topic GET    /topics/:id(.:format)                      topics#show
                         PATCH  /topics/:id(.:format)                      topics#update
                         PUT    /topics/:id(.:format)                      topics#update
                         DELETE /topics/:id(.:format)                      topics#destroy
           post_comments POST   /posts/:post_id/comments(.:format)         comments#create
            post_comment DELETE /posts/:post_id/comments/:id(.:format)     comments#destroy
                   about GET    /about(.:format)                           welcome#about
                    root GET    /                                          welcome#index


<% @comments.each do |comment| %>

<div class="media">
  <div class="media-left">
    <%= image_tag(comment.user.avatar.small.url, class: "media-object") if comment.user.try(:avatar) %>
  <div class="media-body">
      <% %> commented <%= time_ago_in_words(comment.created_at) %> ago
      <% if policy(comment).destroy? %>
        | <%= link_to "Delete", [@topic, @post, comment], method: :delete %>
      <% end %>
    <p><%= comment.body %>
<% end %>

I specifically changed line 11 to:

| <%= link_to "Delete", [@post, comment], method: :delete %>


<%= form_for [topic, post, comment] do |f| %>
  <% if comment.errors.any? %>
    <div class="alert alert-danger">
      <h4>There are <%= pluralize(comment.errors.count, "error") %>.</h4>
        <% comment.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
    <% end %>

    <div class="form-inline">
    <%= form_group_tag(comment.errors[:body]) do %>
      <%= f.label :body %>
      <%= f.text_field :body, class: 'form-control'%>
    <% end %>

    <div class="form-group">
      <%= f.submit "Comment", class: 'btn btn-default' %>
  <% end %>

The first line I have changed to:

<%= form_for [post, comment] do |f| %>

Now I am able to view the project when I start my Rails server. I am not sure how I will go about this last step.

Derive @topic from @post because we still want redirect_to the @post page after creating or destroying a comment. Furthermore, @post is still nested under @topic.

Here is my comments_controller.rb

class CommentsController < ApplicationController
  def create
    # find topic by id
    # @topic = Topic.find(params[:topic_id])
    # find post id through topic
    @post = Post.find(params[:post_id])
    # comments on post
    @comments = @post.comments

    @comment =, :post_id)) = @post

    authorize @comment
      flash[:notice] = "Comment was created."
      redirect_to [@topic, @post]
      flash[:error] = "Error saving the comment. Please try again."
      # must render the the page calling the form!!
      render 'posts/show'

  def new


  def destroy
    @topic = Topic.find(params[:topic_id])
    @post = @topic.posts.find(params[:post_id])
    @comment = @post.comments.find(params[:id])

    authorize @comment
    if @comment.destroy
      flash[:notice] = "Comment was removed."
      redirect_to [@topic, @post]
      flash[:error] = "Comment couldn't be deleted. Try again."
      redirect_to [@topic, @post]

As you can see my route for showing posts is

topic_post GET    /topics/:topic_id/posts/:id(.:format)      posts#show

How am I supposed to do this when I am told to remove the @topic initialization? I have attempted to include it with my current modified code but I receive this error when attempting to delete a comment.

Processing by CommentsController#destroy as HTML
  Parameters: {"authenticity_token"=>"ocCSra0R/kcA+5MHVowZDNShghHhNUKYcO3yJaDuUKcZsRab90who4SuOK4MmS/4XXhycK0XZJ1UbS/n09aFEg==", "post_id"=>"2", "id"=>"7"}
  Topic Load (0.1ms)  SELECT  "topics".* FROM "topics" WHERE "topics"."id" = ? LIMIT 1  [["id", nil]]
Completed 404 Not Found in 7ms (ActiveRecord: 0.6ms)

Best regards.


Derive @topic from @post because we still want redirect_to the @post page after creating or destroying a comment. Furthermore, @post is still nested under @topic.

It's saying instead of loading the topic via a Topic.find, you get it from the post. The topic_id is no longer in the params, so you have to get it from the post. No big deal since every post has a topic, right? This must be true otherwise making your routes more shallow would not be possible.

Something like this:

def destroy
  @post = Post.find(params[:post_id])
  @topic = @post.topic
  # ... the rest I think is unchanged

You can completely remove the @topic if you really want to, and just do this in the redirects:

redirect_to [@post.topic, @post]


On rendering from controller, current_page method does not seem to work

I have a navigation bar included in application.html.erb. Because for some pages, such as the signup page, I need to place additional code inside the navigation bar, I have excluded those pages for showing the navigation bar through application.html.erb and instead included it in their respective view pages. See code...

Ruby boolean logic: some amount of variables are true

Let say I have 3 variables: a, b, c. How can I check that just zero or one of them is true?...

Keep leading zeroes when converting string to integer

For no particular reason, I am trying to add a #reverse method to the Integer class: class Integer def reverse self.to_s.reverse.to_i end end puts 1337.reverse # => 7331 puts 1000.reverse # => 1 This works fine except for numbers ending in a 0, as shown when 1000.reverse returns 1 rather...

Split an array into slices, with groupings

I've got some Ruby code here, that works, but I'm certain I'm not doing it as efficiently as I can. I have an Array of Objects, along this line: [ { name: "foo1", location: "new york" }, { name: "foo2", location: "new york" }, { name: "foo3", location: "new york"...

What is Rack::Utils.multipart_part_limit within Rails and what function does it perform?

Rack::Utils.multipart_part_limit is set to 128 by default. What purpose does the value have and what effect does it have within the Rails system?...

Get the actual value of a boolean attribute

I have the span: <span disabled="disabled">Edit Member</span> When I try to get the value of the disabled attribute: page.in_iframe(:id => 'MembersAreaFrame') do |frame| expect(page.span_element(:xpath => "//span[text()='Edit Member']", :frame => frame).attribute('disabled')).to eq("disabled") end I get: expected: "disabled" got: "true" How do I get the value of specified attribute instead of a...

Mongoid HABTM relationships across embedded documents

I'm trying to create a Mongoid N-N reference association between two embedded documents in Rails 4 however I'm finding it difficult to get my head round how this is done. I started by adding the HABTM association to the relevant models below (Track and Option) but of course I'm getting...

Heroku rake db:migrate failing - uninitialized constant

My app is working fine locally and my push to Heroku was successful. But, when I run heroku run rake db:migrate, I get the following error: NameError: uninitialized constant AddWeightToExercises Here is the failed migration: class AddWeightToExercise < ActiveRecord::Migration def change add_column :exercises, :weight, :float end end edit: Thanks for...

Active Record association links, but can't assign values

I'm setting up permissions for each user to determine what access they have to certain data. I have a user model and a permission model that is set up with a has_many and belongs_to association like so: app/models/permission.rb class Permission < ActiveRecord::Base has_many :users end app/models/user.rb class User < ActiveRecord::Base...

Ruby: How to copy the multidimensional array in new array?

seating_arrangement [ [:first, :second, :none], [:first, :none, :second], [:second, :second, :first], ] I need to copy this array into new array. I tried to do it by following code: class Simulator @@current_state def initialize(seating_arrangement) @@current_state = seating_arrangement.dup end But whenever I am making any changes to seating_arrangement current_state changes automatically....

Make instance variable accessible through hash in Ruby

In Rails, ActiveRecord objects, attributes are accessible via method as well as through Hash. Example: user = User.first # Assuming User to be inheriting from ActiveRecord::Base # Accessing attribute 'name' via method user[:name] # Attribute 'name' is accessible via hash as well How to make instance variables accessible through...

Rails shared controller actions

I am having trouble building a controller concern. I would like the concern to extend the classes available actions. Given I have the controller 'SamplesController' class SamplesController < ApplicationController include Searchable perform_search_on(Sample, handle: [ClothingType, Company, Collection, Color]) end I include the module 'Searchable' module Searchable extend ActiveSupport::Concern module ClassMethods def...

How to send a mail by postfix mail server with rails?

On my local host, I followed this article made a mail server and it works well. I want to develop a mailer feature with rails framework. Here are my settings: # mailer config.action_mailer.default_url_options = { :host => '' } config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: 'localhost', port: 25,...

Ruby access words in string

I don't understand the best method to access a certain word by it's number in a string. I tried using [] to access a word but instead it returns letter. puts s # => I went for a walk puts s[3] # => w ...

Can Rails deal with DB uniqueness without index?

I see add_index ~ unique: true statement in schema.rband think uniqueness is constraint for table, not for index.Using index is one of way realizing uniqueness, Programmer should not designate to the RDBMS "how" and index quicken searching but take costs inserting. In fact, is there another way to keep uniqueness...

For loop with flexible stop variable

I need to write a loop for x in (1..y) where the y variable can be changed somehow. How can I do that? For example: for x in (1..y/x) But it does not work. ...

Stack level too deep because recursion

I have a model named Tweet. The columns of the Tweet model are: -id -content -user_id -picture -group -original_tweet_id Every tweet can have one or multiple retweets. The relation happens with the help of original_tweet_id. All the tweets have original_tweet_id nil , whilst the retweets contain the id of the...

Showing and editing has_many objects in Rails

I'm trying to do something that I imagine to be very basic, but I'm very new to Rails and am not sure what sure what I'm doing wrong. I've gone through several tutorials and searched for an answer and can't find what the issue is. Would appreciate any help! I've...

Allowing some enabled and disabled option on collection_select

I am trying to populate a dropdown box on a view that has all the states. This works just fine: <%= f.collection_select :state_id, @states, :id, :name %> Now, I need to make the following: Some states are going to be disabled for choosing, but they still have to appear on...

Javascript not executing when using link_to

I have couple javascript functions that attaching action to buttons in controller view some/index. I navigate to some/index using link_to method. But when i'm using link_to method, buttons has no attached javascript actions (in some/index view). When i try open some/index directly from my browser, buttons have all attached actions...

Rails: Posting from a form to a M:M table

On the group index page I display a listing of group discussions and a form to create a new discussion. class Group < ActiveRecord::Base has_many :discussions end class Discussion < ActiveRecord::Base belongs_to :user belongs_to :group end Controller: class DiscussionsController < ApplicationController def index @group = Group.find(params[:group_id]) @discussion ={group_id:})...

Heroku RAM not increasing with upgraded dynos

I have a massive function i have been calling manually through the heroku rails console. I have been receiving the error rapid fire in my logs: 2015-06-22T14:56:42.940517+00:00 heroku[run.9877]: Process running mem=575M(112.4%) 2015-06-22T14:56:42.940517+00:00 heroku[run.9877]: Error R14 (Memory quota exceeded) A 1X dyno is suppose to have 512 MB of RAM. I...

Ruby- get a xml node value

can someone help me in extracting the node value for the element "Name". Type 1: I am able to extract the "name" value for the below xml by using the below code <Element> <Details> <ID>20367</ID> <Name>Ram</Name> <Name>Sam</Name> </Details> </Element> doc = Nokogiri::XML(response.body) values = doc.xpath('//Name').map{ |node| node.text}.join ',' puts values...

Ruby on Rails - Help Adding Badges to Application

I'm creating a rails application that is a backend for a mobile application. The backend is implemented with a RESTful web API. Currently I am trying to add gamification to the platform through the use of badges that can be earned by the user. Right now the badges are tied...

How to pass array in rails 4 strong parameters

I have to pass a array of food_item_ids in my order_controller. Every order will have many food_items. How can I pass these food_items_id as an array in strong parameters. orders_controller.rb def create @order = if render :json, @order, status:201, location: [:api, @order] else render :json, { errors: @order.errors...

Rails basic auth not working properly

I am building a small API that uses basic authentication. What I have done, is that a user can generate a username and password, that could be used to authenticate to the API. However I have discovered that it is not working 100% as intended. It appears that a request...

Get X days out of an Array

I have an array filled with Datetime objects: [Mon, 22 Jun 2015, Tue, 23 Jun 2015, Wed, 24 Jun 2015, Thu, 25 Jun 2015, Fri, 26 Jun 2015, Sat, 27 Jun 2015, Sun, 28 Jun 2015] I know how to select what I want from the array ex:{|x|x.monday? ||...

Is it possible to have two callbacks in around_destroy in rails?

Although this can be accomplished as follows: class Model < ActiveRecord::Base around_destroy :callback def callback puts 'callback1 before yield' puts 'callback2 before yield' yield puts 'callback1 after yield' puts 'callback2 after yield' end end But I wish to do the following: class Model < ActiveRecord::Base around_destroy :callback1, :callback2 def callback1...

Loop until i get correct user

I have users stored in Redis and want to be able to call only certain subsets from a set, if i don't get the correct user back i want to put it back in the set and then try again until i get one of the desired users @redis =...

If statement for search field in Rails

I have a blog site and recently added a search bar for visitors to browse through blog posts. When used, all of the posts go away in the directory and only those which return via the search appear. In order to show the general directory and view all, I would...

Rails Association Guidance [on hold]

I am new to rails 4. I have gone through lots of tutorials and trying to solve below scenario. But still no success. Can anybody point me in the right direction. How to handle associations for below scenario. Scenario: 1. Patient can have many surgeries. 2. Surgery has two types...

How to pivot array into another array in Ruby

I have a multidimensional array like this one : myArray = [["Alaska","Rain","3"],["Alaska","Snow","4"],["Alabama","Snow","2"],["Alabama","Hail","1"]] I would like to end up with CSV output like this. State,Snow,Rain,Hail Alaska,4,3,nil Alabama,2,nil,1 I know that to get this outputted to CSV the way I want it I have to have output array like this: outputArray =[["State","Snow","Rain","Hail"],["Alaska",4,3,nil],["Alabama",2,nil,1]]...

Same enum values for multiple columns

I need to do something like this: class PlanetEdge < ActiveRecord::Base enum :first_planet [ :earth, :mars, :jupiter] enum :second_planet [ :earth, :mars, :jupiter] end Where my table is a table of edges but each vertex is an integer. However, it seems the abvove is not possible in rails. What might...

How to find a record from database by using find_by with two fields?

I want to find a record from database in ruby on rails by using find_by with two fields. How can I accomplish it?

ActiveAdmin ::Show 4 textbox in active admin using has_many relationship

I have one poll table having with question filed and each question have 4 answer which is has_many relation ship with answer table class Poll < ActiveRecord::Base has_many :answer end My answer model class Answer < ActiveRecord::Base belongs_to :poll, :class_name => "Poll", :foreign_key => "question_id" end My active admin form...

Routes work in Development But not in Production

Weird Error. I have some routes that work perfectly during development but once i deploy and try to access them it comes up with page does not Exist error I have the following routes.rb file: TransportUnl::Application.routes.draw do resources :trucks resources :shipments do collection do get :autocomplete_location_cs end end devise_for :users...

RoR Tutorial Chapter 3 - Guard detects changes but doesn't fully execute tests

I'm currently following the book and the video and in the video, Hartl runs a guardfile so that it automatically runs tests after any changes have been made. So in the videos when he presses return whilst in guard mode, it says: 17:35:31 - INFO - Running: test/controllers/static_pages_controller_test.rb followed by...