在Rails 3中更新之前,需要检查属性块是否已更改.
street1,street2,city,state,zipcode
我知道我可以使用类似的东西
if @user.street1 != params[:user][:street1]
then do something....
end
Run Code Online (Sandbox Code Playgroud)
但这段代码真的很长.有更干净的方式吗?
无法更新生产服务器上的gem.
我试着bundle install --deployment和bundle install --without development test
但继续得到:
You are trying to install in deployment mode after changing
your Gemfile. Run `bundle install` elsewhere and add the
updated Gemfile.lock to version control.
If this is a development machine, remove the Gemfile freeze
by running `bundle install --no-deployment
Run Code Online (Sandbox Code Playgroud)
我不知道这是否正确,但需要快速修复.我跑了bundle install --no-deployment然后bundle update又跑bundle install --deployment了
在Linode,RVM,Rails 3,带乘客模块的Apache,运载波和迷你魔法上运行Ubuntu 10.04
我明白了:
Rails Error: Unable to access log file. Please ensure that /srv/www/mysite.com/testapp/log/production.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
Run Code Online (Sandbox Code Playgroud)
和 Errno::EACCES (Permission denied /srv/www/mysite.com/testapp/public/uploads/tmp/20110517-1707-2938-6455):
我跑了 chmod -R root:root /srv/www/mysite.com/testapp
然后:chmod -R www-data:www-data /srv/www/mysite.com/testapp&chmod -R www-data:www-data /srv/www/mysite.com/testapp/public/uploads
由于只有2个应该可写的目录是日志文件和上传目录,我试图确保其余的.我还需要其他任何文件夹/文件可写吗?
以前问过相似的.
但无法从用户表单中获得与就业关系的关系.
已经阅读http://www.justinball.com/2008/07/03/checkbox-list-in-ruby-on-rails-using-habtm/ 和http://millarian.com/programming/ruby-on-rails/quick-tip-has_many-through-checkboxes /(我真的希望它有效.)
表格提交,但仅在就业方面创建空白记录.
<%= form_for @user do |f| %>
...
<p>
<% Company.all.each do |company| %>
<%= check_box_tag :company_ids, company.id, @user.companies.include?(company), :name => 'user[company_ids][]' -%>
<%= label_tag :companies_ids, company.id %>
<% end %>
</p>
<p><%= f.submit %></p>
<% end %>
Run Code Online (Sandbox Code Playgroud) 使用sqlite3进行本地开发.Prod DB是MySql.
有一个用于列更改的迁移文件.
class ChangeDateToOrders < ActiveRecord::Migration
def self.up
change_column(:orders, :closed_date, :datetime)
end
def self.down
change_column(:orders, :closed_date, :date)
end
end
Run Code Online (Sandbox Code Playgroud)
错误说出来 index name 'temp_index_altered_orders_on_closed_location_id_and_parent_company_id' on table 'altered_orders' is too long; the limit is 64 characters
知道sqlite对索引名称有限制,但有没有解决方法呢?
编辑我使用的解决方法.
class ChangeDateToOrders < ActiveRecord::Migration
def self.up
remove_index(:orders, [:closed_location_id, :parent_company_id])
change_column(:orders, :closed_date, :datetime)
add_index(:orders, [:closed_location_id, :parent_company_id], :name => "add_index_to_orders_cli_pci")
end
def self.down
remove_index(:orders, :name => "add_index_to_orders_cli_pci")
change_column(:orders, :closed_date, :date)
add_index(:orders, [:closed_location_id, :parent_company_id])
end
end
Run Code Online (Sandbox Code Playgroud) 想要确保只有登录的用户可以查看/下载从carrierwave上传的文件.
这是怎么做到的?
将商店目录从公共目录移动到RAILS_ROOT,创建显示和下载的路径.
问题是如果它是一个图像,<%= image_tag(photo.image_url) %>我得到完整路径/Users/myname/projects/appname/files/image/id/image.png,这样就不会渲染.
此外,如果视图不会渲染我的绝对路径,只是来自网站的相对路径,那将是很好的.
在尝试运行迁移以添加空间索引时,请获取
Unknown key: spatial/Users/ME/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.2/lib/active_support/core_ext/hash/keys.rb:70:in `block in assert_valid_keys'
Run Code Online (Sandbox Code Playgroud)
运用
迁移索引文件看起来像
class CreateAddresses < ActiveRecord::Migration
def change
create_table :addresses, :options => 'ENGINE=MyISAM' do |t|
t.string :street_1
t.string :street2
t.string :city
t.string :state
t.string :zip
t.string :country
t.string :full_address
t.column :latlon, :point, :null => false
t.timestamps
end
add_index :addresses, :latlon, :spatial => true
end
end
Run Code Online (Sandbox Code Playgroud)
UPDATE
当我将database.yml文件中的适配器从mysql2更改为mysql2spatial时,更正了此错误和其他错误
我的项目的最后一部分,希望.
需要检查user.email属性是否更改.如果是,那么需要告诉mailchimp更改或添加电子邮件.
看起来Dirty会这样做,但以前从未使用过它.如何捕获块中的更改,或将其传递给块,然后更新属性?
有问题解决这个问题.
试图做一个
rescue_from NoMethodError, :with => :try_some_options
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
编辑:为了测试我正在做一个简单的重定向
def try_some_options
redirect_to root_url
end
Run Code Online (Sandbox Code Playgroud)
编辑2:我的控制器示例.添加(例外),如下所示.
我知道我收到错误的原因.使用Authlogic和authlogic_facebook_connect插件.当从facebook插件创建用户时,如果用户在本地注册,则不会创建与用户相关联的"MyCar"模型.因为我确实调用了用户模型并在网站的不同部分引用了用户汽车,所以我想做一些类似于你在下面看到的内容并最终将它放在我的application_controller中.
class UsersController < ApplicationController
before_filter :login_required, :except => [:new, :create]
rescue_from NoMethodError, :with => :try_some_options
...
def show
store_target_location
@user = current_user
end
def create
@user = User.new(params[:user])
if @user.save
MyCar.create!(:user => @user)
flash[:notice] = "Successfully created profile."
redirect_to profile_path
else
render :action => 'new'
end
end
...
protected
def try_some_options(exception)
if logged_in? && current_user.my_car.blank?
MyCar.create!(:user => current_user)
redirect_to_target_or_default profile_path
end
end
...
end …Run Code Online (Sandbox Code Playgroud) 使用http://blog.bernatfarrero.com/in-place-editing-with-javascript-jquery-and-rails-3/
gem使用json进行更新,但是如何触发update.js.erb来更新页面的不同部分?
编辑在发票页面中使用此功能.发票中的每个项目都有一个可以使用best_in_place更新的价格字段.
只有在字段成功更新后,我才需要更新订单项的总价格和发票到期金额.
结束了类似的事情:
respond_to do |format|
if @item.update_attributes(params[:item])
format.html { redirect_to order_url(@item.order_id), :notice => "Successfully updated item." }
format.js { }
format.json { head :ok }
Run Code Online (Sandbox Code Playgroud)
编辑best_in_place.js第175行
loadSuccessCallback : function(data) {
this.element.html(data[this.objectName]);
// Binding back after being clicked
$(this.activator).bind('click', {editor: this}, this.clickHandler);
if(this.objectName == "item") $.getScript('update.js'); // If its an item, call update.js.erb
},
Run Code Online (Sandbox Code Playgroud)