mardi 4 octobre 2016

Rails Random Number Between 0 and 3 always 0 or 1

I am using random numbers to try to call a random partial. I have the erb:

<% @random_partial = 'man_tests/test' + [*0..3].sample %>
<%= render partial: @random_partial %>

But I have also tried rand(0..3).round.to_s and the same thing happens: No matter how many times I click the button, it shows me _test0.html.erb or _test1.html.erb. I have tried enough that it would be statistically crazy if it was by chance that I only ever got these two. I even tried switching the content from _test0 and _test3 to see if it was somehow causing the problem and I still only got the ones named 0 and 1.

Can anyone help me figure out what's causing this problem?

Error session-1::#

While i was running one of my ruby script it is throwing error like

"session-1::#".And it is continuoesly in the loop only.

lundi 3 octobre 2016

Where to begin if I want to find if certain cities are near to each other if the cities are given?

I want to use Ruby and I'm not exactly sure how to write this code (I'm not well versed with API's though I know the google maps api will help here).

Say you are given a list of cities and the program you are writing has to output which two cities have the closest proximity to each other, how would this work?

ActiveModel::ForbiddenAttributesError when tried to add new comment

I have following code in app/controllers/comments_controller.rb

class CommentsController < ApplicationController
  def create
    book = Book.find params[:book_id]
    comment = book.comments.new params[:comment]
    comment.save
    flash[:notice] = 'Comment saved'
    redirect_to book_path(book)     
  end
end

I am not sure why i am getting error at line comment = book.comments.new params[:comment]

update set of fields ruby

Is it possible in Ruby (2) and Rails (3.2) to update a selection of fields in a single command. For example, I need to update a set of fields in a hook, where all the names match. I end up with something like this

  def set_address
    assign_attributes(address1: subscription.subscriber.address1,
                      address2: subscription.subscriber.address2,
                      address3: subscription.subscriber.address3,
                      city: subscription.subscriber.city,
                      county: subscription.subscriber.county,
                      country_id: subscription.subscriber.country_id,
                      postcode: subscription.subscriber.postcode)
  end

given that all the field names match across the two models is there an easier way to do this, or a way to group the fields together so that I can execute

def set_address
  address = subscription.subscriber.address
end

Any advice appreciated

dimanche 2 octobre 2016

text box not showing for author and comment

I have this code in show.html.erb when i load application it wont show text box for author and comment. not sure whats wrong.

I tried to change it to

form_for @book do |f|

But then i get error undefined method author

<h3>Post Your Comment</h3>
<% form_for([@book, Comment.new]) do |f| %>
  <p><%= f.label :author %></p>
  <p><%= f.text_field :author %></p>

  <p><%= f.label :text, 'Comment' %></p>
  <p><%= f.text_area :text %></p>

  <%= f.submit 'Save' %>
<% end %>

undefined local variable or method 'book'

I am new to rails and trying to create a sample app. I have

App/routes.rb

Rails.application.routes.draw do 
     resources :books do
        book.resources :comments, :only => :create
     #root 'books#index'
end
end

No sure why i am getting error

/bookshelf/config/routes.rb:3:in `block (2 levels) in <top (required)>': undefined local variable or method `book' for #<ActionDispatch::Routing::Mapper:0x007fdebb03e728> (NameError)