我一直在尝试找出阻止访问 Rails 3 应用程序(少量 IP 地址和几个 IP 子网除外)的正确方法。
在寻找方法时,我发现了这个问题/答案。建议代码如下:
应用控制器
before_filter :protect
def protect
@ips = ['127.0.0.1', '203.123.10.1'] #And so on ...]
if not @ips.include? request.remote_ip
# Check for your subnet stuff here, for example
# if not request.remote_ip.include?('127.0,0')
render :text => "You are unauthorized"
return
end
end
Run Code Online (Sandbox Code Playgroud)
这是有效的,所以我将其更改为重定向到静态页面而不仅仅是文本消息。
不过,我想做的是允许从与 Rails 应用程序服务器位于同一子网上的本地 IP 进行访问。子网很简单192.168.1.0/24。
将子网添加到接受的 IP 的最简单/最干净的方法是什么?
因此,我正在尝试创建一个网站,您可以在其中输入名为 Info 的对象的标题和价格。当我输入标题和价格并按提交时,它被重定向到的页面显示“找不到 InfosController 的操作\'更新\'”。我希望它闪烁一条消息,通知用户对象已成功创建。有什么帮助吗?
\n\n控制器/infos_controller.rb
\n\nclass InfosController < ApplicationController\n def index\n @info = Info.new\n end\n\ndef create\n @info = Info.new(params[:info])\n\nif @info.save\n flash[:notice] = \'Object created successfully, thank you\'\n render :index\n else\n flash[:notice] = \'Sorry! Object was not created successfully\'\n render :new\n end\nend\n def update\n @info = Info.find(params[:id])\n if @info.update_attributes(params[:info])\n # if update is successful\n else \n # if update is unsuccessful\n end\n redirect_to infos_path\nend\nend\nRun Code Online (Sandbox Code Playgroud)\n\n视图/infos/index.html.erb
\n\n<h1>Please enter information about an Info object</h1>\n<%= form_for @info, url: {action: …Run Code Online (Sandbox Code Playgroud) 我有一个简单但也许愚蠢的问题。
使用 Symfony2 PHP 框架,我经常扩展控制器,如下所示(当然这取决于工作类型):
class MainController extends Controller{
private $locale = array();
protected function Locale() {
$em = $this->getDoctrine()
->getManager();
$this->locale = $em->getRepository('CommonLanguageBundle:Language')
->findBy(
array('code' => $this->getRequest()
->getLocale()
)
);
// \Doctrine\Common\Util\Debug::dump($this->locale);
return $this->locale[0];
}
//..
}
class StoreController extends MainController{
function a_method() {
$data = $this->Locale()->getId();
//...
}
}
class DefaultController extends StoreController {
$data = $this->Locale()->getId();
//...
}
Run Code Online (Sandbox Code Playgroud)
这是一个好的做法吗?
在网上冲浪我发现了很多文章,但对我来说仍然不太清楚。
最后,如果它在 Symfony2 中运行良好,那么它对于 MVC 模式来说通常会好吗?
我正在关注这个 AngularJS+ASP.NET 教程,他们使用了$scope,但我试图用新语法替换过时的用法controller,如这个问题中所述:“AngularJs“控制器作为”语法 - 澄清?”
我所做的目前不起作用。页面在函数$http.get内部调用nextQuestion(),但视图保持不变,只有标题"loading question..."。
我的代码:
JS http://pastebin.com/RfngRuZD
var app = angular.module('QuizApp', [])
app.controller('QuizCtrl', ['$http', function ($http) {
this.answered = false;
this.title = "loading question...";
this.options = [];
this.correctAnswer = false;
this.working = false;
this.answer = function () {
return this.correctAnswer ? 'correct' : 'incorrect';
};
// GET
this.nextQuestion = function () {
this.working = true;
this.answered = false;
this.title = "loading question...";
this.options = …Run Code Online (Sandbox Code Playgroud) 我有一个模型,其中包含与之相关的各种不同类型的信息。
Opportunity.rb
has_many :notes
has_many :emails
has_many :calls
Run Code Online (Sandbox Code Playgroud)
我目前在显示页面上有 3 个表,在我调用的控制器中
@notes = @opportunity.notes.sorted
@emails = @opportunity.emails.sorted
@calls = @opportunity.calls.sorted
Run Code Online (Sandbox Code Playgroud)
并将它们显示在3个表中
我想按时间顺序对它们进行排序,这样我就可以得到一个时间表。
每个模型都有日期,我想按日期排序,然后按日期顺序返回对象。即表格如下:
Note | Content | 11-Feb | Bob
Call | Content | 07-Feb | Dave
Email | Content | 04-Feb | Steve
Call | Content | 03-Feb | Dave
Note | Content | 01-Feb | Margaret
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何将控制器中的这些表合并到可以在视图中使用的对象中,以便我可以创建一个表,按日期(“created_at”)排序,并仍然保持一切看起来漂亮干净。
我有一个使用数据脚手架生成的视图。该视图有一个文本字段:
创建视图:
<div class="form-group">
@Html.LabelFor(model => model.GroupId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.GroupId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.GroupId, "", new { @class = "text-danger" })
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我想将一个值从控制器传递到此文本字段。我做了以下,这似乎不起作用。
控制器:
public ActionResult Create(int id)
{
ViewBag.GroupId = id;
Debug.WriteLine("DEBUG: "+id);
return View();
}
Run Code Online (Sandbox Code Playgroud) 这是我目前用来创建控制器和模型的
php artisan make:controller API/name_of_controller --api --model=name_of_model
Run Code Online (Sandbox Code Playgroud)
然后创建迁移
php artisan make:migration create_users_table
Run Code Online (Sandbox Code Playgroud)
过去在我开始使用 API 之前,我曾经这样做过在一行中创建模型、迁移和控制器
php artisan make:model Banana -mcr
Run Code Online (Sandbox Code Playgroud)
有没有办法用 API 控制器做到这一点?
我们最近从 ember 2+ 转移到 Ember 3.18.0,我正在努力从组件调用控制器功能。在以前的版本中,我们使用 sendAction 来冒泡动作,但现在由于 sendAction 已贬值并且正在使用闭包,我无法正确执行。
下面是我的代码
控制器.hbs
{{generic-err-modal err=receivedErr showDialog= this.showErrorModal onSave=(action "closePromptDialog")}}
Run Code Online (Sandbox Code Playgroud)
控制器.js
@action
closePromptDialog(){
this.set("showErrorModal",false);
}
Run Code Online (Sandbox Code Playgroud)
组件.hbs
{{#if @showDialog}}
<PaperDialog id="genericModal" class="flex-50" @fullscreen={{fullscreen}} @onClose={{action "closePromptDialog"}} @origin={{dialogOrigin}}>
<PaperDialogContent class="text-align-center">
{{paper-icon "remove_circle_outline" class="red" size=48}}
</PaperDialogContent>
<PaperDialogContent>
<h2>{{@err.errorMessage}}</h2>
</PaperDialogContent>
<PaperDialogActions @class="layout-row">
<span class="flex"></span>
<PaperButton @primary={{true}} @onClick={{action "hideModal"}} @raised={{true}}>Ok</PaperButton>
</PaperDialogActions>
</PaperDialog>
{{/if}}
Run Code Online (Sandbox Code Playgroud)
组件.js
@action
hideModal(){
this.args.onSave();
}
Run Code Online (Sandbox Code Playgroud)
在这我收到错误
Uncaught TypeError: method is not a function
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。
我使用的 Ember 版本是 3.18.0
我正在使用 Hibernate 在 Spring Boot 中开发 REST API。
我的控制器中有这个功能
@PostMapping("/profile")
public ResponseEntity<String> saveProfile(@Valid @RequestBody SaveProfileVM saveProfileVM,
BindingResult bindingResult)
throws JsonProcessingException {
if (bindingResult.hasErrors()) return super.fieldExceptionResponse(bindingResult);
Profile profile;
boolean optimisticLockException = true;
int retryCount = 0;
do {
try {
profile = accountService.saveProfile(saveProfileVM.getAccountId(),
saveProfileVM.getName(),
saveProfileVM.getEmail());
optimisticLockException = false;
retryCount++;
} catch (ObjectOptimisticLockingFailureException exception) {
retryCount++;
System.out.println(exception.getMessage());
}
} while (optimisticLockException && retryCount < MAX_OPTIMISTIC_LOCK_EXCEPTION_RETRY_COUNT);
return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(profile));
}
Run Code Online (Sandbox Code Playgroud)
并且MAX_OPTIMISTIC_LOCK_EXCEPTION_RETRY_COUNT是 3
我不想do..while and try..catch blocks在需要检查的每个方法中复制ObjectOptimisticLockingFailureException
do {
try{} …Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,它有一个五页的底部导航栏。我使用 getx。在第一页,我列出了数据。我的问题是,当我从数据库手动更改数据(底部导航栏中的第一页)然后我越过页面时,回到第一页我看不到更改。
控制器;
class ExploreController extends GetxController {
var isLoading = true.obs;
var articleList = List<ExploreModel>().obs;
@override
void onInit() {
fetchArticles();
super.onInit();
}
void fetchArticles() async {
try {
isLoading(true);
var articles = await ApiService.fetchArticles();
if (articles != null) {
//articleList.clear();
articleList.assignAll(articles);
}
} finally {
isLoading(false);
}
update();
}
}
Run Code Online (Sandbox Code Playgroud)
和我的用户界面;
body: SafeArea(
child: Column(
children: <Widget>[
Header(),
Expanded(
child: GetX<ExploreController>(builder: (exploreController) {
if (exploreController.isLoading.value) {
return Center(
child: SpinKitChasingDots(
color: Colors.deepPurple[600], size: 40),
);
}
return ListView.separated(
padding: …Run Code Online (Sandbox Code Playgroud) controller ×10
model ×2
view ×2
action ×1
angularjs ×1
asp.net ×1
c# ×1
closures ×1
components ×1
ember.js ×1
flutter ×1
function ×1
getx ×1
hibernate ×1
ip ×1
javascript ×1
lambda ×1
laravel ×1
migration ×1
object ×1
php ×1
sorting ×1
spring-boot ×1
symfony ×1
symfony-2.4 ×1