Why can't I get /comments/new.html.erb? Posted: 03 Oct 2016 08:04 AM PDT I created models by scaffold command class Post < ActiveRecord::Base has_many :comments, as: :commentable end class Comment < ActiveRecord::Base belongs_to :commentable , polymorphic: true end ... routes.rb: resources :posts, :images, :links do resources :comments end comments_controller.rb: def new @comment = Comments.new end /posts/show.html.erb: <%= link_to 'Add comment', new_post_comment_path (@post)%> Here I think I need ...(@post, @comment),like from http://guides.rubyonrails.org/routing.html: <%= link_to 'Ad details', magazine_ad_path(@magazine, @ad) %> but I haven't @comment here. I get error: Showing /home/loza/Projects/my_blog/app/views/comments/_form.html.erb where line #1 raised: undefined method `comments_path' for #<#<Class:0x007f2e4a77c2f0>:0x007f2e4ab23ef8> Extracted source (around line #1): <%= form_for(@comment) do |f| %> <% if @comment.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2> <ul> How need I write so as to get /comments/new.html.erb? |
call method of a controller from different controller view Posted: 03 Oct 2016 08:16 AM PDT The idea that when the user filling the booking info, he need to verify his phone number in the form where he receive the code and put it with rest of booking info before submitting , so I have the following view <%= form_for(@booking) do |f| %> <%= current_or_not_authenticated_user.email %> <br> <%= f.label :patient_id %> <%= f.collection_select :patient_id, User.order(:created_at), :id, :email %> <button type="button" class="btn btn-primary btn-md" data-toggle="modal" data-target="#patientModal"> New Patient </button> <br> <div class="form-group"> <%= f.label :type %> <%= f.select :booking_type, options_for_select([['Physiologist'], ['Psychiatrist'], ['Pediatrician']]) %> <%= f.hidden_field :customer_id, :value => current_or_not_authenticated_user.id %> </div> <div class="form-group"> <%= f.label :address %> <%= f.collection_select :address_id, Address.where(user_id: current_or_not_authenticated_user.id), :id, :address1 %> <button type="button" class="btn btn-primary btn-md" data-toggle="modal" data-target="#addressModal"> New Address </button> <% if current_user && !current_user[:verified_by_phone_at] %> <div class="form-group"> <%= label_tag(:phone) %> <%= text_field_tag(:phone) %> <button id="smsBtn" type="button" class="btn btn-primary">Send SMS</button> </div> <div class="form-group"> <%= label_tag :code %> <%= text_field_tag :code %> </div> <% end %> <%= f.submit "Confirm Booking" %> </div> <br> <% end %> <script> $(document).ready(function () { $('#smsBtn').on('click', function () { $.ajax({ url: <%=phone_verification_index_path %>, type: 'ajax', data: $('#code').val() }); }); }); </script> clicking on smsbtn should go to phone_verification controller which in turn call third party method that send sms to the user, then the user fill his code and click on submit. my problem is when the user click on smsbtn, the @booking form get submitted and I don't want to form submit unless the user click on Confirm Booking, the only way that my jquery was working, was when I moved both phone field and smsbtn outside form. the order of the fields is important because I don't want the user to find code field first then phone field under it. |
fill a JSON file with embedded arrays from Rails Posted: 03 Oct 2016 07:24 AM PDT I have a simple table indexed by an id. I would like to have a structured JSON that group each user from a family in an array structure. And then each family in a global array. How can I do that ? my plain code : json.rss @rss do |rs| json.id rs.id json.name rs.name json.family rs.family json.lastdate rs.lastdate json.last rs.last json.s1w rs.s1w json.s2w rs.s2w json.s1m rs.s1m json.s2m rs.s2m json.s3m rs.s3m json.s6m rs.s6m json.ytd rs.ytd end |
Collecting text submitted in input field with ember-rails Posted: 03 Oct 2016 07:27 AM PDT I am following an ember-rails tutorial. It's using coffeescript and I'm writing in javascript. Essentially I have a form that when I submit text on it it should place the text below. My controller looks like this: Emberapp.ApplicationController = Ember.Controller.extend({ entries: [], actions: { addEntry: function(){ this.entries.pushObject name: this.get('newEntryName'); this.set('newEntryName', ""); } } }); My template looks like this <div id = "container"> <h1>Emberails</h1> {{ input value=newEntryName enter='addEntry' }} <ul> {{#each entries}} <li>{{name}}</li> {{/each}} </ul> </div> When I reload the page I get an error stating that 'name: this.get('newEntryName');' is an Unexpected identifier. I've been checking for the syntax rules online but I'm not sure if it is a coffeescript to javascript error or something else. |
What is the purpose of the "system" folder in rails "public/system/"? Posted: 03 Oct 2016 07:05 AM PDT The standard place to store non-precompiled public assets in a rails app is in "public/system." Is there any reason for this? I'd like to keep things simple, why shouldn't I just put assets in the "public" folder? |
Creating model and nested model (1:n) at once with ActiveRecord Posted: 03 Oct 2016 06:54 AM PDT My Rails5 application has an organization model and a user model (1:n relationship). The workflow of creating an organization should include the creation of the organization's first user as well. I thought this would be able with ActiveRecord through nested models, however the create action fails with the error message "Users organization must exist". class Organization < ApplicationRecord has_many :users, dependent: :destroy accepts_nested_attributes_for :users end class User < ApplicationRecord belongs_to :organization end class OrganizationsController < ApplicationController def new @organization = Organization.new @organization.users.build end def create @organization = Organization.new(organization_params) if @organization.save redirect_to @organization else render 'new' end end def organization_params params.require(:organization).permit(:name, users_attributes: [:name, :email, :password, :password_confirmation]) end end In the view I use the <%= f.fields_for :users do |user_form| %> helper. Is this a bug on my side, or isn't this supported by ActiveRecord at all? Couldn't find anything about it in the rails guides. After all, this should be (theoretically) possible: First do the INSERT for the organization, then the INSERT of the user (the order matters, to know the id of the organization for the foreign key of the user). |
Destroy functionality not working in ToDo app . Editing works just fine Posted: 03 Oct 2016 06:49 AM PDT I am making a basic ToDo app and have lost functionality to destroy (delete) a task after I create it. There are two buttons on the app so far, edit, and delete. The edit button is fully functional. I can't seem to figure out where I went wrong. A click on the delete button does not even prompt a dialog box to come up with options any longer. Any advice would be appreciated greatly. Task.rb:
class Task < ActiveRecord::Base validates_presence_of :title validate :future_completed_date private def future_completed_date if !completed.blank? && completed > Date.today self.errors.add(:completed, "can't be in the future") end end end
Tasks controller:
class TasksController < ApplicationController def new @task = Task.new render :show_form end def create @task = Task.new(task_params) save_task end def edit @task = Task.find(params[:id]) render :show_form end def update @task = Task.find(params[:id]) @task.assign_attributes(task_params) save_task end def destroy @task = Task.find(params[:id]) authorize! :destroy, @task @task.destroy @tasks = Task.all end private def save_task if @task.save @tasks = Task.all render :hide_form else render :show_form end end def task_params params.require(:task).permit(:title, :note, :completed) end end |
Redmine override application.rb and add new Initializer in plugin Posted: 03 Oct 2016 06:37 AM PDT I'am using a gem ntlm-sso. I want to use it in Redmine. As it is written in the instructions, I added new file in config/initializers/ntlm-sso.rb and adding the line config.middleware.use "NTLMAuthentication" to config/application.rb . It works, but I want to use it as a plugin, without changing the core code. How can I override application.rb and add new initializer as Redmine plugin? |
How to use simple_form with a gem to select state/province and city one a country is selected? Posted: 03 Oct 2016 06:32 AM PDT I am looking to a find a solution to select in a form, based on country selected (which works well) one of the states or provinces of the selected country and once the state is selected, one of the cities in that state to be selected further. I am looking at this gem (since others I found are not a match for Rails 5). https://github.com/loureirorg/city-state/issues Or is someone else has a better solution I am looking to use it. |
Sidekiq + ActionJob with ActionCable in production mode Posted: 03 Oct 2016 07:11 AM PDT My stack Rails 5, ActionCable, ActiveJob, Sidekiq 2.2 and Redis. In development mode all work correct. In production with Sidekiq and Redis ActiveJob doesn't work. Source code: #notification_channel def execute(data) act = data['act'] data['current_profile'] = current_profile data['profile'] = Profile.find(data['profile']); follow_unfollow(act, data['profile']) NotificationJob.perform_later act, data end #NotificationJob class NotificationJob < ApplicationJob queue_as :default def perform(type, data) begin #some code ActionCable.server.broadcast "notifications_channel_#{data['profile'].id}", params rescue => e logger.error(e.message.red + "\n") logger.error(e.backtrace.inspect.red + "\n") end end end Cable configs: #config/cable.yml production: adapter: redis url: redis://localhost:6379/1 Sidekiq Configs: #config/initializers/sidekiq.rb Sidekiq.configure_server do |config| config.redis = { :url => 'redis://localhost:6379/1', :namespace => my_namespace } end Sidekiq.configure_client do |config| config.redis = { :url => 'redis://localhost:6379/1', :namespace => my_namespace } end In production log I see: #production.log D, A LOT OF ACTIONS FROM notification_channel I, [2016-10-03T00:00:11.535425 #8897] INFO -- : [ActionCable] [tag] [ActiveJob] Enqueued NotificationJob (Job ID: eb93ed75-a8f3-488d-ac1a-ccd6656d2a7c) to Sidekiq(my_namespace_default) with arguments: "follow", {"act"=>"follow", ...other_params...} Sidekiq log contains only info about Booting, has no message about some actions. Please help me! |
rails - list of tenants to migrate appears to be empty when loading a schema Posted: 03 Oct 2016 06:28 AM PDT what does it mean if in rails I run db:schema:load, and the schema loads until it says that "the list of tenants to migrate appears to be empty"? And what ways are available to solve this? As in here (sorry I am unable to format it better): ... --create_table("users", {:force=>:cascade}) -> 0.4490s -- create_table("web_migrations", {:id=>false, :force=>:cascade}) -> 0.6138s -- initialize_schema_migrations_table() -> 0.0332s [WARNING] - The list of tenants to migrate appears to be empty. This could mean a few things: 1. You may not have created any, in which case you can ignore this message 2. You've run `apartment:migrate` directly without loading the Rails environment * `apartment:migrate` is now deprecated. Tenants will automatically be migrated with `db:migrate` Note that your tenants currently haven't been migrated. You'll need to run `db:migrate` to rectify this. |
If statement causing NoMethodError for new accounts Posted: 03 Oct 2016 07:33 AM PDT I'm working on a project where I want a user to be able make a post once a day. If the user has a post already that day then I want the UI elements for submitting a post not to show. To do this I've used: <% if current_user.posts.last.date.past? %> in my html.erb file. However the problem with this is if the user has never made a post then it causes undefined method `date' for nil:NilClass error. I'm not sure what to do to fix this? |
How to pluck "as alias_name" from rails active record query Posted: 03 Oct 2016 07:47 AM PDT I have this query: Client.select("name as dname") Which is working fine. Client.select("name as dname").first.dname => "Google" Now I want to get all dnames as an array but pluck method does not work as dname is not column name. 2.2.5 :040 > Client.select("name as dname").pluck(:dname) (0.6ms) SELECT dname FROM "clients" ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "dname" does not exist How to get array of dnames? Is there any method like pluck which works on column name alias which is defined using as. I can do this Client.select("name as dname").map{|d| d.dname} But looping through every record is not making any sense to me |
Rails change ActiveJob adapter for a single job Posted: 03 Oct 2016 05:34 AM PDT I have some scheduled tasks that run jobs with a real queue service. Now, I want to be able to run those tasks manually from a dashboard, wait for its execution, and read/process some of its outputs in a controller action. The typical case being when an admin wants to manually reload some statistics, and it's fine to stall the server for a couple secs. Is there a way to change the ActiveJob adapter to "inline" in order to do that ? If possible I'd also like to read some artifacts generated by this job (could be instance variables, or the return value of the perform action). Any way to do that ? |
How do I return distinct values with options_from_collection_for_select helper? Posted: 03 Oct 2016 06:02 AM PDT I am trying to return the countries of my products within a select tag using options_from_collection_for_select() I have a Product model, with country as a column name. My code looks like this <%= select_tag(:country, options_from_collection_for_select(Product.all.order(:country), :id, :country), :prompt => "Alle Länder") %> However it gives me each country more than once in the dropdown field i.e. each country appears 20 times. Now I've been trying to use a solution suggested in a similar question, like so options_from_collection_for_select(Product.all.pluck('DISTINCT country'), :id, :country), :prompt => "Alle Länder") %> however I get an error stating: "undefined method `country' for "Spain":String Did you mean? count" Now I cant figure out which method is appropriate in this case. Thank you very much! |
sharing session of Rails cookie store between rails and grape [duplicate] Posted: 03 Oct 2016 05:28 AM PDT This question is an exact duplicate of: I want to share Rails session of cookie store between Rails and grape. I watch following ling method. https://gist.github.com/jgyllen/3862855 But I cannot share session. Do you know someone better way? |
Why we are creating two different mailer view(welcome_email.text.erb and welcome_email.html.erb) for single mailer action? Posted: 03 Oct 2016 05:16 AM PDT Many times I have created two different formats for action mailer view templates like welcome_email.text.erb and welcome_email.html.erb. But I didn't find the proper solution for why we are creating those files? can you please any one help me? |
Sorting based on multiple submodel conditions/states Posted: 03 Oct 2016 07:26 AM PDT I have a Room model that has_many Intervals. Intervals has a start_date and end_date attributes. I want to order the Rooms based on the following conditions 1- Room with no intervals 2- Room with any interval(s) with start_date > Date.today 3- Room without any intervals with start_date > Date.tody I want to sort the Rooms in such a way that Rooms satisfying 1 come first then Rooms satisfying 2 then 3 using a postgresql query not a method on the model. While I have no problem finding out which Rooms have no intervals using the query/scope below joins("LEFT OUTER JOIN intervals ON rooms.id = intervals.room_id").where("intervals.id is NULL") I am having issues ordering by the 3 conditions in a single query, I tried the following Room.joins("LEFT OUTER JOIN intervals ON rooms.id = intervals.room_id").order("CASE WHEN intervals.id IS NULL THEN 1 END ASC") The issue with this query is that if you call distinct on it you get the error PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list , the other issue even when you call distinct here and it works properly you would have sorted by only one condition which is the first 1. My idea is to generate a function with case conditional to give a value of generated column based on the 3 conditions and give the first the value 1, the second value 2, and the third value 3 and sort by this computed value, whenever I try it though I get hit by the error mentioned above. Any help would be appreciated. This is my trial to combine all 3 conditions: Room.joins("LEFT OUTER JOIN intervals ON rooms.id = intervals.room_id").group("room.id").order("CASE WHEN room.id IS NULL THEN 1 WHEN COUNT(CASE WHEN intervals.start_date > '#{Date.today.to_s(:db)}' THEN 1 END) > 0 THEN 2 WHEN COUNT(CASE WHEN intervals.start_date > '#{Date.today.to_s(:db)}' THEN 1 END) = 0 THEN 3 END") This however gives the error: PG::GroupingError: ERROR: column "intervals.id" must appear in the GROUP BY clause or be used in an aggregate function Any help would be appreciated. |
rails validate in model that value is inside array[2] Posted: 03 Oct 2016 08:04 AM PDT This question similar with this one, but those answers don't work for me(May be they are deprecated). I don't want to reopen old question by bounty, because I have a little different arguments. In my case i have an array like this allowed_values = [99.50..200] and I tried with: class Thing < ActiveRecord::Base validates :price, :inclusion=> { :in => allowed_values } It does not work. I have tried with so many ways by examples validation here. I have little experience on coding rails. So please, help to find solution. More info: ruby version: ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-linux] rails version: Rails 4.1.5 |
Net::SSH::AuthenticationFailed while deploying Rails 4.2.x application using capistrano 3.6.1 Posted: 03 Oct 2016 05:05 AM PDT When I try to deploy the application with capistrano 3.6.1 and ruby 2.3.1, I get following error for Rails 4.2.x amit@amit:~/smart-recruit$ cap production doctor (Backtrace restricted to imported tasks) cap aborted! Net::SSH::AuthenticationFailed: Authentication failed for user deploy@myserver.com Tasks: TOP => rvm:hook => passenger:rvm:hook => passenger:test_which_passenger (See full trace by running task with --trace) deploy.rb # config valid only for current version of Capistrano lock '3.6.1' set :application, 'smart-recruit' set :repo_url, 'git@github.com:SomeUser/smart-recruit.git' set :deploy_to, '/var/www/rubyapps/smart-recruit' set :user, 'deploy' set :pty, false #There is a known bug that prevents sidekiq from starting when pty is true on Capistrano 3. set :use_sudo, true set :deploy_via, :remote_cache set :linked_files, %w{config/database.yml config/secrets.yml} set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system} set :branch, 'development' set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub)} config/deploy/production.rb set :stage, :production server 'myserver.com', roles: %w{web app db}, primary: true Capfile # Load DSL and set up stages require 'capistrano/setup' # Include default deployment tasks require 'capistrano/deploy' require 'capistrano/bundler' require 'capistrano/rails' require 'capistrano/rvm' require 'capistrano/rails/assets' require 'capistrano/rails/migrations' require 'capistrano/faster_assets' require 'capistrano/passenger' require 'capistrano/sidekiq' # Load custom tasks from `lib/capistrano/tasks` if you have any defined Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r } set :rvm_type, :user set :rvm_ruby_version, '2.3.1' I verified that passenger + apache is working fine on the server. Note: It works perfectly for Ruby 2.2.2 and Capistrano 3.5.0 in other applications |
Rails 5 - model with 2 fields referring to same object Posted: 03 Oct 2016 06:20 AM PDT In the app I am building to learn RoR, I have a model "Document" and a model "Business Partner". On the "Document" model, I have 2 fields ("Sender" and "Receiver") referring to the model "Business Partner". How do I model twice a belongs_to the same target with different fields? What should the migration be? Related question: how do I model the relationship to it self for business partner? i.e. one company has many business partners, yet can be a business partner too. Note - not to the same record (company A cannot have a relationship to company A (itself). |
Rails to Heroku deployment -> Sass::SyntaxError: Invalid CSS after " bootstrap": expected ";", was ".css" Posted: 03 Oct 2016 04:36 AM PDT I've been searching for an answer for quite sometime but can't seem to solve, so I'm hopping that I can reach it with some help. I'm trying to deploy a rails application to Heroku but it gives me the following error: rake aborted! Sass::SyntaxError: Invalid CSS after " bootstrap": expected ";", was ".css" (sass):6146 /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/scss/parser.rb:1189:in expected' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/scss/parser.rb:1125:in expected' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/scss/parser.rb:1120:in tok!' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/scss/parser.rb:777:in block in try_declaration' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/scss/parser.rb:1165:in call' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/scss/parser.rb:1165:in rethrow' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/scss/parser.rb:782:in try_declaration' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/scss/parser.rb:711:in declaration_or_ruleset' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/scss/parser.rb:676:in block_child' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/scss/parser.rb:668:in block_contents' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/scss/parser.rb:657:in block' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/scss/parser.rb:649:in ruleset' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/scss/parser.rb:675:in block_child' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/scss/parser.rb:668:in block_contents' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/scss/parser.rb:125:in stylesheet' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/scss/parser.rb:41:in parse' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/engine.rb:406:in _to_tree' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sass-3.4.22/lib/sass/engine.rb:281:in render' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/sass_compressor.rb:48:in call' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/sass_compressor.rb:28:in call' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/processor_utils.rb:75:in call_processor' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/processor_utils.rb:57:in block in call_processors' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/processor_utils.rb:56:in reverse_each' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/processor_utils.rb:56:in call_processors' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/loader.rb:134:in load_from_unloaded' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/loader.rb:60:in block in load' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/loader.rb:317:in fetch_asset_from_dependency_cache' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/loader.rb:44:in load' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/cached_environment.rb:20:in block in initialize' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/cached_environment.rb:47:in yield' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/cached_environment.rb:47:in load' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/base.rb:66:in find_asset' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/base.rb:73:in find_all_linked_assets' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/manifest.rb:142:in block in find' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/legacy.rb:114:in block (2 levels) in logical_paths' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/path_utils.rb:228:in block in stat_tree' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/path_utils.rb:212:in block in stat_directory' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/path_utils.rb:209:in each' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/path_utils.rb:209:in stat_directory' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/path_utils.rb:227:in stat_tree' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/legacy.rb:105:in each' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/legacy.rb:105:in block in logical_paths' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/legacy.rb:104:in each' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/legacy.rb:104:in logical_paths' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/manifest.rb:140:in find' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/sprockets/manifest.rb:185:in compile' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-rails-3.2.0/lib/sprockets/rails/task.rb:68:in block (3 levels) in define' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.0/lib/rake/sprocketstask.rb:147:in with_logger' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/sprockets-rails-3.2.0/lib/sprockets/rails/task.rb:67:in block (2 levels) in define' /tmp/build_1cc2a72cadb401f09a57ebad6ed802d1/vendor/bundle/ruby/2.2.0/gems/rake-11.3.0/exe/rake:27:in ' Pre compiling locally, even with the RAILS_ENV=production activated, works without any issue, but when I push to Heroku this error comes up. Thank you in advance! |
Link two attributes to one form element Posted: 03 Oct 2016 04:13 AM PDT I want to be able to have one select which specifies the value of two model attributes. Therefore if you were to select "yes" on a drop-down it would set the value of attribute a and attribute b to "yes" when the form is submitted. Although the below doesnt work it might help explain what I mean: = f.select :trial, :free , [['Yes', 'true'], ['No', 'false']], {}, class: 'selectpicker mandatory' |
Rails new object with store_accessor and Postgresql jsonb fails Posted: 03 Oct 2016 04:04 AM PDT I have a model Car which has a jsonb field for extra data. class Car < ApplicationRecord store_accessor :data, :extra_options end class AddDataToCars < ActiveRecord::Migration[5.0] def change add_column :cars, :data, :jsonb, null: false, default: '{}' add_index :cars, :data, using: :gin end end I can seed my table with: car = Car.create!({ make: 'Lada', model: 'Niva', data: { extra_options: 'Some options' } }) and then get and set the extra_options field with: car.extra_options # => "Some options" car.extra_options = "Other options" # => "Other options" However I cannot create a new object and then straight away set the extra_options field car = Car.new # => #<Car id: nil, make: nil, model: nil, created_at: nil, updated_at: nil, data: "{}"> car.extra_options = "Some option" # IndexError: string not matched # ... I can do car.data = { extra_options: 'options' } # => {:extra_options=>"options"} car.extra_options # => "options" But I would like the extra_options field to behave like a normal field so I can easily use it in my views like <%= form_for(rating) do |f| %> <%= f.label :extra_options %> <%= f.text_field :extra_options %> <% end %> Is that at all possible? If not, how do I create a text field in the view that writes to the extra_options field? |
force capistrano to use bundle exec on sidekiq start Posted: 03 Oct 2016 03:54 AM PDT I use capistrano-sidekiq. bundle exec cap production sidekiq:start results in: HOME/.rbenv/bin/rbenv exec sidekiq --index 0 --pidfile /home/ubuntu/cap/shared/tmp/pids/sidekiq-0.pid --environment production --logfile /home/ubuntu/cap/shared/log/sidekiq.log --daemon How do I make the above command produce the following (note bundle exec ): HOME/.rbenv/bin/rbenv exec bundle exec sidekiq --index 0 --pidfile /home/ubuntu/cap/shared/tmp/pids/sidekiq-0.pid --environment production --logfile /home/ubuntu/cap/shared/log/sidekiq.log --daemon |
Form submit button not showing Posted: 03 Oct 2016 03:57 AM PDT I'm working on a project where I'm trying to implement a voting system, but I'm having some trouble, I've tried creating a form with a hidden field: <% form_for :vote, url: votes_path do |f| %> <%= f.hidden_field :rating, value: '1' %></td> <%= form_submit_button("vote 1") %> <% end %> But when I run that the submit button does not appear, I'm not really sure what I'm doing wrong. |
Rails associated models with radio buttons Posted: 03 Oct 2016 03:46 AM PDT I have a boat model and I would like to show to user water sports equipment. User can select either available or optional radio button in boat#new action. But the problem is I could not find how to associate them. Here how it will look like; Then for boat#show action I would like to show whether they are available or optional. I do not want to put; has_one :canoe has_one :fishing equipment has_one : wakeboard ... to boat model for every each of the water sport. How can I do that in an efficient way? |
ActionCable delete message Posted: 03 Oct 2016 03:45 AM PDT I followed this tutorial to create a chat with ActionCable and Devise: https://www.sitepoint.com/create-a-chat-app-with-rails-5-actioncable-and-devise/ But it's essential, that a message can be deleted too. How can this be done, so that the message is disapearing from all the userscreens simultaniously. Here are my files: rooms.coffee jQuery(document).on 'turbolinks:load', -> messages = $('#messages') if $('#messages').length > 0 messages_to_bottom = -> messages.scrollTop(messages.prop("scrollHeight")) messages_to_bottom() App.global_chat = App.cable.subscriptions.create { channel: "ChatRoomsChannel" chat_room_id: messages.data('chat-room-id') }, connected: -> # Called when the subscription is ready for use on the server disconnected: -> # Called when the subscription has been terminated by the server received: (data) -> messages.append data['message'] messages_to_bottom() send_message: (message, chat_room_id) -> @perform 'send_message', message: message, chat_room_id: chat_room_id $('#new_message').submit (e) -> $this = $(this) textarea = $this.find('#message_body') if $.trim(textarea.val()).length > 1 App.global_chat.send_message textarea.val(), messages.data('chat-room-id') textarea.val('') e.preventDefault() return false message_broadcast_job.rb class MessageBroadcastJob < ApplicationJob queue_as :default def perform(message) ActionCable.server.broadcast "chat_rooms_#{message.guild.id}_channel", message: render_message(message) end private def render_message(message) MessagesController.render partial: 'messages/message', locals: {message: message} end end chat_rooms_channel.rb class ChatRoomsChannel < ApplicationCable::Channel def subscribed stream_from "chat_rooms_#{params['chat_room_id']}_channel" end def unsubscribed # Any cleanup needed when channel is unsubscribed end def send_message(data) current_user.messages.create!(body: data['message'], guild_id: data['chat_room_id']) end end message.rb class Message < ApplicationRecord belongs_to :user belongs_to :guild validates :body, presence: true, length: {minimum: 2, maximum: 1000} def timestamp created_at.strftime('%H:%M:%S %d %B %Y') end after_create_commit { MessageBroadcastJob.perform_later(self) } end I want to have a destroy_link on every message. |
Rails 5 - implement reusable comment model - views? Posted: 03 Oct 2016 03:01 AM PDT In my app that I am building to learn RoR, I have a similar situation like this question. Now my question is how to change my views for this? I have an Annotation model, a Document model and a Comment model. If I switch to a polymorphic association such that my Annotations and my Documents can have Comments, how to do the view (in the partial)? This is the current partial: <%= simple_form_for([@annotation, @annotation.comments.build], html: { class: 'form-vertical', multipart: true }) do |f| %> <%= f.error_notification %> <%= f.input :commenter, :as => :hidden, :input_html => { :value => current_user.username }, label: false %> <%= f.input :body, placeholder: 'comment', focus: true, label: false %> <%= f.button :submit, 'Save' %> <% end -%> |
Create a model record when a button is pressed Posted: 03 Oct 2016 03:18 AM PDT I'm working on a project, and I'm trying to implement a feedback system. There are three buttons, positive, neutral and negative and at the moment when the user clicks a button they are asked if they want to submit more feedback and are taken to the new feedback form. However instead what I want is that when a button is pressed a record is saved with the rating field of the model set by which button they pressed. Then when a user gives more feedback they are taken to the edit page for that feedback. I'm not really sure how to go about doing this, would I need some javascript on the buttons? or does rails have a method that will do this? Thank you |
No comments:
Post a Comment