我目前尝试将rails 3.2应用程序切换到rails 4.0.但我对has_and_belongs_many模型有一个问题.
我创建了一个测试应用程序,我遇到了同样的问题.这就是我所做的:
创建了两个模型:foo_clip和foo_url
class FooClip < ActiveRecord::Base
has_and_belongs_to_many :foo_urls
attr_accessible :id, :name
end
class FooUrl < ActiveRecord::Base
has_and_belongs_to_many :foo_clips
attr_accessible :url
end
Run Code Online (Sandbox Code Playgroud)
在此之后,我更新了迁移文件:
class CreateFooClips < ActiveRecord::Migration
def change
create_table :foo_clips do |t|
t.string :name
t.timestamps
end
end
end
class CreateFooUrls < ActiveRecord::Migration
def change
create_table :foo_urls do |t|
t.string :url
t.timestamps
end
end
end
Run Code Online (Sandbox Code Playgroud)
现在我已经为has_and_belongs_to_many表创建了迁移文件
class CreateFooClipsFooUrls < ActiveRecord::Migration
def change
create_table :foo_clips_foo_urls do |t|
t.belongs_to :foo_url
t.belongs_to :foo_clip
end
end
end
Run Code Online (Sandbox Code Playgroud)
最后一步,我创建了一个用于测试的种子文件:
foourl1 = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用此命令打开凭据文件。
rails credentials:edit
Run Code Online (Sandbox Code Playgroud)
它返回:
没有 $EDITOR 来打开文件。像这样分配一个:
EDITOR="mate --wait" bin/rails 凭证:编辑
对于立即 fork 和退出的编辑器,传递一个等待标志很重要,否则凭据将立即保存而没有机会进行编辑。
所以我做了这个命令:
EDITOR="subl --w" bin/rails credentials:edit
Run Code Online (Sandbox Code Playgroud)
但是,终端会在不打开编辑器的情况下以“加密并保存的新凭据”作为响应。
我有以下代码,我得到了奇怪的错误.
function export_to_excel()
{
results_html = $('report_excel').innerHTML;
results_html = '<html><body><table align="center" cellspacing="0" cellpadding="0" width="100%"><tr><td colspan="9"><b>Employee Salary Register </b></td></tr><tr><td colspan="9"></td></tr>' + results_html + '</table></body></html>';
var input = new Element('input', {
'type': 'hidden',
'name': 'results[html]',
'value': results_html
});
var form = new Element('form', {
'method': 'post',
'name': 'Employee Salary Register',
'action': "html_to_excel"
});
form.insert(input);
document.body.appendChild(form);
form.submit();
}
Run Code Online (Sandbox Code Playgroud)
错误发生在此行
results_html = '<html><body><table align="center" cellspacing="0" cellpadding="0" width="100%"><tr><td colspan="9"><b>Employee Salary Register </b></td></tr><tr><td colspan="9"></td></tr>' + results_html + '</table></body></html>';
Run Code Online (Sandbox Code Playgroud)
编辑:
错误显示在指向该行的Firebug控制台中. 我只是附加了一个屏幕截图
在那里你可以看到,在两个按钮(打印和导出)下,代码出现在屏幕上.这些代码行是功能的一部分 export_to_excel()
它在我的本地服务器中也能很好地工作.我正在使用 …
我有一个使用Devise和Omniauth的现有Rails 4应用程序.我正在尝试修改我现有的应用程序来开发一个API(我的意思是我将拥有我的网络应用程序,我也想要一个移动应用程序的API),但我运气不好.
我正在按照Rails-API中 "已存在的应用程序"部分中列出的步骤进行操作,现在我收到以下错误
undefined method `flash' for #<Devise::SessionsController:0x000000098221d0>
Run Code Online (Sandbox Code Playgroud)
错误在该行弹出<% unless flash.blank? %>的views/devise/sessions/new.html.erb
我试过投入config.middleware.use ActionDispatch::Flash,/config/application.rb但错误存在.
我也有config.api_only = false在/config/application.rb
以下是我的代码
#app/controllers/application_controller.rb
class ApplicationController < ActionController::API
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
include ActionController::RequestForgeryProtection
include Pundit
include ActionController::Serialization
include ActionView::Layouts
include ActionController::ImplicitRender
protect_from_forgery with: :null_session
----some other code------
end …Run Code Online (Sandbox Code Playgroud) 我正在尝试将名为 blazer 的 Gem 与我的 Rails 应用程序集成,并且必须在 blazer.yml 文件中指定 mysql 数据库 URL,以便它可以访问临时和生产环境中的数据。
我相信定义 MySQL 数据库 URL 的标准格式是
mysql2://user:password@hostname:3306/database
Run Code Online (Sandbox Code Playgroud)
我以与字符串相同的格式定义了 URL,当我验证 URI 时,出现以下错误
URI::InvalidURIError:错误的 URI(不是 URI?):mysql2://f77_oe_85_staging:LcCh%264855c6M;kG9yGhjghjZC?JquGVK@factory97-aurora-staging-cluster.cluster-cmj77682fpy4kjl.us-east-1.rds.amazonaws。 com/factory97_oe85_staging
定义Mysql数据库URL:
'mysql2://f77_oe_85_staging:LcCh%264855c6M;kG9yGhjghjZC?JquGVK@factory97-aurora-staging-cluster.cluster-cmj77682fpy4kjl.us-east-1.rds.amazonaws.com/factory97_oe85_staging'
Run Code Online (Sandbox Code Playgroud)
请指教
我会尝试以简单的方式.我在页面上有一个注册按钮 (重定向到/users/sign_up页面)/visitors/owner-faq.注册后我想要URL这个页面(/visitors/owner-faq),以便我可以使用它来设置类似的内容
if sign_up_from_url == '/visitors/owner-faq'
@user.role = 'Owner'
end
Run Code Online (Sandbox Code Playgroud)
我试着request.referer在控制器中使用,但它/users/sign_up没有给我/visitors/owner-faq.然而,当我<%= url_for(:back) %>在注册视图页面中使用时,它给了我/visitors/owner-faq我想要的.但是我无法弄清楚如何在控制器中访问它.任何人都可以建议我如何在控制器中访问该值?我正在使用Devise进行注册.
我的问题似乎与 heroku 上的任何其他经典资产管道问题一样,但没有一个解决方案对我有用。
题:
如何解决这个问题呢?
现在的情况:
我创建了一个自定义布局来包含我的供应商资产。下面是代码
#app/views/layouts/gentellela_theme.html.erb
<!DOCTYPE html>
<html>
<head>
<title>HMS</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= stylesheet_link_tag "css/custom", media: "all", "data-turbolinks-track" => true %>
<%= stylesheet_link_tag "fullcalendar.print.min", :media => "print", 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag "dataTables.bootstrap4.min", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= javascript_include_tag "gentellela_theme", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body class="nav-md">
<%= render "layouts/sidenav" %> …Run Code Online (Sandbox Code Playgroud) 抱歉问了个愚蠢的问题
在这里,当我加注时,@property我会争吵,但是当我加注时,@property.id它显示undefined method id给我对此的结论
在owner.rb
has_many :properties
Run Code Online (Sandbox Code Playgroud)
在property.rb
belongs_to :owner
Run Code Online (Sandbox Code Playgroud)
在owner_controller.rb中
def new
user = User.friendly.find(current_user.slug)
@owner = user.owner
authorize @owner
@property = Property.where(owner_id: @owner)
#raise @property.id.inspect
@renter = User.friendly.find(params[:renter_id]).renter
@message = Message.new(renter_id: @renter.id, owner_id: @owner.id,property_id: @property.id)
end
Run Code Online (Sandbox Code Playgroud)
提前致谢
我是 Ruby On Rails 的新手。我观看了许多教程以明确 Rails 路由的工作原理。我仍然无法理解,如果 rails 将 URL 与GET路由中的命令匹配,然后转到控制器查看操作,那么home/index当我的控制器根本没有操作时,它如何打开我的控制器的 home 位置。
我的家庭控制器
class HomeController < ApplicationController
layout false
end
Run Code Online (Sandbox Code Playgroud)
我的路线文件:
Rails.application.routes.draw do
root 'home#index'
get 'home/index'
end
Run Code Online (Sandbox Code Playgroud) 我有一个Place使用 STI 约定的基类和多个子类。我有一个单独的模型Post,它belongs_to是以下子类之一Place:
class Place < ApplicationRecord
end
class SubPlace < Place
has_many :posts, class_name: "SubPlace", foreign_key: "sub_place_id"
end
class Post < ApplicationRecord
belongs_to :sub_place, class_name: "SubPlace", foreign_key: "sub_place_id"
end
Run Code Online (Sandbox Code Playgroud)
可以使用 Rails 控制台保存新记录,但在尝试查找特定记录Post时出现以下错误:PostsSubPlace
ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column places.sub_place_id does not exist)
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点,或者我的关联必须仅与基类相关?
添加架构:
create_table "posts", force: :cascade do |t|
t.string "title"
t.bigint "sub_place_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["sub_place_id"], name: "index_posts_on_sub_place_id"
end
create_table "places", …Run Code Online (Sandbox Code Playgroud) associations ×2
devise ×2
amazon-s3 ×1
heroku ×1
html ×1
javascript ×1
mysql ×1
mysql2 ×1
postgresql ×1
prototypejs ×1
rails-api ×1
ruby ×1