问题的简要说明:我正在使用rails 4应用程序(4.1.8),我试图让flash[:notice]
和flash[:alert]
显示在表单下。
两个控制器:landingpage_controller
和contacts_controller
。 landingpage_controller
通过其show
操作和contacts_controller
具有new
和create
操作提供静态目标,以将联系人存储在数据库表中。
在静态目标网页上,id="contact-modal"
的模式包含一个带有simple_form_for @contact的部分(见下文)。提交表单后,如果字段未全部填写,则创建
通缉输出:
理想情况下,部分将在不离开/关闭模态的情况下重新加载,其中包括:成功消息和空表单或警报消息以及提交时的表单。 我该怎么做?
控制器:app/controllers/contacts_controller.rb
class ContactsController < ApplicationController
def new
@contact = Contact.new
render layout: "contact"
end
def create
@contact = Contact.new
respond_to do |format|
if @contact.save
flash[:notice] = "Success"
format.js
else
flash[:alert] = "Error"
format.js
end
end
end
private
def contact_params
params.require(:contact).permit(:email, :structure, :message_content)
end
end
表格:app/views/contacts/_new.html.haml
= simple_form_for @contact, html: { id: "contact-form"} do |c|
= c.input :email
= c.input :structure
= c.input :message_content
= c.button :submit
.messages-container
= if flash[:notice]
%p
= flash[:notice]
= if flash[:alert]
%p
= flash[:alert]
路线:
resources :contacts, only: [:new, :create]
我知道部分重新加载可能涉及AJAX。我已经阅读了几个关于StackOverflow的问题,但一直无法弄清楚。请参阅here,here以及这两篇博文:jetthoughts,ericlondon。
非常感谢您的帮助
答案 0 :(得分:0)
您的代码中存在几个问题:
package assignment.pkg2;
import java.util.Scanner;
public class Assignment2 {
public static int getMonthNumber(String s) {
if (s.equalsIgnoreCase("jan")) {
return 1;
}
if (s.equalsIgnoreCase("feb")) {
return 2;
}
if (s.equalsIgnoreCase("mar")) {
return 3;
}
if (s.equalsIgnoreCase("apr")) {
return 4;
}
if (s.equalsIgnoreCase("may")) {
return 5;
}
if (s.equalsIgnoreCase("jun")) {
return 6;
}
if (s.equalsIgnoreCase("jul")) {
return 7;
}
if (s.equalsIgnoreCase("aug")) {
return 8;
}
if (s.equalsIgnoreCase("sep")) {
return 9;
}
if (s.equalsIgnoreCase("oct")) {
return 10;
}
if (s.equalsIgnoreCase("nov")) {
return 11;
}
if (s.equalsIgnoreCase("dec")) {
return 12;
} else {
System.out.println("Not valid month!");
}
return 0;
}
public static boolean isLeapYear(int year) {
int month = 0;
int s = getDaysIn(month, year);
return year % 4 == 0 && (year % 100 != 0) || (year % 400 == 0);
}
public static int getDaysIn(int month, int year) {
switch (month) {
case 1:
return 31;
case 2:
if (isLeapYear(month)) {
return 29;
} else {
return 28;
}
case 3:
return 31;
case 4:
return 30;
case 5:
return 31;
case 6:
return 30;
case 7:
return 31;
case 8:
return 31;
case 9:
return 30;
case 10:
return 31;
case 11:
return 30;
case 12:
return 31;
default:
return -1;
}
}
public static String getMonthName(int month) {
switch (month) {
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
default:
return "Invalid month.";
}
}
public static int getStartDay(int month, int year) {
int days = 0;
for (int i = 1900; i < year; i++) {
days = days + 365;
if (isLeapYear(i)) {
days = days + 1;
}
}
for (int i = 1; i < month; i++) {
days = days + getDaysIn(month, year);
}
int startday = (days + 1) % 7 + 1;
return startday;
}
public static void displayCalendar(int month, int year) {
String monthName = getMonthName(month);
int startDay = getStartDay(month, year);
int monthDays = getDaysIn(month, year);
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
int weekDay = startDay - 1;
for (int i = 1; i <= startDay; i = i + 1) {
System.out.print(" ");
}
for (int x = 1; x <= monthDays; x++) {
weekDay = weekDay + 1;
if (weekDay > 7) {
System.out.println();
weekDay = 1;
}
System.out.format(" %3d", x);
}
if (weekDay > 7) {
System.out.println();
}
}
public static void main(String[] args) {
Scanner c = new Scanner(System.in);
System.out.print("Give the first three letters of a month and enter the year: ");
String month, year;
month = c.next();
year = c.next();
int yearno = Integer.parseInt(year);
int monthno = getMonthNumber(month);
displayCalendar(monthno, yearno);
}
}
,因为您通常不会使用&#39 ; t想要整页重新加载。 1.1将您的render
重命名为_new.html.haml
2.1创建一个新视图_form.html.haml
(我猜你已经有了这个,否则你的new.html.erb
行动可能无法正常运作)内容为new
根据我的理解,您不希望模式关闭,只是在成功提交后呈现表单或者是否有错误。
在那种情况下:
1.在您的views / contacts文件夹中创建一个= render 'form'
文件
create.js.erb
create.js.erb
2。更改$("#your_modal_id").html("<%= j( render 'form') %>")
操作
create
def create
@contact = Contact.new(contact_params)
respond_to do |format|
if @contact.save
flash[:notice] = "Success"
format.js
else
flash[:alert] = "Error"
format.js
end
end
end
警告:即使表单成功,也会填写表格。
有关此主题的更多信息,请参阅:
http://guides.rubyonrails.org/v4.1.8/working_with_javascript_in_rails.html#form-for
希望它有所帮助,我希望我没有忘记任何事情