samedi 30 mai 2015

Rendering in Rails (ruby 2.2.1, rails 4.2.1)

My problem is in rendering error messages, when @post.save returns false. I don't want use flash messages.

I have two resourses: config/routes.rb

  resources :users
  resources :posts, only: [:create, :destroy]

User can have many microposts on his personal page (e.g. users/1). And I need to add ability of creating microposts from user's show view. Creating with valid information works well, but I want to display error messages from model Post in another way.

app/controllers/posts_controller.rb

...
  def create
    @post = current_user.posts.build(post_params)
    if @post.save
      redirect_to user_path(current_user)
    else
      #?????
      render ???
    end
  end

app/views/users/show

...
<%= form_for(@post) do |form| %>
  <%= render 'shared/error_messages', object: form.object %>
  <%= form.text_area :text, placeholder: "Что у Вас нового?" %>

  <%= form.submit "Добавить" %>
<% end %>

In partial I am using object.errors.full_messages. Any ideas?




Aucun commentaire:

Enregistrer un commentaire