Questions tagged [ruby-on-rails]

Ruby on Rails is an open-source web development framework written in Ruby. If your question code is written in Ruby on Rails, tag with Ruby as the language tag. Many things are defined by convention, freeing you from having to re-invent things to stay productive.

Filter by
Sorted by
Tagged with
28
votes
4answers
14k views

Unit-testing a controller in Ruby on Rails

I intend this to be a general question on writing an effective set of test cases for a controller action. I include the following ingredients: Ruby on Rails RSpec: A testing framework. I considered ...
25
votes
5answers
32k views

Use of a regex stored inside a YAML file

I installed settingslogic and in the configuration file I put the regex for the email as follows: ...
15
votes
4answers
14k views

Setting the default display name for my model

I have a method in my Ruby code that sets the default name for my model. Rubocop complains that Assignment Branch Condition Size is too high, 21.24/15. How can I improve this? ...
10
votes
2answers
6k views

Return render in Ruby on Rails

Is it bad to do return render? I am doing this to NOT also allow a redirect. ...
9
votes
3answers
2k views

Storing a Ruby app's settings

I have used code which looks a little like this in an app I'm working on: ...
9
votes
2answers
4k views

Rails controller method that conditionally filters results

This code for my blog checks to see if tags are in the params hash. If they are, then only posts that are tagged will be paginated. Otherwise, all of the posts are paginated. ...
9
votes
1answer
84 views

Summing the size/count of associations for multiple records

I'm working on a plain Ruby class within a Rails application. For the total_leg_count method---is that the best way to go about achieving what I want? I feel like ...
8
votes
2answers
3k views

Using I18n in capybara feature specs

Here I am trying to avoid hardcoding in flash messages by using I18n. Also, I have tried to use a CSS selector for submitting the form to make the tests less brittle (I don't really care that much ...
8
votes
2answers
5k views

Handling nil: Trying to avoid #try

Yesterday, I introduced a bug into our codebase by calling #titleize on a string that could possibly come in as nil: ...
8
votes
2answers
201 views

Error-testing be with cucumber/capybaraweb

I'm working on writing tests for a rails application using cucumber and capybara. I have a scenario for a user editing a post, and making it invalid. The scenario looks like this: ...
8
votes
1answer
23k views

Filtering (search) a model with multiple params in hash

This function in my Profile model receive a hash from a search form and should filter the table depending on those values. I guess there is something like a pattern ...
8
votes
1answer
10k views

My first controller rspec test

I'm just started learning TDD with Rails and RSpec. This is my first test for controller. Just plain RESTful UserController that responds with JSON, so it has no new and edit methods. Please review it ...
8
votes
1answer
2k views

Refactoring contest: Rails accepts_nested_attributes_for a many-to-many associated model

I found myself struggling when it came to refactor the following (working) code. Since can't be found a lot of documentation about Rails 'accepts_nested_attributes_for' applied to a many-to-many ...
8
votes
3answers
3k views

Linking issues, classifying the relationship as a cause, effect, superset, etc

I first saw this gigantic if and tried to refactor it. Could only end with a endless switch statement. Old code - ...
8
votes
1answer
829 views

Avoiding sub-queries in named scopes

I use named scopes all over the place. When I have a named scope that deals with two different tables, I tend to query the second table in a sub-query. That way, if I mix and match with other tables ...
8
votes
1answer
244 views

RubyGem for Enums

A little background first. I've been working on a fairly large Rails application which quickly grew into a breeding ground for "smelly" code. One antipattern we were using a lot was storing objects ...
8
votes
1answer
1k views

Object cache storage for Rails

I want to have fast cache in which I want to keep all my nomenclature data. I don't want to go with Memcached because I have to do serialize/de-serialize on each object which is slow. So I choose to ...
8
votes
1answer
99 views

Adding product with attributes

I've been working on refactoring a piece of my code that allows a user to add items to their cart. Each product can also be added with different attributes. Thus if somebody adds the same product, ...
8
votes
1answer
1k views

Dynamic Controller Creation in Rails

I have overrode Rails' ActionDispatch::Routing::RouteSet::Dispatcher.controller_reference method to check if a controller exists by checking for the required ...
7
votes
1answer
404 views

using method missing to reduce the code in ruby

I have a set of Class methods which is : ...
7
votes
4answers
3k views

Avoid printing attributes of the nil object in Rails

Currently, when I'm wanting to print a value for my class I do the following... <%= @person.team.name if @person.team.present? %> This seems really ...
7
votes
1answer
12k views

Faster way to update/create of a large amount of records?

In our Rails 3.2.13 app (Ruby 2.0.0 + Postgres on Heroku), we are often retreiving a large amount of Order data from an API, and then we need to update or create each order in our database, as well as ...
7
votes
1answer
353 views

Refactor Helper methods

I have two helper methods that return li for view with the li for current view having id as selected. ...
7
votes
1answer
104 views

Controller action to process orders and payments

I'm trying to reduce the if statements and simplify this create action. ...
7
votes
1answer
1k views

ActiveRecord extension to find next / previous record(s) in one query

I just wrote this gem order_query to find next or previous records relative to the record assuming a (possibly complex) ORDER BY over the records. For example, this ...
7
votes
2answers
897 views

Shorter, better way to write a “query” involving virtual attributes

I have this code in a model for an invoicing app. This virtual attribute current_invoice gets the only non-rescinded invoice associated with a contract. The code ...
7
votes
1answer
2k views

Developing a multistep form

I implemented a multistep form with the method described by Ryan Bates in ep217 but I had some weird behavior when refreshing or moving between the steps Acts_as_good_style (old but still good) has a ...
7
votes
1answer
2k views

Dynamic association generation in ActiveRecord

I'm developing a Rails-based web interface/API for a large, vendor-supplied, legacy Oracle database. One of the constraints is that I can't change the schema other than adding views, because it still ...
7
votes
1answer
454 views

Email notifications system in Rails project

I'm working on the Rails project where I have many-to-many :through relationship. Each setting has many notifications through email_notification model where the notification can be primary or a ...
7
votes
2answers
227 views

Script to redo a rails migration easily

I need to redo a migration occasionally. The workflow is usually something like this: find or ls | grep the migration using ...
7
votes
0answers
5k views

spec_helper.rb for RSpec, Capybara, FactoryGirl, Spork and Devise

Having been through hundreds of often-conflicting posts on testing with RSpec, Capybara, FactoryGirl, Spork, Guard and Devise without Cucumber, this is the spec_helper which "seems to" work. I'm ...
6
votes
5answers
458 views

Search for products based on criteria

The method must search Products found by criteria and store them in @products. The criteria are as follows: If the user enters ...
6
votes
2answers
687 views

Rails view to list users in a zebra-striped table

...
6
votes
2answers
137 views

Nested trial and error in if-else condition

Using Rails 3.2, I have the following: ...
6
votes
2answers
413 views

Find associated object if exists, otherwise generate it

I have a simplified model that looks like this: ...
6
votes
2answers
156 views

Controller for classrooms and students

The actions I'm wondering about are members, add_student, and remove_student. They work ...
6
votes
2answers
104 views

How to avoid mounting SQL by concatenating strings?

I found myself mounting a query by concatenating strings. This just seems wrong. Any ideas of how to avoid this kind of situation? I'm using Rails 4, and I don't see how can ...
6
votes
2answers
699 views

Refactor highlight matched word in string

I have following method which highlight matched word in text: ...
6
votes
2answers
3k views

Rails validating API parameters

I wanted to keep my API request logic separate form the controller logic. I therefore make use of a separate model (EmailChecker) that creates an instance of this ...
6
votes
3answers
176 views

A method that calculates win streaks

Right now, I have the following: A win_streak column on the User table. This method in ...
6
votes
2answers
164 views

Return input as a partial fraction

I need to write a function that, when given an input (string, float, or int), returns that input as a reduced partial fraction, and it needs to accept a wide array of inputs: ...
6
votes
1answer
3k views

Mapping arrays to ranges in Ruby

I have a simple Rails app, which is used to run some clinical surveys. Participants answer sets of questions (multiple-choice, valued 1-5), and, within each set, the answers are summed up and the ...
6
votes
1answer
419 views

How and where to define a partial helper module

I want to split my helpers in different modules (and files), but I found some problems which I got around with the following solution: ...
6
votes
1answer
342 views

Simple authorization module with Rails

I created a simple Authorization module with Rails. I found that there are other authorization systems, such as CanCanCan, but they grant permissions at Model level and, for this particular website I ...
6
votes
2answers
752 views

Parsing Day Range with Time in Ruby

I need to shorten/simplify this code: ...
6
votes
1answer
3k views

Using Rails concerns for validation in models

This is my first Rails project. I have read about concerns which are a great tool, but I am confused about how I should redesign my models. Some models have similar fields, but the validations are ...
6
votes
1answer
549 views

React.js components for a golf app

I've recently started to look into React. I feel like I'm understanding it, but I want to make sure I'm doing the best practices before I make too many bad habits. I'm using the ...
6
votes
1answer
10k views

Best way to include image_tag inside link_to

What is the most elegant and readable way to in include (potentially long) image_tag calls inside of link_to? Example ...
6
votes
1answer
396 views

How to manage different views

I answered a question in stackoverflow but I gave a solution that looks a little dirty to me, I'd like your opinion because I think there is a much smarter way to do it. A guest user should be able ...
6
votes
1answer
117 views

Seeding a database

I'm seeding my database with a relatively complex data model. Constraints: Every meal has exactly one main dish (marked as is_main in intermediate table). Here is ...

1
2 3 4 5
12