Skip to content

Commit

Permalink
fix ecto validate
Browse files Browse the repository at this point in the history
  • Loading branch information
Kabie committed Jan 5, 2015
1 parent eba567d commit e27a69b
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 14 deletions.
7 changes: 3 additions & 4 deletions web/controllers/comment_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,18 @@ defmodule ElixirChina.CommentController do
content: params["content"], time: utc}

case Comment.validate(comment) do
[] ->
nil ->
Repo.insert(comment)
increment_score(Repo.get(User, user_id), 1)
post = from(p in Post, where: p.id == ^comment.post_id, preload: :user)
|> Repo.one
post = %{post | update_time: utc, comments_count: post.comments_count+1 }
Repo.update(post)
# POST_REPLY = 0
notify_subscriber(comment.post_id, post.user_id, 0)
notify_mentioed_users(comment.post_id, comment.content)
redirect conn, to: Helpers.post_path(:show, post_id)
errors ->
render conn, "new", comment: comment, errors: errors, user_id: get_session(conn, :user_id)
render conn, "new.html", comment: comment, errors: errors, user_id: get_session(conn, :user_id)
end
end

Expand All @@ -61,7 +60,7 @@ defmodule ElixirChina.CommentController do
comment = validate_and_get_comment(conn, id)
comment = %{comment | content: params["content"]}
case Comment.validate(comment) do
[] ->
nil ->
Repo.update(comment)
json conn, %{location: Helpers.post_path(:show, post_id)}
errors ->
Expand Down
4 changes: 2 additions & 2 deletions web/controllers/post_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule ElixirChina.PostController do
category_id: String.to_integer(category_id), time: utc, update_time: utc}

case Post.validate(post) do
[] ->
nil ->
post = Repo.insert(post)
increment_score(Repo.get(User, user_id), 10)
redirect conn, to: Helpers.post_path(:show, post.id)
Expand All @@ -72,7 +72,7 @@ defmodule ElixirChina.PostController do
category_id: String.to_integer(params["category_id"])}

case Post.validate(post) do
[] ->
nil ->
Repo.update(post)
json conn, %{location: Helpers.post_path(:show, post.id)}
errors ->
Expand Down
6 changes: 3 additions & 3 deletions web/controllers/user_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ defmodule ElixirChina.UserController do
user = %User{email: email, name: name, admin: false, password: password}

case User.validate(user) do
[] ->
user = %{user | :password => to_string User.encrypt_password(user.password)}
nil ->
user = %{user | :password => to_string User.encrypt_password(user.password)}
user = Repo.insert(user)
conn = put_session conn, :user_id, user.id
conn = put_session conn, :current_user, user
Expand Down Expand Up @@ -55,7 +55,7 @@ defmodule ElixirChina.UserController do
user = %{user | password: params["password"]}

case User.validate_password(user.password) do
[] ->
nil ->
user = %{user | :password => to_string User.encrypt_password(user.password)}
Repo.update(user)
render conn, "show.html", user: user, user_id: get_session(conn, :user_id)
Expand Down
2 changes: 1 addition & 1 deletion web/templates/comment/edit.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<textarea rows="8" name="comment[content]" class="editor" id="editor"><%= @comment.content %></textarea>
</div>
<div class="form-group">
<input type="submit" class="span-primary submit_btn" value="更新"></button>
<input type="submit" class="btn btn-primary" value="更新"></button>
</div>
</form>
</div>
Expand Down
2 changes: 1 addition & 1 deletion web/templates/layout/application.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<link rel="stylesheet" href="/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/app.css">
<link rel="stylesheet" href="/css/editor.css" />
<script src="//cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/jquery-2.1.3.min.js"></script>
<script type="text/javascript" src="/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/js/editor.js"></script>
<script type="text/javascript" src="/js/marked.js"></script>
Expand Down
4 changes: 2 additions & 2 deletions web/templates/post/_form.html.eex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<form action="<%= @action %>" method="post">
<input type="hidden" name="_method" value="PATCH">
<input type="hidden" name="_method" value="POST">
<div class="form-group form-inline">
<label class="">所属板块
<select name="post[category_id]" class="form-control" value="<%= @post.category_id %>">
Expand All @@ -16,6 +16,6 @@
<textarea name="post[content]" class="editor" id="editor"><%= @post.content %></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="<%= @post && "更新" || "发布" %>"></input>
<input type="submit" class="btn btn-primary" value="<%= @post.id && "更新" || "发布" %>"></input>
</div>
</form>
2 changes: 1 addition & 1 deletion web/templates/post/edit.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$.ajax({
url: $that.getAttribute('action'),
type: "POST",
type: "PATCH",
data: $('form').serialize(),
success: function(data) {
window.location = data.location;
Expand Down

0 comments on commit e27a69b

Please sign in to comment.