0
레일을 처음 사용하여 기본 CRUD 응용 프로그램을 연습했습니다. 모든 것은 데이터를 저장하지 않는 새로운 양식 항목을 제출할 때를 제외하고 작동하는 것 같습니다. 지금은 제출하지 않을 것입니다 그래서 몇 가지 유효성 검사를 설정했습니다. 아직 다른 항목을 편집하여 새 데이터를 추가 할 수 있습니다. 그래서 제 편집이 효과가 있습니다.레일 : 양식 입력이 작동하지 않습니다.
여기 내 posts_controller.rb의
class PostsController < ApplicationController
def index
@posts = Post.all
end
def show
@post = Post.find(params[:id])
end
def new
@post = Post.new
end
def create
@post = Post.new(post_params[:post])
if @post.save
redirect_to posts_path, :notice => "Your email was sent!"
else
render "new"
end
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update_attributes(post_params)
redirect_to posts_path, :notice => "Your email has been updated."
else
render "edit"
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path, :notice => "Your email has been deleted"
end
private
def post_params
params.require(:post).permit(:name, :email, :message)
end
end
_form.html.erb
<%= form_for @post do |f| %>
<% if @post.errors.any? %>
<h2>Errors:</h2>
<ul>
<% @post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
<%= f.label :name %>
<%= f.text_field :name %>
<br>
<br>
<%= f.label :email %>
<%= f.text_field :email %>
<br>
<br>
<%= f.label :message %><br>
<%= f.text_area :message %>
<br>
<%= f.submit "Send Email" %>
<% end %>
edit.html.erb
<h1>Edit</h1>
<%= render "form" %>
index.html.erb
<h1>Emails</h1>
<hr>
<% @posts.each do |post| %>
<p><em>Name:</em> <%= link_to post.name, post %></p>
<p><em>Email:</em> <%= post.email %></p>
<p><em>Message:</em> <%= post.message %></p>
<p><%= link_to "Edit", edit_post_path(post) %>
| <%= link_to "Delete", post, :method => :delete %>
</p>
<hr>
<% end %>
<p><%= link_to "Add a New Post", new_post_path %></p>
new.html.erb
<h1>Email</h1>
<p>Send an email:</p>
<%= render "form" %>
show.html.erb
<p><em>Name:</em> <%= @post.name %></p>
<p><em>Email:</em> <%= @post.email %></p>
<p><em>Message:</em> <%= @post.message %></p>
<hr>