我正在使用导航控制器开发应用程序.我知道如何在最初加载视图时设置标题.我推了一个新的视图控制器并设置它的标题.现在的问题是,当我按下后退按钮时,原始视图控制器的标题不再存在.我试着做...
- (void)viewWillAppear:(BOOL)animated {
self.navigationController.title = @"Some Title";
}
Run Code Online (Sandbox Code Playgroud)
虽然这没有设定标题.有谁知道我能做什么?
如果我们有"example_name",我们可以使用[ActionName("")]在url中更改它所以,我想为控制器名称执行此操作.
我可以做这个:
URL中的ControllerName> example_nameController >:"/ example_controller"
我想在URL中更改这样的控制器名称:"/ example-conroller"
def update
@album = Album.find(params[:id])
if @album.update_attributes(params[:album])
redirect_to(:action=>'list')
else
render(:action=>'edit')
end
end
Run Code Online (Sandbox Code Playgroud)
我正在讨论的Rails 1.1.6教程建议使用update_attributes更新模型的方法,如上面列出的控制器的示例代码中所示.看看Rails文档,我想知道为什么这个update方法不是首选,特别是因为它的逻辑命名.
controller model ruby-on-rails updatemodel update-attributes
关于我的面包屑解决方案,我有点不安全.每个控制器操作中都定义了名称和链接:
<a href="http://localhost:3000/">Home</a>
<% if defined? @l1_link %>
> <a href="<%= @l1_link%>"><%= @l1_name %></a>
<% if defined? @l2_link %>
> <a href="<%= @l2_link%>"><%= @l2_name %></a>
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
这样我可以使用:
@l1_link = user_path()
Run Code Online (Sandbox Code Playgroud)
问题:因为我不那么聪明 - 这种系统能否在未来的某个地方引发灾难?这(严重)效率低下吗?
我有rspec测试控制器设计验证的问题.
我有以下设置
我已经包括在内
config.include Devise::TestHelpers, :type => :controller
Run Code Online (Sandbox Code Playgroud)
在我的spec_helper.rb中
在我的merchants_controller_spec.rb中
describe MerchantsController do
before :each do
@user = Factory(:user)
@merchant = Factory(:merchant, :user_id => @user.id,:is_approved => false, :is_blacklisted => false)
controller.stub!(:current_user).and_return(@user)
end
describe "GET index" do
it "assigns all merchants as @merchants" do
merchant = Factory(:merchant,:is_approved => true, :is_blacklisted => false)
get :index
assigns(:merchants).should eq([merchant])
end
end
end
Run Code Online (Sandbox Code Playgroud)
我的商人_controller.rb
class MerchantsController < ApplicationController
before_filter :authenticate_user!
def index
@merchants = Merchant.approved
debugger
respond_to do |format|
format.html # index.html.erb
format.xml { render …Run Code Online (Sandbox Code Playgroud) 希望有人能指出我正确的方向.
我有一个控制器Update def运行"update_attributes".目前它返回false,没有错误消息.我对Ruby很新,但不是编码,这让我难以忍受了好几天!我试图使用下面指定的值更新用户模型和数据库.
def update
#get currently logged in user
@user = current_user
#update user params based on edit form...
if @user.update_attributes(params[:user])
redirect_to profile_path, :notice => "Successfully updated profile."
else
render :action => 'edit'
end
end
Run Code Online (Sandbox Code Playgroud)
我的编辑def ....
def edit
@user = current_user
end
Run Code Online (Sandbox Code Playgroud)
表单将以下内容发送到此更新方法:
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ?
_method: put
authenticity_token: 9T4ihVI0p8j7pZEFxof3Bfahi2a+o3BPmtXJDnfHT4o=
user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
first_name: Amanda
last_name: Ross
is_owner: '1'
email: example@googlemail.com
commit: Update
action: update
controller: users
id: '20'
Run Code Online (Sandbox Code Playgroud)
并且params [:user]返回:
{"first_name"=>"Amanda", "last_name"=>"Ross", "is_owner"=>"1", "email"=>"example@googlemail.com"}
Run Code Online (Sandbox Code Playgroud)
所有这些字段都在模型中的attr_accessible中,我可以毫无问题地创建用户.
这里是development.log输出(对不起它有点乱)....
Started …Run Code Online (Sandbox Code Playgroud) ruby controller ruby-on-rails update-attributes ruby-on-rails-3
我正在使用Google的Preconditions类来验证用户的输入数据.
但我担心使用Preconditions类检查用户输入数据的最佳位置在哪里.
首先,我在Controller中编写了验证检查代码,如下所示:
@Controller
...
public void register(ProductInfo data) {
Preconditions.checkArgument(StringUtils.hasText(data.getName()),
"Empty name parameter.");
productService.register(data);
}
@Service
...
public void register(ProductInfo data) {
productDao.register(data);
}
Run Code Online (Sandbox Code Playgroud)
但我认为registerService层中的方法将使用另一个Controller方法,如下所示:
@Controller
...
public void register(ProductInfo data) {
productService.register(data);
}
public void anotherRegister(ProductInfo data) {
productService.register(data);
}
@Service
...
public void register(ProductInfo data) {
Preconditions.checkArgument(StringUtils.hasText(data.getName()),
"Empty name parameter.");
productDao.register(data);
}
Run Code Online (Sandbox Code Playgroud)
另一方面,服务层的方法将仅用于一个控制器.
我很困惑.哪种方法可以更好地检查控制器或服务中的前提条件?
提前致谢.
我在堆栈溢出文章中看到很多评论我发现@Transactional与@Service或@Controller一起使用的某些事情
"通常,应该在服务层进行交易."
"正常情况是在服务层级注释"
"认为事务属于服务层.它是了解工作单元和用例的人.如果你有几个DAO注入到需要在单个事务中协同工作的服务,那么这是正确的答案." [资源]
使用带有@service层的@transactional的缺点
如果我有2个方法,例如saveUser()和saveEmail()(因为我将电子邮件存储在数据库中以便稍后发送 - 就像一个队列)我会在我的服务中创建一个方法saveUserAndSendEmail(用户用户),它将是事务性的.[资源]
这意味着我在服务层创建了许多方法,而不是一个Save Generic Method,如下所示
public <T> long save(T entity) throws DataAccessException {
Session session = sessionFactory.getCurrentSession();
long getGenVal=(Long) session.save(entity);
return getGenVal;
}
Run Code Online (Sandbox Code Playgroud)
根据上面的解决方案,这意味着我们有很多方法,如以下LOL ..
public <T> long saveAccount(T entity)....
public <T> long saveWithAuditLog(T entity, K entity1)....
public <T> long saveWithAuditLogAndEntries(T entity, K entity, M entity)....
克服这种情况
我在@Controller中使用@Transactional,只需制作一个通用保存方法,并使用这个简单的保存方法保存所有实体/模型.如果任何方法无法保存,则控制器中的所有事务都会成功回滚.
确保@Transactional应与@Controller一起使用的其他情况
在@Controller中:
pt.save(entity1);
pt.save(entity2);
int a = 2/0;
pt.save(entity3);
Run Code Online (Sandbox Code Playgroud)
如果@Transactional on Service,前2个实体成功保存,但第三个不回滚所有事务
如果@Tratroller上的@Transactional,所有事务回滚都会发生异常
为什么堆栈溢出问道,"不要在控制器中进行事务处理.将它们放在服务层类中."? [资源]
是否有Magento函数从此url获取"id"的值:
http://example.com/path/action/id/123
我知道我可以在"/"上拆分url来获取值,但我更喜欢单个函数.
这不起作用:
$id = $this->getRequest()->getParam('id');
Run Code Online (Sandbox Code Playgroud)
堆垛机!我现在正在学习laravel5,我喜欢它,但我正在努力做一件事.从Laravel 5开始,我们可以在控制器的构造函数中使用中间件,如下所示:
控制器文件:
public function __construct()
{
$this->middleware('admin', ['only' => 'create']);
}
Run Code Online (Sandbox Code Playgroud)
现在我想要的是定义HERE ^(不在路径文件中)中间件,用于多个视图,如'create','edit'和'show'.确定
public function __construct()
{
$this->middleware('admin', ['only' => 'create|edit|show']);
}
Run Code Online (Sandbox Code Playgroud)
不幸的是不起作用.我宁愿不使用路线.有什么想法,亲爱的朋友们?
controller ×10
ruby ×2
service ×2
url ×2
asp.net-mvc ×1
breadcrumbs ×1
c# ×1
hibernate ×1
ios ×1
laravel ×1
magento ×1
middleware ×1
model ×1
objective-c ×1
php ×1
rspec ×1
spring ×1
spring-mvc ×1
testing ×1
updatemodel ×1
validation ×1
views ×1