在引导程序导航栏中.您可以通过添加类来获得单击按钮的效果active
.当然,我想在我的网页上使用它.例如,如果我在关于我们的页面上,我想点击关于我们的按钮.
最好的方法是什么?我打算去每个页面,在底部有一个jQuery函数将类添加active
到它.有没有更好的办法?
我想在我的控制器中为"user_info_token"列中的用户生成一个令牌.但是,我想检查当前没有用户拥有该令牌.这段代码是否足够?
begin
@new_token = SecureRandom.urlsafe_base64
user = User.find_by_user_info_token(@new_token)
end while user != nil
@seller.user_info_token = @new_token
Run Code Online (Sandbox Code Playgroud)
或者有更清洁的方法来做到这一点?
在我的rails应用程序中,我在ajax的ajax帖子上收到"警告:无法验证CSRF令牌真实性".
app/views/layouts/application.html.haml:
!!!
%html
%head
= stylesheet_link_tag "application", :media => "all"
= javascript_include_tag "application"
= csrf_meta_tags
%body
= yield
Run Code Online (Sandbox Code Playgroud)
ajax帖子:
$.ajax({ url: '#{card_credits_path}',
type: 'POST',
beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', '#{form_authenticity_token}')},
dataType: "json",
data: { credit_uri: response.data.uri,
email: $('#email:hidden').val(),
name: $('.name').val()
},
success: function(randomobject) {
window.location = '/';
},
error: function(){
console.log("Card information is wrong!");
}
});
Run Code Online (Sandbox Code Playgroud) 我希望ajax帖子上的成功转到主页.出于某种原因,我一直做错了.知道我应该怎么做才能解决这个问题?
window.APP_ROOT_URL = "<%= root_url %>";
Run Code Online (Sandbox Code Playgroud)
阿贾克斯
$.ajax({ url: '#{addbank_bankaccts_path}',
type: 'POST',
beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', '#{form_authenticity_token}')},
dataType: "json",
data: 'some_uri=' + response.data.uri ,
success: function(APP_ROOT_URL) {
window.location.assign(APP_ROOT_URL);
}
});
Run Code Online (Sandbox Code Playgroud) 有人可以非常简单地为我解释MVC吗?我似乎无法绕过它.
大多数教程或冗长的解释都让我失望.
我正在尝试在rails中创建一个创建产品页面.这包括添加多个图像和文本字段.我有一个产品型号和一个照片.我正在使用paperclip gem进行照片上传.但是当我查看产品页面时,我没有得到任何图片.照片未保存到数据库.
PS我使用HAML.
应用程序/视图/产品/ show.html.haml
%b Name
= @product.name
%br
%b Description
= @product.description
%br
- @product.photos.each do |photo|
= image_tag photo.image.url
Run Code Online (Sandbox Code Playgroud)
应用程序/控制器/ products_controller
class ProductsController < ApplicationController
before_filter :require_login
before_filter :current_user, only: [:create, :destory]
def new
@product = Product.new
@photo = Photo.new
5.times { @product.photos.build }
end
def create
@photo = current_user.photos.build(params[:photo])
@product = current_user.products.build(params[:product])
if @product.save
render "show", :notice => "Sale created!"
else
render "new", :notice => "Somehting went wrong!"
end
end
def show
@product = Product.find(params[:id])
end …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在rails中创建一个创建产品页面.这包括添加多个图像.我有一个产品模型,一个用于照片和用户.我正在使用paperclip gem进行照片上传.但我有两个问题.
PS我使用HAML而我没有照片控制器.
产品控制器
class ProductsController < ApplicationController
before_filter :current_user, only: [:create, :destory]
before_filter :correct_user, only: :destory
def new
@product = Product.new
@photo = Photo.new
5.times { @product.photos.build }
end
def create
@photo = current_user.photos.build(params[:photo])
@product = current_user.products.build(params[:product])
if @product.save
render "show", :notice => "Sale created!"
else
render "new", :notice => "Somehting went wrong!"
end
end
def show
@product = Product.find(params[:id])
end
Run Code Online (Sandbox Code Playgroud)
创建产品页面
= form_for @product, :html => { :multipart => true } do |f| …
Run Code Online (Sandbox Code Playgroud) 我正在使用带有HAML的rails框架,我有自举设置.我如何单独格式化字段输入.我希望名称的输入字段为左侧屏幕浮动的60%,价格的输入为屏幕的25%并向右浮动.
我想我问我如何在form_for中为单个输入添加类.谢谢
= form_for @product,:url => products_path, :html => { :id => "fileupload", :multipart => true } do |f|
%p
= f.label :name
= f.text_field :name # i want to format this
%p
= f.label :price
= f.text_field :price
Run Code Online (Sandbox Code Playgroud) 我是铁杆上的红宝石.相比之下,我的HTML CSS javascript和jQuery相当不错.最近我使用Michael Hartl的这个教程进入ruby轨道:http://ruby.railstutorial.org/ruby-on-rails-tutorial-book .
但唉,我正在尝试构建自己的项目,并使用gem devise作为一种简单的身份验证方式.因为初学者很难进行身份验证.但是,每个人都提到新手不应该使用这个宝石.
为什么菜鸟不应该为自己的项目设计?
感谢任何提示,但我更喜欢冗长的答案.
我这里有一个简单的问题.我有一个带有created_at的实例变量.我将如何将其转换为 Month, day, year ~ September, 13, 1987
当我试着= @ example.created_at在我看来它给了我 1987-09-13
奇怪的是,当我在控制台中执行此方法时,我得到了 Sun, 13 Sep 1987