Monday, January 9, 2017

How to create a correct association | Fixed issues

Newest questions tagged ruby-on-rails - Stack Overflow

How to create a correct association | Fixed issues


How to create a correct association

Posted: 09 Jan 2017 08:13 AM PST

User can have a lot of microposts. A micropost can have one user.

class User    has_many :microposts, dependent: :destroy  end    class Micropost    belongs_to :user  end  

How to add an association fulfills the conditions - user can write a lot of messages to other users?

Optimizing Rails routing definition

Posted: 09 Jan 2017 07:17 AM PST

We have about 650 routes defined in our Rails 3.2.22 app. Around 20 of them make up over 90% of our requests. These 20 all fall under an optional scope: (/:api_version). Where is the most optimal place to define these routes.

Is it at the beginning of the definition tree or the end? In other words, should they be defined by popularity in ascending or descending order?

rails admin uninitialized constant User::Like

Posted: 09 Jan 2017 06:48 AM PST

I have added the gem rails_admin. It works well at first but whenever I want to edit or look up something this problem occurs.

I tried to remove the line but it could not find where is it located in the app. I read that many ran into a similar problem to mine but nothing worked for me! Also, since my app is in a different language, for some reason I thought that could be a reason why it is not functioning.

Here what I get:

uninitialized constant User::Like      Extracted source (around line #158):                       end                  raise NameError.new("uninitialized constant #{candidates.first}", candidates.first)              end            end  

Here's a full trace:

Trace of template inclusion: /usr/local/rvm/gems/ruby-2.3.0/gems/rails_admin-1.1.1/app/views/rails_admin/main/new.html.haml    Rails.root: /home/ubuntu/workspace    Application Trace | Framework Trace | Full Trace  activerecord (4.2.5) lib/active_record/inheritance.rb:158:in `compute_type'  activerecord (4.2.5) lib/active_record/reflection.rb:295:in `compute_class'  activerecord (4.2.5) lib/active_record/reflection.rb:291:in `klass'  activerecord (4.2.5) lib/active_record/reflection.rb:671:in `source_reflection'  activerecord (4.2.5) lib/active_record/reflection.rb:640:in `foreign_key'  rails_admin (1.1.1) lib/rails_admin/adapters/active_record/association.rb:37:in `foreign_key'  rails_admin (1.1.1) lib/rails_admin/config/fields/factories/association.rb:6:in `block (2 levels) in <top (required)>'  rails_admin (1.1.1) lib/rails_admin/config/fields/factories/association.rb:6:in `each'  rails_admin (1.1.1) lib/rails_admin/config/fields/factories/association.rb:6:in `detect'  rails_admin (1.1.1) lib/rails_admin/config/fields/factories/association.rb:6:in `block in <top (required)>'  rails_admin (1.1.1) lib/rails_admin/config/fields.rb:54:in `block (2 levels) in factory'  rails_admin (1.1.1) lib/rails_admin/config/fields.rb:54:in `each'  rails_admin (1.1.1) lib/rails_admin/config/fields.rb:54:in `detect'  rails_admin (1.1.1) lib/rails_admin/config/fields.rb:54:in `block in factory'  rails_admin (1.1.1) lib/rails_admin/config/fields.rb:50:in `each'  rails_admin (1.1.1) lib/rails_admin/config/fields.rb:50:in `factory'  rails_admin (1.1.1) lib/rails_admin/config/has_fields.rb:130:in `_fields'  rails_admin (1.1.1) lib/rails_admin/config/has_fields.rb:133:in `_fields'  rails_admin (1.1.1) lib/rails_admin/config/has_fields.rb:108:in `all_fields'  rails_admin (1.1.1) lib/rails_admin/config/has_fields.rb:83:in `fields'  rails_admin (1.1.1) app/controllers/rails_admin/main_controller.rb:123:in `get_collection'  rails_admin (1.1.1) app/controllers/rails_admin/main_controller.rb:37:in `list_entries'  rails_admin (1.1.1) app/views/rails_admin/main/_form_filtering_select.html.haml:20:in `__usr_local_rvm_gems_ruby_______gems_rails_admin_______app_views_rails_admin_main__form_filtering_select_html_haml__2345908052801766424_70236212464320'  actionview (4.2.5) lib/action_view/template.rb:145:in `block in render'  activesupport (4.2.5) lib/active_support/notifications.rb:166:in `instrument'  actionview (4.2.5) lib/action_view/template.rb:333:in `instrument'  actionview (4.2.5) lib/action_view/template.rb:143:in `render'  actionview (4.2.5) lib/action_view/renderer/partial_renderer.rb:339:in `render_partial'  actionview (4.2.5) lib/action_view/renderer/partial_renderer.rb:310:in `block in render'  actionview (4.2.5) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'  activesupport (4.2.5) lib/active_support/notifications.rb:164:in `block in instrument'  activesupport (4.2.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'  activesupport (4.2.5) lib/active_support/notifications.rb:164:in `instrument'  actionview (4.2.5) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'  actionview (4.2.5) lib/action_view/renderer/partial_renderer.rb:309:in `render'  actionview (4.2.5) lib/action_view/renderer/renderer.rb:47:in `render_partial'  actionview (4.2.5) lib/action_view/renderer/renderer.rb:21:in `render'  actionview (4.2.5) lib/action_view/helpers/rendering_helper.rb:32:in `render'  haml (4.0.7) lib/haml/helpers/action_view_mods.rb:10:in `block in render_with_haml'  haml (4.0.7) lib/haml/helpers.rb:89:in `non_haml'  haml (4.0.7) lib/haml/helpers/action_view_mods.rb:10:in `render_with_haml'  rails_admin (1.1.1) lib/rails_admin/config/fields/base.rb:227:in `block in <class:Base>'  rails_admin (1.1.1) lib/rails_admin/config/configurable.rb:66:in `instance_eval'  rails_admin (1.1.1) lib/rails_admin/config/configurable.rb:66:in `block in register_instance_option'  rails_admin (1.1.1) lib/rails_admin/config/proxyable/proxy.rb:27:in `method_missing'  rails_admin (1.1.1) app/helpers/rails_admin/form_builder.rb:73:in `field_for'  rails_admin (1.1.1) app/helpers/rails_admin/form_builder.rb:58:in `block in input_for'  actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'  actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:202:in `with_output_buffer'  haml (4.0.7) lib/haml/helpers/action_view_xss_mods.rb:5:in `with_output_buffer_with_haml_xss'  actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:38:in `capture'  haml (4.0.7) lib/haml/helpers/action_view_mods.rb:52:in `capture_with_haml'  actionview (4.2.5) lib/action_view/helpers/tag_helper.rb:106:in `content_tag'  haml (4.0.7) lib/haml/helpers/action_view_mods.rb:69:in `content_tag_with_haml'  rails_admin (1.1.1) app/helpers/rails_admin/form_builder.rb:57:in `input_for'  rails_admin (1.1.1) app/helpers/rails_admin/form_builder.rb:46:in `block in field_wrapper_for'  actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'  actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:202:in `with_output_buffer'  haml (4.0.7) lib/haml/helpers/action_view_xss_mods.rb:5:in `with_output_buffer_with_haml_xss'  actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:38:in `capture'  haml (4.0.7) lib/haml/helpers/action_view_mods.rb:52:in `capture_with_haml'  actionview (4.2.5) lib/action_view/helpers/tag_helper.rb:106:in `content_tag'  haml (4.0.7) lib/haml/helpers/action_view_mods.rb:69:in `content_tag_with_haml'  rails_admin (1.1.1) app/helpers/rails_admin/form_builder.rb:44:in `field_wrapper_for'  rails_admin (1.1.1) app/helpers/rails_admin/form_builder.rb:35:in `block (2 levels) in fieldset_for'  rails_admin (1.1.1) app/helpers/rails_admin/form_builder.rb:35:in `collect'  rails_admin (1.1.1) app/helpers/rails_admin/form_builder.rb:35:in `block in fieldset_for'  actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'  actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:202:in `with_output_buffer'  haml (4.0.7) lib/haml/helpers/action_view_xss_mods.rb:5:in `with_output_buffer_with_haml_xss'  actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:38:in `capture'  haml (4.0.7) lib/haml/helpers/action_view_mods.rb:52:in `capture_with_haml'  actionview (4.2.5) lib/action_view/helpers/tag_helper.rb:106:in `content_tag'  haml (4.0.7) lib/haml/helpers/action_view_mods.rb:69:in `content_tag_with_haml'  rails_admin (1.1.1) app/helpers/rails_admin/form_builder.rb:31:in `fieldset_for'  rails_admin (1.1.1) app/helpers/rails_admin/form_builder.rb:16:in `block (2 levels) in generate'  rails_admin (1.1.1) app/helpers/rails_admin/form_builder.rb:15:in `collect'  rails_admin (1.1.1) app/helpers/rails_admin/form_builder.rb:15:in `block in generate'  rails_admin (1.1.1) app/helpers/rails_admin/form_builder.rb:131:in `without_field_error_proc_added_div'  rails_admin (1.1.1) app/helpers/rails_admin/form_builder.rb:7:in `generate'  rails_admin (1.1.1) app/views/rails_admin/main/new.html.haml:2:in `block in __usr_local_rvm_gems_ruby_______gems_rails_admin_______app_views_rails_admin_main_new_html_haml__2436263065298238128_70236469416200'  haml (4.0.7) lib/haml/helpers/action_view_mods.rb:137:in `block (2 levels) in form_for_with_haml'  haml (4.0.7) lib/haml/helpers.rb:278:in `with_tabs'  haml (4.0.7) lib/haml/helpers/action_view_mods.rb:137:in `block in form_for_with_haml'  actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'  actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:202:in `with_output_buffer'  haml (4.0.7) lib/haml/helpers/action_view_xss_mods.rb:5:in `with_output_buffer_with_haml_xss'  actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:38:in `capture'  haml (4.0.7) lib/haml/helpers/action_view_mods.rb:52:in `capture_with_haml'  actionview (4.2.5) lib/action_view/helpers/form_helper.rb:444:in `form_for'  haml (4.0.7) lib/haml/helpers/action_view_mods.rb:139:in `form_for_with_haml'  haml (4.0.7) lib/haml/helpers/action_view_xss_mods.rb:28:in `form_for_with_haml_xss'  rails_admin (1.1.1) app/helpers/rails_admin/main_helper.rb:9:in `rails_admin_form_for'  rails_admin (1.1.1) app/views/rails_admin/main/new.html.haml:1:in `__usr_local_rvm_gems_ruby_______gems_rails_admin_______app_views_rails_admin_main_new_html_haml__2436263065298238128_70236469416200'  actionview (4.2.5) lib/action_view/template.rb:145:in `block in render'  activesupport (4.2.5) lib/active_support/notifications.rb:166:in `instrument'  actionview (4.2.5) lib/action_view/template.rb:333:in `instrument'  actionview (4.2.5) lib/action_view/template.rb:143:in `render'  actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'  actionview (4.2.5) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'  activesupport (4.2.5) lib/active_support/notifications.rb:164:in `block in instrument'  activesupport (4.2.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'  activesupport (4.2.5) lib/active_support/notifications.rb:164:in `instrument'  actionview (4.2.5) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'  actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'  actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'  actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:52:in `render_template'  actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:14:in `render'  actionview (4.2.5) lib/action_view/renderer/renderer.rb:42:in `render_template'  actionview (4.2.5) lib/action_view/renderer/renderer.rb:23:in `render'  actionview (4.2.5) lib/action_view/rendering.rb:100:in `_render_template'  actionpack (4.2.5) lib/action_controller/metal/streaming.rb:217:in `_render_template'  actionview (4.2.5) lib/action_view/rendering.rb:83:in `render_to_body'  actionpack (4.2.5) lib/action_controller/metal/rendering.rb:32:in `render_to_body'  actionpack (4.2.5) lib/action_controller/metal/renderers.rb:37:in `render_to_body'  actionpack (4.2.5) lib/abstract_controller/rendering.rb:25:in `render'  actionpack (4.2.5) lib/action_controller/metal/rendering.rb:16:in `render'  actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'  activesupport (4.2.5) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'  /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/benchmark.rb:308:in `realtime'  activesupport (4.2.5) lib/active_support/core_ext/benchmark.rb:12:in `ms'  actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:44:in `block in render'  actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'  activerecord (4.2.5) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'  actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:43:in `render'  remotipart (1.3.1) lib/remotipart/render_overrides.rb:16:in `render_with_remotipart'  rails_admin (1.1.1) lib/rails_admin/config/actions/new.rb:28:in `block (4 levels) in <class:New>'  actionpack (4.2.5) lib/action_controller/metal/mime_responds.rb:217:in `respond_to'  rails_admin (1.1.1) lib/rails_admin/config/actions/new.rb:27:in `block (2 levels) in <class:New>'  rails_admin (1.1.1) app/controllers/rails_admin/main_controller.rb:22:in `instance_eval'  rails_admin (1.1.1) app/controllers/rails_admin/main_controller.rb:22:in `new'  actionpack (4.2.5) lib/action_controller/metal/implicit_render.rb:4:in `send_action'  actionpack (4.2.5) lib/abstract_controller/base.rb:198:in `process_action'  actionpack (4.2.5) lib/action_controller/metal/rendering.rb:10:in `process_action'  actionpack (4.2.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'  activesupport (4.2.5) lib/active_support/callbacks.rb:117:in `call'  activesupport (4.2.5) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'  activesupport (4.2.5) lib/active_support/callbacks.rb:505:in `call'  activesupport (4.2.5) lib/active_support/callbacks.rb:92:in `__run_callbacks__'  activesupport (4.2.5) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'  activesupport (4.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'  actionpack (4.2.5) lib/abstract_controller/callbacks.rb:19:in `process_action'  actionpack (4.2.5) lib/action_controller/metal/rescue.rb:29:in `process_action'  actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'  activesupport (4.2.5) lib/active_support/notifications.rb:164:in `block in instrument'  activesupport (4.2.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'  activesupport (4.2.5) lib/active_support/notifications.rb:164:in `instrument'  actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'  actionpack (4.2.5) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'  activerecord (4.2.5) lib/active_record/railties/controller_runtime.rb:18:in `process_action'  actionpack (4.2.5) lib/abstract_controller/base.rb:137:in `process'  actionview (4.2.5) lib/action_view/rendering.rb:30:in `process'  actionpack (4.2.5) lib/action_controller/metal.rb:196:in `dispatch'  actionpack (4.2.5) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'  actionpack (4.2.5) lib/action_controller/metal.rb:237:in `block in action'  actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:76:in `dispatch'  actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:45:in `serve'  actionpack (4.2.5) lib/action_dispatch/journey/router.rb:43:in `block in serve'  actionpack (4.2.5) lib/action_dispatch/journey/router.rb:30:in `each'  actionpack (4.2.5) lib/action_dispatch/journey/router.rb:30:in `serve'  actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:817:in `call'  railties (4.2.5) lib/rails/engine.rb:518:in `call'  railties (4.2.5) lib/rails/railtie.rb:194:in `public_send'  railties (4.2.5) lib/rails/railtie.rb:194:in `method_missing'  actionpack (4.2.5) lib/action_dispatch/routing/mapper.rb:51:in `serve'  actionpack (4.2.5) lib/action_dispatch/journey/router.rb:43:in `block in serve'  actionpack (4.2.5) lib/action_dispatch/journey/router.rb:30:in `each'  actionpack (4.2.5) lib/action_dispatch/journey/router.rb:30:in `serve'  actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:817:in `call'  rack-pjax (1.0.0) lib/rack/pjax.rb:12:in `call'  actionpack (4.2.5) lib/action_dispatch/middleware/flash.rb:260:in `call'  warden (1.2.6) lib/warden/manager.rb:35:in `block in call'  warden (1.2.6) lib/warden/manager.rb:34:in `catch'  warden (1.2.6) lib/warden/manager.rb:34:in `call'  rack (1.6.4) lib/rack/etag.rb:24:in `call'  rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'  rack (1.6.4) lib/rack/head.rb:13:in `call'  remotipart (1.3.1) lib/remotipart/middleware.rb:32:in `call'  actionpack (4.2.5) lib/action_dispatch/middleware/params_parser.rb:27:in `call'  actionpack (4.2.5) lib/action_dispatch/middleware/flash.rb:260:in `call'  rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'  rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'  actionpack (4.2.5) lib/action_dispatch/middleware/cookies.rb:560:in `call'  activerecord (4.2.5) lib/active_record/query_cache.rb:36:in `call'  activerecord (4.2.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'  activerecord (4.2.5) lib/active_record/migration.rb:377:in `call'  actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'  activesupport (4.2.5) lib/active_support/callbacks.rb:88:in `__run_callbacks__'  activesupport (4.2.5) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'  activesupport (4.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'  actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'  actionpack (4.2.5) lib/action_dispatch/middleware/reloader.rb:73:in `call'  actionpack (4.2.5) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'  actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'  web-console (2.3.0) lib/web_console/middleware.rb:20:in `block in call'  web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'  web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'  actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'  railties (4.2.5) lib/rails/rack/logger.rb:38:in `call_app'  railties (4.2.5) lib/rails/rack/logger.rb:20:in `block in call'  activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'  activesupport (4.2.5) lib/active_support/tagged_logging.rb:26:in `tagged'  activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `tagged'  railties (4.2.5) lib/rails/rack/logger.rb:20:in `call'  actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'  rack (1.6.4) lib/rack/runtime.rb:18:in `call'  activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'  rack (1.6.4) lib/rack/lock.rb:17:in `call'  actionpack (4.2.5) lib/action_dispatch/middleware/static.rb:116:in `call'  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'  railties (4.2.5) lib/rails/engine.rb:518:in `call'  railties (4.2.5) lib/rails/application.rb:165:in `call'  rack (1.6.4) lib/rack/lock.rb:17:in `call'  rack (1.6.4) lib/rack/content_length.rb:15:in `call'  rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'  /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'  /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'  /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'`  

Please help-out, thanks in advance!

How to create objects using rails polymorphic association

Posted: 09 Jan 2017 06:53 AM PST

I want to create objects using Polymorphic association but it returns me error like:

NoMethodError (undefined method `new' for nil:NilClass):  

Here are params:

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"TAQdqSTXAFCrq6yZBUnjVXe7IsPqmxuLh/qM/2QvKaVvsfP  /Sy6VNF7H38IzOg8aMj39t6HR+aZLLtyj8uMzyw==", "search"=>"Newport-Mesa Unified   School District", "id"=>"32", "type"=>"PrivateSchool", "commit"=>"Submit"}  

Here are models:

class PrivateSchool < ActiveRecord::Base    has_many :popular_schools, as: :resource, :dependent => :destroy  end    class PopularSchool < ActiveRecord::Base    belongs_to :resource, polymorphic: true  end    class School < ActiveRecord::Base    has_many :popular_schools, as: :resource, :dependent => :destroy  end  

Controller code:

def index      @popular_school = PopularSchool.new      @popular_schools = PopularSchool.all    end      def create      @popular_school.new.save      respond_to do |format|        format.html { redirect_to root_path }        format.json { head :no_content }        format.js      end    end  

Rails undefined method * for nil:NilClass

Posted: 09 Jan 2017 06:46 AM PST

I am having an issue and I have done some reasearch, from my research I have found that the variable that is being used is empty, however I am unsure as to why it is empty, Maybe its something obvious to someone else?

I am trying to display a nested form on a page from another controller, I am used nested resources, Which I think might be my issue, but unsure how to resolve it.

Getting the following error:

undefined method `submission' for nil:NilClass  

Structure of Project Main Folders -Members --Questions --Submissions

Concept:

Question - has_many - Submissions Submission - Belongs_to - Question

Submission Model:

class Submission < ActiveRecord::Base      belongs_to :question      belongs_to :member  end  

Question Model:

class Members::Question < ActiveRecord::Base    has_many :submissions  end  

Submissions Controller:

def create            @question =  Members::Question.find(params[:question_id])          @submission.member_id =  current_member.id          @submission = @question.submissions.create(params[:submission].permit(:content, :question_id))    *Redirect Method Here *     end  

In the form I am using the following method:

<%= form_for([@question, @question.submission.build]) do |f| %>    <% if @submission.errors.any? %>      <div id="error_explanation">        <h2><%= pluralize(@submission.errors.count, "error") %> prohibited this submission from being saved:</h2>          <ul>        <% @submission.errors.full_messages.each do |message| %>          <li><%= message %></li>        <% end %>        </ul>      </div>    <% end %>      <div class="field">      <%= f.label :content %><br>      <%= f.text_area :content %>    </div>      <div class="actions">      <%= f.submit %>    </div>  <% end %>  

And then to display the form on the show page of the question, I am using

<%= render 'members/submissions/form' %>  

Routes:

  namespace :members do      resources :questions,  only: [:index,:show] do        resources :submissions       end    end  

Any one any ideas where I am going wrong?

Any help is appreciated.

Interactive Ruby error return Method Name in error

Posted: 09 Jan 2017 06:11 AM PST

The command line is returning an error with the method name as soon as enter is pressed after typing 'end'

Error

enter image description here

Ruby on rails display many records in a table in one html page

Posted: 09 Jan 2017 06:03 AM PST

I am now working on a job-listing project with ruby on rails. I have create a model called job which records the job on the list and a controller called jobs to control the actions. And each job belongs to a category, such as IT, Finance etc. I want to add a button for the user to select, for example the jobs of IT field. I have tried

rails g controller it/jobs  

In the it_jobs_controller

def index     @jobs = Job.where(:field => "IT")  end  

The above works well. I have also tried, in jobs controller, to add a method

def get_it      @jobs = Job.where(:field => "IT")  end  

And in config/routes

resources :jobs do      member do          get :get_it      end  end  

But it seems that I need to add params[:id] to link to that page, but that is not what I want. Could anyone tell me how to add a page like index page, but not to create a new controller.

Thank you very much!

Pass javascript local variable into rails tag

Posted: 09 Jan 2017 05:18 AM PST

I want pass the javascript variable into rails tag. Here my code

function add_questionfield(obj)  {    var last_id = $('input[type="text"]:last').size();    last_id++;    alert(last_id);    $(".question_div_<%= @subjectModuleId %>").show();    $(".question_div_<%= @subjectModuleId %>").append('<%=text_field_tag "question_'+ last_id +'",@question.question, name: 'questions[]',class:'form-control', placeholder:'Type Question Here',                                                   :maxlength => 200,:autocomplete => :off %>');  }  

Here I pass last_id into rails tag.

Expected id was question_2

How to solve this. Thank you But its generate question_last_id

Rspec tests of rack_cache

Posted: 09 Jan 2017 05:15 AM PST

I am using rack_cache to cache some of the responses withing Redis. I would like to test with rspec so I would be sure that caching works. I can do this with:

config.action_dispatch.rack_cache = true  

The problem is because I would like to set this setting on single test, not for whole app.

For high level caching we can use around bock:

config.around(:each, :caching) do |example|    caching = ActionController::Base.perform_caching    ActionController::Base.perform_caching = example.metadata[:caching]    example.run    ActionController::Base.perform_caching = caching  end  

Is there a way to do something similar for Rack cache?

Data Conversion in Ruby

Posted: 09 Jan 2017 06:26 AM PST

I am having issue while doing these conversions:

string = "test \\ud83d\\ude01" #into '1f601' and vise versa.    unicode_value = 'U+1F601' #into string '\\ud83d\\ude01'  

I have tried this method to encode

string.encode('utf-8') #output is "test \\ud83d\\ude01"  

Also tried this one

string.force_encoding('utf-8')  #output is "test \\ud83d\\ude01"  

Thanks

retrieve multiple columns within collection using simple form and pluck

Posted: 09 Jan 2017 06:15 AM PST

The idea is to display a collection of records with checkboxes, but also I would like to retrieve multiple columns for each record. I know the pluck method can retrieve multiple columns, however, simple form does not display them. So my code looks like:

<%= f.association :option, collection: @question.option.pluck(:lead, :description), as: :check_boxes %>  

But in my view, only the :lead column is displayed?

Ruby on Rails - Plesk - Blog - Error Db: migrate [on hold]

Posted: 09 Jan 2017 04:34 AM PST

Good Morning

I'm testing the features of Plesk Onyx

I'm testing Ruby

I followed the step by step tutorial of plesk itself in the url Https://www.plesk.com/extensions/ruby/

When I come across the step: Run rake task db: migrate

I get the error:

  DEPRECATION WARNING: The configuration option `config.serve_static_assets` has been renamed to `config.serve_static_files` to clarify its role (it merely enables serving everything in the `public` folder and is unrelated to the asset pipeline). The `serve_static_assets` alias will be removed in Rails 5.0. Please migrate your configuration files accordingly. (called from block in at /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/config/environments/production.rb:12)       DEPRECATION WARNING: You did not specify a `log_level` in `production.rb`. Currently, the default value for `log_level` is `:info` for the production environment and `:debug` in all other environments. In Rails 5 the default value will be unified to `:debug` across all environments. To preserve the current setting, add the following line to your `production.rb`:        config.log_level = :info        . (called from block in tsort_each at /opt/plesk/ruby/2.3.0/lib64/ruby/2.3.0/tsort.rb:228)       rake aborted!       Generate a secret with `rake secret` and paste it into `config/initializers/secret_token.rb`.       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/config/initializers/secret_token.rb:10:in `'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `load'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `block in load'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `load'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/railties-4.2.4/lib/rails/engine.rb:652:in `block in load_config_initializer'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.4/lib/active_support/notifications.rb:166:in `instrument'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/railties-4.2.4/lib/rails/engine.rb:651:in `load_config_initializer'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/railties-4.2.4/lib/rails/engine.rb:616:in `block (2 levels) in '       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/railties-4.2.4/lib/rails/engine.rb:615:in `each'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/railties-4.2.4/lib/rails/engine.rb:615:in `block in '       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/railties-4.2.4/lib/rails/initializable.rb:30:in `instance_exec'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/railties-4.2.4/lib/rails/initializable.rb:30:in `run'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/railties-4.2.4/lib/rails/initializable.rb:55:in `block in run_initializers'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/railties-4.2.4/lib/rails/initializable.rb:44:in `each'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/railties-4.2.4/lib/rails/initializable.rb:44:in `tsort_each_child'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/railties-4.2.4/lib/rails/initializable.rb:54:in `run_initializers'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/railties-4.2.4/lib/rails/application.rb:352:in `initialize!'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/railties-4.2.4/lib/rails/railtie.rb:194:in `public_send'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/railties-4.2.4/lib/rails/railtie.rb:194:in `method_missing'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/config/environment.rb:11:in `'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `block in require'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/railties-4.2.4/lib/rails/application.rb:328:in `require_environment!'       /var/www/vhosts/sailorweb.com.br/rails.sailorweb.com.br/vendor/bundle/ruby/2.3.0/gems/railties-4.2.4/lib/rails/application.rb:457:in `block in run_tasks_blocks'       Tasks: TOP => db:migrate => environment       (See full trace by running task with --trace)       ===============  

I contacted Xavier himself, the script author who informed me that he did not know what could be wrong, should work without problems

[Https://github.com/xaviershay/enki][2]    They could try to help me and understand what's wrong. So the tutorial itself can be redone  

Site URL http://rails.sailorweb.com.br/

How to migrate string data type to hstore

Posted: 09 Jan 2017 05:33 AM PST

I am upgrading my rails 3 application to rails 4. And according to the guide I removed this line from my model

serialize :my_serialized_column, ActiveRecord::Coders::Hstore.new({})  

but now I am getting the error while looping over :my_serialized_column which was previously hash

undefined method `each' for "":String

Now my question is how do I change the data_type of my column without losing the data I already have

How to get data from 2 tables based on search keyword rails

Posted: 09 Jan 2017 07:50 AM PST

I have 2 tables schools and private_schools and they are not related to each other. What I need to do is to search the data from both tables to populate the results.

Both tables has the attribute name from which I need to get results. So when user will write some keyword the data will be searched from both tables.

Here is the query I am trying but it returns errors.

Query:

User.includes(:schools, :private_schools).where("schools.name = ? or   private_schools.name = ?", params[:keyword])  

I know the User. sort of thing is also useless and is also not needed but how we should cope this issue to search keywords from both tables at same time?

Error is:

ActiveRecord::PreparedStatementInvalid (wrong number of bind variables (1 for 2) in: schools.name = ? or private_schools.name = ?):

Per comments the code of models are:

Private School:

class PrivateSchool < ActiveRecord::Base    belongs_to :teacher    has_many :private_school_specializations, :dependent => :destroy    has_many :private_classes    has_many :invitation_promo_codes    has_many :popular_schools, as: :resource, :dependent => :destroy  end  

School:

class School < ActiveRecord::Base    belongs_to :user    has_many :students    has_many :subjects    has_many :teachers    has_many :departments    has_many :student_ids    has_many :teacher_ids    has_many :class_rooms    has_many :popular_schools, as: :resource, :dependent => :destroy  end  

User:

class User < ActiveRecord::Base    has_one :school    has_one :student    has_one :teacher    accepts_nested_attributes_for :student    accepts_nested_attributes_for :teacher  end  

Undefined method render_to_string

Posted: 09 Jan 2017 03:10 AM PST

I have a gem that contains a method called get_component that is supposed to render the card called and it's specific data. I also have a call in a separate host app that makes the call to the gem method. I'm trying to make a simple integration but rails seems to think that the method doesn't exist, which is confusing because ApplicationController inherits from ActionController::Base. I've tried to manually make an object of the ActionController class.

ac = ActionController::Base.new()  ac.render_to_string()  

and also calling ApplicationController again:

ApplicationController.new.render_to_string  

Which doesn't particularly make sense but was worth testing. Both times i still end up with:

undefined method `render_to_string' for Pugin::ApplicationController:Class  

I'm a little stumped, Does it not recognise ActionController::Base in the controller or is there something fundamentally wrong with how I'm trying to call the method?

Person File

=> PUGIN.get_component('modules/card', { \      title: defined?(person.display_name) ? link_to(person.display_name, person_path(person.id)) : "No Information", \      house: defined?(person.houses.first.id) ? person.houses.first.id : "Lord", \      image: [{ src: "http://wraggelabs.com/static/images/default_avatar-2.gif", \                alt: person.display_name \             }], \      details: [{ description: defined?(person.constituencies.first.id) ? link_to(person.constituencies.first.name, constituencies_path(person.constituencies.first.id)) : "No Information"}, \               { description: defined?(person.houses.first.id) ? link_to(person.houses.first.id, houses_path(person.houses.first.id)) : "No Information"}, \               { description: defined?(person.parties.first.id) ? link_to(person.parties.first.name, houses_path(person.parties.first.id)) : "No Information"}] \     })  

PUGIN gem controller

module Pugin    class ApplicationController < ActionController::Base      protect_from_forgery with: :exception        def self.get_component(component_name, properties = {})        #properties.each |property| do          #render :partial => "layouts/pugin/#{component_name}", locals: {properties: property}          render_to_string("layouts/pugin/_#{component_name}", :formats => [:html])        

No comments:

Post a Comment