ruby-on-rails-3.2

Rails 3.2.8 insert or update based on condition

亡梦爱人 提交于 2019-12-13 01:04:44
问题 I am new to Rails and using this code to update or insert. user = User.find_by_email(params[:email]) if user.nil? User.create!(params) else user.save(params) end // params is a hash with keys as table columns This code is not working. Also, I would like to know if Rails has something magical to do this in one line ? I've not declared email as primary key but its going to be unique. Will it help me to declare it as primary ? 回答1: Your code doesn't work because the parameter to save is as a

haml, blocks and partials

╄→гoц情女王★ 提交于 2019-12-13 00:39:18
问题 To clean and factor ugly views, I'd like to do the following: 1) In the view: = document_left_container do = document_information 2) In my helper: def document_left_container(&block) render partial: "/document_left_container", locals: { custom_block: block } end def document_information render partial: "document_information" end 3) Partials: For document_left_container: .foo = custom_block.call For document_information: .bar 4) The expected result: <div class='foo'> <div class='bar'> </div> <

Need to convert a Boolean from Postgres (== String) to a Ruby Boolean

萝らか妹 提交于 2019-12-13 00:29:18
问题 I'm using Postgres with Rails. There's a query with a subselect which returns a boolean, but Postgres always returns a String like 't' or 'f' . But in the generated JSON I need a real boolean. This is my query: SELECT *, EXISTS ( SELECT TRUE FROM measurement JOIN experiment ON measurement.experiment_id = experiment.id JOIN location ON experiment.location_id = location.id WHERE location.map_id = map.id LIMIT 1 ) AS measurements_exist FROM "map" It doesn't matter whether I use THEN true or THEN

error message when uploading file in ruby on rails

大城市里の小女人 提交于 2019-12-13 00:23:26
问题 The file uploading is working properly using ruby on rails. Now i need to validate my uploading. It should validate and display error message when no file is selected after clicking upload. Please help me with a code for this error validation. My view file is (uploadfile.html.erb): <h1>File Upload</h1> <%= form_tag({action: :uploadFile}, multipart: true) do %> <p><label for="upload_file">Select File</label> <%= error_messages_for :data_file %> <%= file_field 'upload', 'datafile' %></p> <%=

Upgrading to Rails 3.2.8 causes stack level too deep error with ActiveRecord

懵懂的女人 提交于 2019-12-12 22:48:37
问题 I am fairly new to Ruby however I am running into an issue which I am having trouble getting to the bottom of. My simple application was working fine and then, after upgrading the Rails version to 3.2.8 I hit an issue when trying to query my database for records. This works fine when using Rails 3.0.7 but after upgrading to rails 3.2.8 it starts to fail with a Stack Level too deep error. My model looks like this: class Resource < ActiveRecord::Base validates :title, :presence => true, :length

Rails Engines - simple possible engine to (1) add a model and (2) add the association in the containing class

这一生的挚爱 提交于 2019-12-12 21:23:24
问题 I am trying to write my first engine but am having problems with the following scenario. In the host app, I will have User model (this is guaranteed so in the engine I can refer to User class rather than some level of indirection) which has a name. In the engine, I will have a post model and need to create an association between the post model and the user model in the containing app. >rails plugin new abc --mountable ... >rails g model Post header:string body:text user_id:integer ... edit

Rails: Include all of model A and only a subset of related model B

让人想犯罪 __ 提交于 2019-12-12 19:51:25
问题 I'm trying to understand the rails database API better. If I have the following models: class Link has_many :votes belongs_to :user end class Vote belongs_to: link belongs_to :user end class User has_many :links has_many :votes end I want to "eagerly" return a list of ALL links and for each link I want the votes where the user_id equals the current user. I tried this: l = Link.all(:include => :votes, :conditions => { :votes => { :user_id => current_user.id}}) But that only returns a list of

morris.js on rails 3.2.1 cannot get json from html

☆樱花仙子☆ 提交于 2019-12-12 19:50:07
问题 I have a strange error on the morris.js graphing library this works in graphs.js $(document).ready(function() { Morris.Line({ element: 'annual', data: [ {y: '2012', a: 100}, {y: '2011', a: 75}, {y: '2010', a: 50}, {y: '2009', a: 75}, {y: '2008', a: 50}, {y: '2007', a: 75}, {y: '2006', a: 100} ], xkey: 'y', ykeys: ['a'], labels: ['Series A'] }); }) this does not graphs.js $(document).ready(function() { Morris.Line({ element: 'annual', data: $('#logs_chart').data('logs'), xkey: 'y', ykeys: ['a'

Make output of diff-lcs human readable

允我心安 提交于 2019-12-12 18:17:40
问题 I'm using the diff-lcs gem to output a difference between two bodies of html content. Here's the sample content. Version one: <p>Paragraph one. Sentence one.</p> <p>Paragraph two. Another sentence.</p> <p>Paragraph three. I dare you to change me!</p> Version two: <p>Paragraph one. Sentence two.</p> <p>Paragraph two. Another sentence.</p> <p>Paragraph three. I dare you to update me!</p> Using this: seq1 = @versionOne.body seq2 = @versionTwo.body seq = Diff::LCS.diff(seq1, seq2) You get this

Modal from Dropdown link in Twitter Bootstrap

扶醉桌前 提交于 2019-12-12 15:46:01
问题 I am trying to have a modal launch from a link that is in my dropdown menu. The modal seems to launch (the site goes grey) but it can't be seen. Modals from regular links not in the dropdown work just fine. I have fooled with the jquery but nothing has worked out since I am new to jquery. Here is my site code. <nav class="pull-right"> <ul class="nav"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">My Item<b class="caret"></b></a> <ul class="dropdown-menu">