Affichage des articles dont le libellé est Active questions tagged ruby - Stack Overflow. Afficher tous les articles
Affichage des articles dont le libellé est Active questions tagged ruby - Stack Overflow. Afficher tous les articles

vendredi 31 juillet 2015

Hash and values

I came across this Ruby script:

frequency = Hash.new(0)
...
...
file.read.downcase.scan(/\b[a-z]{4,20}\b/){|word| frequency[word] =
frequency[word]+1}

The point I couldn't understand is frequency[word] = frequency[word]+1

Wouldn't frequency[word] give me the word matched? How can we add it to 1?

Mongoid, setting custom accessor field in mongoid-history gem

I'm adding mongoid-history gem to my project.

According to guide in github, when I add Userstamp to my tracker it creates created_by field with accessor called creator.

They have written that I can rename it via gem config.

How to rename this field?

Rails_admin show child models fields in parents show page

I have Item model and I have ItemImage child model which is embedded inside of its parent.

When I open single Items page in rails_admin, it shows all of its children like this:

ItemImage #55b766556c656e12d0ac0000, ItemImage #55b766556c656e12d0ad0000, and ItemImage #55b766566c656e12d0ae0000

How to make rails_admin to show all of its children fields inside parents page? Is it possible to show them as table?

I'm using Mongoid.

How best to represent roles in the DB of a rails app?

I'm building authorization in my app and have three roles:

  • User
  • Moderator
  • Admin

What's the best way to represent this in Rails? I've thought about adding the following boolean fields to my user's table:

  • is_user
  • is_moderator
  • is_admin

And then creating is_user?, etc functions in the User model.

A user can have multiple roles (can be all three).

Can't Compile LESS to CSS with LESS2CSS Plugin on Sublime Text 3 Mac

I am using Sublime Text 3 on Mac OS Yosemite. I want to use LESS2CSS package but it not working. Here are the errors it gives:

WARNING] Please install gem 'therubyracer' to use Less.
/Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- v8 (LoadError)
    from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Library/Ruby/Gems/2.0.0/gems/less-2.6.0/lib/less/java_script/v8_context.rb:2:in `<top (required)>'
    from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Library/Ruby/Gems/2.0.0/gems/less-2.6.0/lib/less/java_script.rb:9:in `default_context_wrapper'
    from /Library/Ruby/Gems/2.0.0/gems/less-2.6.0/lib/less/java_script.rb:17:in `context_wrapper'
    from /Library/Ruby/Gems/2.0.0/gems/less-2.6.0/lib/less/loader.rb:13:in `initialize'
    from /Library/Ruby/Gems/2.0.0/gems/less-2.6.0/lib/less.rb:14:in `new'
    from /Library/Ruby/Gems/2.0.0/gems/less-2.6.0/lib/less.rb:14:in `<module:Less>'
    from /Library/Ruby/Gems/2.0.0/gems/less-2.6.0/lib/less.rb:9:in `<top (required)>'
    from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Library/Ruby/Gems/2.0.0/gems/less-2.6.0/bin/lessc:3:in `<top (required)>'
    from /usr/bin/lessc:23:in `load'
    from /usr/bin/lessc:23:in `<main>'

I've installed Node.js and installed less, but it is still not working.

Cannot find my mistake with jQuery autocomplete and Rails 4

router

  resources :pcps  do
    collection do
      get 'autocomplete'
    end
  end

controller

def autocomplete
    @pcps = Pcp.order(:last).where("last like ?", "%#{params[:term]}%")
    render json: @pcps.map(&:full_name)
end

View

= f.text_field :pcp, data: {autocomplete_source:  '/pcps/autocomplete'}

Coffee script

This code works

jQuery ->
  $('#sched_pcp').autocomplete
    source: ["Atest", "Btest","Ctest","Dtest","Btest1","Btest2"]

This code does not work.

jQuery ->
  $('#sched_pcp').autocomplete
    source: $('#sched_pcp').data('autocomplete-source')

I am using ruby 2.2 and rails 4.2 The url '/pcps/autocomplete' returns the correct data. Can someone help me see my mistake please? or maybe give me an idea how to debug it.

How do I seed a unique record ONLY via my seeds.db?

I have the following code in my seeds.rb used to create a record in my basic Rails app via seeding.

Post.create( title: "Unique Title!", body: "this is the most amazingly unique post body ever!" )

When running the rake db:seed command, it obviously seeds the db with this data. My question is how do I add a check or safeguard in the code so that it only enters that once, i.e. as a unique? If I rerun rake db:seed, I don't want add that same entry again.

Thanks

Ruby HTTPS Post issue

I have two codes (variable info masked intentionally), the first one I receive the response with 200 code return, but second one I get 403 forbidden, any idea?

def get_token()
    http = Net::HTTP.new(server, 443)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl?

    #path(a.k.a) ->http://ift.tt/1DXPdIx'
    path = '/rest/fastlogin/v1.0?app_key=' + app_key + '&username=%2B' + username + '&format=json'
    puts path

    headers = {'Content-Type'=> 'application/x-www-form-urlencoded', 'Authorization' => password }

    resp, data = http.post(path, data, headers)

    puts 'Code = ' + resp.code
    puts 'Message = ' + resp.message

    resp.each {|key, val| puts key + ' = ' + val}
    puts data
    puts JSON.parse(resp.body)["access_token"]

    result = {}

    result["code"] = resp.code
    result["token"] = JSON.parse(resp.body)["access_token"]

    print  result

    return result
end



def get_token1()
    path = '/rest/fastlogin/v1.0?app_key=' + app_key + '&username=%2B' + username + '&format=json'

    uri = URI.parse('https://' + server + path)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl?

    req = Net::HTTP::Post.new(uri.path)
    req["Authorization"] = password

    puts uri.host
    puts uri.path
    puts uri.port
    resp,data = http.request(req)
    print resp 
end

No route matches [POST] - Rails destroy

I'm new to RoR and still don't have enough experience on solving the different errors that may appear to me. In this case I'm designing a blog where I can post articles. More specifically, my problem is related to deleting these articles.

As far as I know, writing:

resources :articles

in the routes file is an alternative for writing:

get "/articles"            #index
post "/articles"           #create
delete "/articles/:id"     #delete
get "/articles/:id"        #show
get "/articles/new"        #new
get "/articles/:id/edit"   #edit
patch "/articles/:id"      #update
put "/articles/:id"        #update

When I try to delete an article I get the following error:

No route matches [POST] "/articles/1"

The code I wrote was:

View

<% @articles.each do |art| %>
    <%= art.title %>
    <div>
        <%= art.body %> - <%= link_to "Delete", art, method: :delete %>
    </div>
<% end %>

Controller

def destroy
    @article = Article.find(params[:id])
    @article.destroy
    redirect_to articles_path       
end

I can't see what am I missing. Thanks in advanced.

Merit doesn't add points to user after create action

I've used this instructions for simply add score when a user creates a "solucion" (which is a kind of "answer" to a micropost). I have added the has_merit line to user.rb (user model).

I want to display the user points earned for that action at the show view. show.html.erb (for solucion):

<h2><span class="red"><%= current_user.points %></span><br>Points</br></h2>

It displays 0 points...

point_rules.rb:

module Merit
  class PointRules
   include Merit::PointRulesMethods

   def initialize
    score 5, on: 'solucions#create'
   end
  end
end

When I create a solucion with the current_user (already saving the user_id index and identifier to solucion), This is what my rails server output shows...

Direct link to github gist:

http://ift.tt/1DXPevP

Embed:

<script src="http://ift.tt/1SquLMf"></script>

Illformed requirement error when installing gems

I'm getting the following error whenever I try to install any gem (sass as an example)

ERROR:  While executing gem ... (Gem::Requirement::BadRequirementError)
    Illformed requirement ["sass"]

I'm running Windows 10 Pro (not activated yet) so I was wondering if this could be a reason as to why this is happening? I've tried using the 32 bit version of Ruby 2.2.2 as well and it gives the same error. How do I fix this and what could be the reason I get it? Any help is appreciated, thanks!

Kill process launched via thread

I have a headless application that uses sockets for communication. When launched, it remains active until sent a message telling it to quit (or it crashes, or is killed).

When unit testing this application using Ruby, I need to launch the process, interact with it (via sockets), and then kill it.

I thought I could do this using this pattern:

class TestServer < MiniTest::Unit::TestCase
  def setup
    @thread = Thread.new{ `#{MY_COMMAND}` }
  end

  def test_aaa
    # my test code
  end

  def teardown
    @thread.kill if @thread
  end
end

However, that teardown code kills the thread but does not kill the process launched by it.

How can I launch the process in a way that:

  1. Allows it to run in the background (immediately returns control to my Ruby test harness)
  2. Allows me to force kill the process later on, if need be.

I happen to be developing on OS X, but if possible I'd appreciate a generic solution that works across all OS where Ruby runs.

Connecting to background EventMachine application for unit testing

I am writing a headless Ruby application using EventMachine that communicates over sockets. I want to write some unit tests for this app. This means that my Ruby test script needs to launch that app in the background, perform socket communication with it, and then close that process.

This code fails. The socket connection is refused.

require 'socket'
PORT = 7331
CMD = File.expand_path("../../bin/rb3jay",__FILE__)
@thread = Thread.new{ `#{CMD} -D --port #{PORT}` }
@socket = TCPSocket.open('localhost', PORT)
#=> Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 7331

If I inject a 2 second delay before attempting the socket connection, it works as desired:

@thread = Thread.new{ `#{CMD} -D --port #{PORT}` }
sleep 2
@socket = TCPSocket.open('localhost', PORT)

This seems a gross hack. Maybe 2 seconds is long enough for my machine, but too short somewhere else. How should I correctly:

  • Launch my EventMachine application in the background
  • Create a socket connection to it as soon as it is ready
  • After my tests are done, ensure that the socket is closed and that I kill the background application

Recursive SQL using Rails 4 (modeling)

I read a tutorial, but I did not understand properly. I will quote it here and present the problem that seek to solve.

Tutorial

My problem: I'm doing a forum that can have various categories and subcategories. Then decide to use SQL Recursive PostgreSQL to facilitate me and not pollute my db with unnecessary tables. Suppose I have a table categories, and it has the categories_id field. Then the model would look something like this:

  attr_accessible :parent

  belongs_to :parent, :class_name => "Category"
  has_many :children, :class_name => "Category", :foreign_key => 'categories_id'

  scope :top_level, where(:categories_id => nil)

1- I did not understand fully if parent and children are a field in the table or not. 2- Why did he have to create a attr_accessible :parent? 3- Why the creation of scope?

Thanks,

Command "rails s" not working?

 C:\Users\Sprache\Sites\simple_cms>rails -v
Rails 4.2.3

C:\Users\Sprache\Sites\simple_cms>bundle list
Gems included by the bundle:
* actionmailer (4.2.3)
* actionpack (4.2.3)
* actionview (4.2.3)
* activejob (4.2.3)
* activemodel (4.2.3)
* activerecord (4.2.3)
* activesupport (4.2.3)
* arel (6.0.2)
* binding_of_caller (0.7.2)
* builder (3.2.2)
* bundler (1.10.6)
* byebug (5.0.0)
* coffee-rails (4.1.0)
* coffee-script (2.4.1)
* coffee-script-source (1.9.1.1)
* columnize (0.9.0)
* debug_inspector (0.0.2)
* erubis (2.7.0)
* execjs (2.5.2)
* globalid (0.3.5)
* i18n (0.7.0)
* jbuilder (2.3.1)
* jquery-rails (4.0.4)
* json (1.8.3)
* loofah (2.0.2)
* mail (2.6.3)
* mime-types (2.6.1)
* mini_portile (0.6.2)
* minitest (5.7.0)
* multi_json (1.11.2)
* mysql2 (0.3.19)
* nokogiri (1.6.6.2)
* rack (1.6.4)
* rack-test (0.6.3)
* rails (4.2.3)
* rails-deprecated_sanitizer (1.0.3)
* rails-dom-testing (1.0.6)
* rails-html-sanitizer (1.0.2)
* railties (4.2.3)
* rake (10.4.2)
* rdoc (4.2.0)
* sass (3.4.16)
* sass-rails (5.0.3)
* sdoc (0.4.1)
* sprockets (3.2.0)
* sprockets-rails (2.3.2)
* thor (0.19.1)
* thread_safe (0.3.5)
* tilt (1.4.1)
* turbolinks (2.5.3)
* tzinfo (1.2.2)
* tzinfo-data (1.2015.5)
* uglifier (2.7.1)
* web-console (2.2.1)

Hey all! Following this course here on lynda, and I'm having a wee issue.

So I've got my new project setup and I'm in the root folder.

I try to run

rails s

and

rails server

and

bundle exec rails server

and I keep getting the response


C:\Users\Sprache\Sites\simple_cms>rails s C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/nokogiri-1.6.6.2-x64-mingw32/lib/nokogiri .rb:29:in require': cannot load such file -- nokogiri/nokogiri (LoadError) from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/nokogiri-1.6.6.2-x64-mingw32 /lib/nokogiri.rb:29:inrescue in ' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/nokogiri-1.6.6.2-x64-mingw32 /lib/nokogiri.rb:25:in <top (required)>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/loofah-2.0.2/lib/loofah.rb:3 :inrequire' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/loofah-2.0.2/lib/loofah.rb:3 :in <top (required)>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/rails-html-sanitizer-1.0.2/l ib/rails-html-sanitizer.rb:2:inrequire' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/rails-html-sanitizer-1.0.2/l ib/rails-html-sanitizer.rb:2:in <top (required)>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionview-4.2.3/lib/action_ view/helpers/sanitize_helper.rb:3:inrequire' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionview-4.2.3/lib/action_ view/helpers/sanitize_helper.rb:3:in <top (required)>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionview-4.2.3/lib/action_ view/helpers/text_helper.rb:32:in' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionview-4.2.3/lib/action_ view/helpers/text_helper.rb:29:in <module:Helpers>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionview-4.2.3/lib/action_ view/helpers/text_helper.rb:6:in' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionview-4.2.3/lib/action_ view/helpers/text_helper.rb:4:in <top (required)>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionview-4.2.3/lib/action_ view/helpers/form_tag_helper.rb:18:in' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionview-4.2.3/lib/action_ view/helpers/form_tag_helper.rb:14:in <module:Helpers>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionview-4.2.3/lib/action_ view/helpers/form_tag_helper.rb:8:in' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionview-4.2.3/lib/action_ view/helpers/form_tag_helper.rb:6:in <top (required)>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionview-4.2.3/lib/action_ view/helpers/form_helper.rb:4:inrequire' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionview-4.2.3/lib/action_ view/helpers/form_helper.rb:4:in <top (required)>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionview-4.2.3/lib/action_ view/helpers.rb:50:in' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionview-4.2.3/lib/action_ view/helpers.rb:4:in <module:ActionView>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionview-4.2.3/lib/action_ view/helpers.rb:3:in' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sprockets-rails-2.3.2/lib/sp rockets/rails/legacy_asset_tag_helper.rb:7:in <module:LegacyAssetTagHelper>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sprockets-rails-2.3.2/lib/sp rockets/rails/legacy_asset_tag_helper.rb:6:in' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sprockets-rails-2.3.2/lib/sp rockets/rails/legacy_asset_tag_helper.rb:4:in <module:Sprockets>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sprockets-rails-2.3.2/lib/sp rockets/rails/legacy_asset_tag_helper.rb:3:in' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sprockets-rails-2.3.2/lib/sp rockets/rails/helper.rb:45:in require' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sprockets-rails-2.3.2/lib/sp rockets/rails/helper.rb:45:in' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sprockets-rails-2.3.2/lib/sp rockets/rails/helper.rb:7:in <module:Rails>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sprockets-rails-2.3.2/lib/sp rockets/rails/helper.rb:6:in' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sprockets-rails-2.3.2/lib/sp rockets/rails/helper.rb:5:in <top (required)>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sprockets-rails-2.3.2/lib/sp rockets/railtie.rb:6:inrequire' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sprockets-rails-2.3.2/lib/sp rockets/railtie.rb:6:in <top (required)>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sass-rails-5.0.3/lib/sass/ra ils/railtie.rb:3:inrequire' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sass-rails-5.0.3/lib/sass/ra ils/railtie.rb:3:in <top (required)>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sass-rails-5.0.3/lib/sass/ra ils.rb:11:inrequire' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sass-rails-5.0.3/lib/sass/ra ils.rb:11:in <top (required)>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sass-rails-5.0.3/lib/sass-ra ils.rb:1:inrequire' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sass-rails-5.0.3/lib/sass-ra ils.rb:1:in <top (required)>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/r untime.rb:76:inrequire' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/r untime.rb:76:in block (2 levels) in require' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/r untime.rb:72:ineach' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/r untime.rb:72:in block in require' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/r untime.rb:61:ineach' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/r untime.rb:61:in require' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.10.6/lib/bundler.r b:134:inrequire' from C:/Users/Sprache/Sites/simple_cms/config/application.rb:7:in <top (required)>' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/com mands/commands_tasks.rb:78:inrequire' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/com mands/commands_tasks.rb:78:in block in server' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/com mands/commands_tasks.rb:75:intap' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/com mands/commands_tasks.rb:75:in server' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/com mands/commands_tasks.rb:39:inrun_command!' from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/com mands.rb:17:in <top (required)>' from bin/rails:4:inrequire' from bin/rails:4:in `'


What do these things mean and what do I do?

Grazie a tutti!

getting No route matches [POST] "/login/log_in"

im using gem parse-ruby-client and im trying to create a login. and when the login is successful then i want to go to a welcome#index

here is my login_controller.rb

class LoginController < ApplicationController
  def index

  end
  def log_in
    @user = Parse::User.authenticate(params[:user][:username], params[:user][:password])
  end
end

index.html.erb

<div class="Log_in_Form">
  <h4><center>Log in with your existing "app_name" account</center></h4>
  <%= form_for(:user, :url => {:controller => 'login', :action => 'log_in'}) do |f| %>
    <center><p> Username:</br> <%= f.text_field :username%> </p></center>
    <center><p> Password:</br> <%= f.password_field :password%></p></center>
    <center><h4><%= f.submit :Login %></h4></center>
  <% end %>
</div>

routes.rb

Rails.application.routes.draw do
  get 'welcome/index'

  root 'login#index'  

  get 'login/log_in' => 'login#log_in'
end

How to use the create method for a many:many through relationship

User and Organization have a many-to-many association through Relationship.

The Relationship model, amongst other things, specifies whether the User is a member of the Organization (member boolean).

If a new Organization together with a new User related to that Organization needs to be created I currently do the following:

Organization.create!(name: "name", ...).users.create(email: "email@email.com",...)

However, this does not yet set the member boolean in the Relationship model to true.

Is there a way to include this in the above command? Or is this only possible separately from creating the user and organization record? Because then it would require relatively a lot of code and seems a bit inefficient to add after the previous create line:

@user = User.find_by(username: username)
@organization = Organization.find_by(name: name)
@relationship = @organization.relationships.find(@user)
@relationship.update_attributes(member: true)

Travis CI Build doesn't install gems for JRuby platform

I have a travis build set up for my project that also run on JRuby. I mention the activerecord-jdbcsqlite3-adapter gem in the Gemfile for the :jruby platform:

platforms :jruby do
  gem "activerecord-jdbcsqlite3-adapter"
end

but the build still always fails with the message LoadError: Please install the sqlite3 adapter:gem install activerecord-sqlite3-adapter(sqlite3 is not part of the bundle. Add it to Gemfile.) and the gem actually doesn't get installed.

The project is open source at http://ift.tt/1LVPBO8, the build is at http://ift.tt/1I97Z3q.

Breaking a parameter and save the items separately

I need to save items separately coming from a form of a text field, but my code is saving these items duplicate form.

My controller

def create

  @answer_option = AnswerOption.break_options(answer_option_params)
  @answer_option = AnswerOption.new(answer_option_params)

  respond_to do |format|
    if @answer_option.save
      format.html { redirect_to @answer_option, notice: 'Answer option was successfully created.' }
      format.json { render :show, status: :created, location: @answer_option }
    else
      format.html { render :new }
      format.json { render json: @answer_option.errors, status: :unprocessable_entity }
    end
  end
end

My model

class AnswerOption < ActiveRecord::Base
  belongs_to :question

  def self.break_options(var)
    ugly_answers = var[:content].split /[\r\n]+/
    ugly_answers.each do |answer|
      AnswerOption.create!(content: answer)
    end
  end

end

Thanks!

I'm trying to create a new file within a folder in my rails application

I'm following along with the 'Rails Crash Course' book and it is telling me to create a file within a folder in a rails application that already exists.

I've searched the whole book but I can't find the instructions to create a file. I feel like this should be easy but I have searched online and cannot find a straight answer.

Here's an example: Create a new file named app/views/posts/index.html.erb and add the following code...

Here's another: Create the file app/views/text_posts/_text_post.html.erb and open for editing...

Anyone got the solution?