我正在为我的项目使用grails 2.3.3和groovy 2.2.0版本.我工作正常,直到我决定将spring security core 1.2.7.3,ui 0.2和acl 1.1.1升级到spring security core 2.0,ui 1.0和acl 2.0.我成功升级了.但是当我尝试登录时,我收到"抱歉,您无权查看此页面." 拒绝访问消息.
我在bootstrap.groovy文件中创建了用户,如下所示.
BootStrap.groovy中
import com.vproc.member.Address;
import com.vproc.member.Profile;
import com.vproc.member.Role ;
class BootStrap {
def init = { servletContext ->
def userRole = Role.findByAuthority('ROLE_USER') ?: new Role(authority: 'ROLE_USER').save(failOnError: true)
def adminRole = Role.findByAuthority('ROLE_COMPANY_ADMIN') ?: new Role(authority: 'ROLE_COMPANY_ADMIN').save(failOnError: true)
def guestRole = Role.findByAuthority('ROLE_GUEST') ?: new Role(authority: 'ROLE_GUEST').save(failOnError: true)
def csrRole = Role.findByAuthority('ROLE_CSR') ?: new Role(authority: 'ROLE_CSR').save(failOnError: true)
//PersonRole.create adminUser, adminRole
def address = new Address( city : 'Pune' , …Run Code Online (Sandbox Code Playgroud) 我正在尝试在博客中实现编辑评论功能.我可以在文章上创建评论并显示它们.当我点击文章的特定评论的"编辑"链接时,它需要我编辑评论表单,但它不包含任何内容.就像我们编辑任何关于堆栈溢出的评论或问题一样,我们需要编辑包含内容的页面.但在我的情况下,我需要编辑评论页面但它是空的(不包含评论内容).以下是我的代码文件.
comments_controller.rb
class CommentsController < ApplicationController
before_filter :user_signed_in, except: [:create]
def new
@comment = Comment.new
end
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.build(params[:comment])
@comment.user_id = current_user.id
@comment.save
flash[:success] = "Comment created!"
redirect_to article_path(@comment.article)
end
def edit
@comment = Comment.find(params[:id])
end
def update
@comment = Comment.find(params[:id])
@article = @comment.article
respond_to do |format|
if @comment.update_attributes(params[:comment])
redirect_to @article_path(@article)
else
render :action => "edit"
end
end
def destroy
@comment = Comment.find(params[:id])
@article = Article.find(params[:article_id])
@comment.destroy
redirect_to @article_path(@artilce)
end
end
Run Code Online (Sandbox Code Playgroud)
评论/ edit.html.erb
<h3>Editing comment</h3> …Run Code Online (Sandbox Code Playgroud) 当我点击[http://127.0.0.1:3000/auth/twitter]时,我在rails中收到OAuth :: Unauthorized 401 Unauthorized错误.我正在使用我的rails应用程序跟踪Railscast视频#241以进行Twitter身份验证.我已经google了很多,但找不到答案.
Info regarding app on twitter:
Callback URL: [http://127.0.0.1:3000/auth/twitter/callback]
Website: [http://127.0.0.1:3000]
### omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
# provider :developer unless Rails.env.production?
provider :twitter, ENV['75UOAIDmKrRXvXKBhNvKA'], ENV['GrIaBI0tQy2TtjOtaFL9VxT6s9qq1sV7h9yRaZW4A']
end
### routes.rb
Chilli::Application.routes.draw do
resources :posts
root :to => 'posts#index'
#match '/auth/:twitter/callback' => 'sessions#create', :as => :auth_callback
match 'auth/twitter/callback', to: 'sessions#create'
end
### application.html.erb
<div id="user_nav">
<%= link_to "Sign in with Twitter", "/auth/twitter"%>
</div>
### sessions_controller.rb
class SessionsController < ApplicationController
def create
user = User.from_omniauth(env['omniauth.auth'])
session[:user_id] = user.id
redirect_to root_url, …Run Code Online (Sandbox Code Playgroud) 我正在实现标签功能,文章可能有一个到多个标签.我能够以这种格式从db获取标记值
["social network", "professional"]
Run Code Online (Sandbox Code Playgroud)
我希望以这种格式输出
"social network professional"
Run Code Online (Sandbox Code Playgroud)
我想将数组转换为字符串而不用,.下面是一个代码片段,它将db中的值作为数组取出.
<%= article.tags.collect(&:name) %>
Run Code Online (Sandbox Code Playgroud)
如何将此输出转换为字符串值而不使用任何逗号?
我想获取在博客应用程序中创建文章的用户的用户名或电子邮件(都在用户表中).目前,我可以从articles_controller.rb获取用户ID
def create
@article = Article.new(params[:article])
@article.user_id = current_user.id
@article.save
redirect_to article_path(@article)
end
Run Code Online (Sandbox Code Playgroud)
但不知道如何获取用户名或电子邮件.基本上我想在文章索引页面上显示用户名或电子邮件.请建议我如何完成它
user.rb
class User < ActiveRecord::Base
has_many :articles
has_many :comments
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :username, :email, :password, :password_confirmation, :remember_me
attr_accessible :title, :body
end
Run Code Online (Sandbox Code Playgroud)
article.rb
class Article < ActiveRecord::Base
attr_accessible :title, :body
has_many :comments
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
articles_controller.rb
class …Run Code Online (Sandbox Code Playgroud) 我有订阅者和联系人域,订阅者可以在grails应用程序中拥有多个联系人(一对多).当我尝试从表联系人中删除数据时,它会抛出错误,如DBCExceptionReporter Cannot delete or update a parent row: a foreign key constraint fails (vprocure5 .subscriber_contact , CONSTRAINTFKC5D3AF49E9F29F5 FOREIGN KEY (contact_id ) REFERENCEScontact (id )).
根据错误消息,我无法删除父行,但实际上我正在尝试删除作为"订阅者"域的子项的联系人数据.如果我没有错,那么订阅者应该是父母,联系人应该是子域名.
订户域
static hasMany= [contacts: Contact ]
Run Code Online (Sandbox Code Playgroud)
联系域名
static belongsTo = [Subscriber ]
Run Code Online (Sandbox Code Playgroud)
ContactController.grooby
package com.vproc.member
import org.springframework.dao.DataIntegrityViolationException
class ContactController {
def springSecurityService
def subscriberService
def imageUploadService
def searchableService
def autoCompleteService
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
def index() {
redirect(action: "list", params: params)
} …Run Code Online (Sandbox Code Playgroud) 我试图在文章的显示页面上显示相关文章.当用户查看任何特定文章时,db中的所有相关文章应该根据标记显示在该页面上(在右侧栏中)(因为每篇文章至少有一个标签).我的应用程序在标签和文章之间有关系(请在下面) .
articles_controller.rb
class ArticlesController < ApplicationController
before_filter :is_user_admin, only: [:new, :create, :edit, :destroy]
def is_user_admin
redirect_to(action: :index) unless current_user.try(:is_admin?)
return false
end
def index
@articles = Article.all(:order => "created_at DESC")
@article_titles = Article.first(10)
@tags = Tag.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def create
@article = Article.new(params[:article])
@article.user_id = current_user.id
if @article.save
flash[:success] = "article created!"
redirect_to article_path(@article)
else
render 'new'
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to action: 'index' …Run Code Online (Sandbox Code Playgroud) 我尝试在视图中显示验证错误以创建新文章页面.我在文章模型中验证了检查正文和标题的存在(验证:title,:body,:presence => true).当我保留文章和标题文本框但显示"模板丢失"错误时,它不允许创建新文章,以下信息.
Missing template articles/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "F:/kuta/billi/app/views" * "C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/twitter-bootstrap-rails-2.2.6/app/views" * "C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/devise-2.2.3/app/views"
Run Code Online (Sandbox Code Playgroud)
我已将<%= f.error_messages%>部分放在文章的新页面中,并将gem'vynamic_form'放在gemfile中.
_form.html.erb for article/new.html.erb
<%= form_for @article, :html => { :class => '' } do |f| %>
<%= f.error_messages %>
<div>
<%= f.label :title, :style => "margin-top:10px;" %>
<div>
<%= f.text_field :title, :style => "width:730px; height:30px; border: 1px solid #66c9ee;margin-top:10px; background-color:#FFFFFF;" %>
</div>
</div>
<div>
<%= f.label :body, :class => 'control-label' %>
<%= f.text_area :body, :style => "width:730px; …Run Code Online (Sandbox Code Playgroud)