ruby

Rendering a JSON object of a join-model and its associated models

蹲街弑〆低调 提交于 2021-02-06 09:03:28
问题 In a Rails ( 4.1.5 / ruby 2.0.0p481 / win64 ) application I have a many-to-many relationship between Student and Course and a join model StudentCourse which represents the association, and has an additional attribute called started (set by default on "false"). I also have added an index in the join-table made of student_id and course_id , and set a unique check on that, like this t.index [:student_id, :course_id], :unique => true, :name => 'by_student_and_course' I wanted that to be a

How to specify “http request header” in OpenURI

纵然是瞬间 提交于 2021-02-06 06:58:53
问题 I am trying to call a URL using Ruby's OpenURI gem, however it needs me to pass certain values inside its HTTP request header. Any idea how to do this? 回答1: According to the documentation, you can pass a hash of http headers as the second argument to open : open("http://www.ruby-lang.org/en/", "User-Agent" => "Ruby/#{RUBY_VERSION}", "From" => "foo@bar.invalid", "Referer" => "http://www.ruby-lang.org/") {|f| # ... } 来源: https://stackoverflow.com/questions/7478841/how-to-specify-http-request

How to specify “http request header” in OpenURI

若如初见. 提交于 2021-02-06 06:58:12
问题 I am trying to call a URL using Ruby's OpenURI gem, however it needs me to pass certain values inside its HTTP request header. Any idea how to do this? 回答1: According to the documentation, you can pass a hash of http headers as the second argument to open : open("http://www.ruby-lang.org/en/", "User-Agent" => "Ruby/#{RUBY_VERSION}", "From" => "foo@bar.invalid", "Referer" => "http://www.ruby-lang.org/") {|f| # ... } 来源: https://stackoverflow.com/questions/7478841/how-to-specify-http-request

How to specify “http request header” in OpenURI

不问归期 提交于 2021-02-06 06:58:10
问题 I am trying to call a URL using Ruby's OpenURI gem, however it needs me to pass certain values inside its HTTP request header. Any idea how to do this? 回答1: According to the documentation, you can pass a hash of http headers as the second argument to open : open("http://www.ruby-lang.org/en/", "User-Agent" => "Ruby/#{RUBY_VERSION}", "From" => "foo@bar.invalid", "Referer" => "http://www.ruby-lang.org/") {|f| # ... } 来源: https://stackoverflow.com/questions/7478841/how-to-specify-http-request

ruby, using regex to find something in between two strings

亡梦爱人 提交于 2021-02-06 01:56:05
问题 Using Ruby + regex, given: starting-middle+31313131313@mysite.com I want to obtain just: 31313131313 ie, what is between starting-middle+ and mysite.com Here's what I have so far: to = 'starting-middle+31313131313@mysite.com' to.split(/\+/@mysite.com.*/).first.strip 回答1: Between 1st + and 1st @ : to[/\+(.*?)@/,1] Between 1st + and last @ : to[/\+(.*)@/,1] Between last + and last @ : to[/.*\+(.*)@/,1] Between last + and 1st @ : to[/.*\+(.*?)@/,1] 回答2: Here is a solution without regex (much

Rails ActiveAdmin: showing table of a related resource in the same view

我们两清 提交于 2021-02-05 20:30:31
问题 When show ing a resource using the Rails ActiveAdmin gem, I want to show a table of another associated model. So let's say a Winery has_many :products . Now I want to show the products associated on the show page of the Winery admin resource. And I want that to be a table similar to what I would get on the index of the Products resource. I got it to work, but only by recreating the HTML structure manually, which kind of sucks. Is there a cleaner way to create an index table style view for a

checking whether the C compiler works… no

爷,独闯天下 提交于 2021-02-05 20:29:25
问题 I recently uninstalled Xcode 4.2 and re-installed Xcode 4.3.1. Installed Command Line Tools also. The error says 'C compiler doesn't work`. On searching for this error, it said that it happens when Xcode is not installed. What am I missing? rvm install 1.9.3 --with-gcc=clang Installing Ruby from source to: /Users/ava/.rvm/rubies/ruby-1.9.3-preview1, this may take a while depending on your cpu(s)... ruby-1.9.3-preview1 - #fetching ruby-1.9.3-preview1 - #extracting ruby-1.9.3-preview1 to /Users

checking whether the C compiler works… no

馋奶兔 提交于 2021-02-05 20:27:54
问题 I recently uninstalled Xcode 4.2 and re-installed Xcode 4.3.1. Installed Command Line Tools also. The error says 'C compiler doesn't work`. On searching for this error, it said that it happens when Xcode is not installed. What am I missing? rvm install 1.9.3 --with-gcc=clang Installing Ruby from source to: /Users/ava/.rvm/rubies/ruby-1.9.3-preview1, this may take a while depending on your cpu(s)... ruby-1.9.3-preview1 - #fetching ruby-1.9.3-preview1 - #extracting ruby-1.9.3-preview1 to /Users

Converting an empty string into nil in Ruby

我与影子孤独终老i 提交于 2021-02-05 20:20:32
问题 I have a string called word and a function called infinitive such that word.infinitive would return another string on some occasions and an empty string otherwise I am trying to find an elegant ruby one line expression for the code-snippet below if word.infinitive == "" return word else return word.infinitive Had infinitive returned nil instead of "", I could have done something like (word.infinitive or word) But since it does not, I can't take advantage of the short-circuit OR Ideally I

Converting an empty string into nil in Ruby

試著忘記壹切 提交于 2021-02-05 20:20:23
问题 I have a string called word and a function called infinitive such that word.infinitive would return another string on some occasions and an empty string otherwise I am trying to find an elegant ruby one line expression for the code-snippet below if word.infinitive == "" return word else return word.infinitive Had infinitive returned nil instead of "", I could have done something like (word.infinitive or word) But since it does not, I can't take advantage of the short-circuit OR Ideally I