for my rails app, my error messages are not displaying correctly. i think the reason was because on a failed validation, my controller did a redirect as opposed to a render. however, im having trouble rendering. all my variables seem to be missing. for example in my pub_messages#create i have...
def create
@pub_message = current_user.pub_messages.build
@pub_message.to_id = params[:pub_message][:to_id]
@pub_message.user_id = current_user.id
@pub_message.content = params[:pub_message][:content]
if @pub_message.save
flash[:success ] = "Your post has been sent"
redirect_to user_path(params[:pub_message][:to_id])
else
render 'users/show'
end
end
^(on a side note, im maually saving each of the attributes because of some security issue so i didn't make :to_id attr_accessible)
but back on point, when i do render 'users/show', it seems like it can't find any of my variables. it goes into my users show view, which complains about...
undefined method `name' for nil:NilClass
1: <% provide(:title, @user.name) %>
however, if it went to my users#show action, i declared @user.
def show
@user = User.find(params[:id])
@current_user = current_user
if user_signed_in?
@message = current_user.messages.build
@pub_message = current_user.pub_messages.build
end
@feed_items = @user.feed.paginate(page: params[:page], per_page: 20)
end
am i missing or doing something wrong? thank you
UPDATE: so it seems like it doesn't go to the show action. how could i resolve the error messages not displaying? if i do a redirect, doesn't it cause the browser to immediately request a new page? and hence my error messages would never come up?
UPDATE2: so...im actually not coming from the 'new' page of the model, but instead im coming from the users show template. and in the template, i have the following
<%= form_for([current_user, @pub_message]) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.hidden_field :to_id, :value => @user.id %>
<div class="micropost_message_field">
<%= f.text_area :content, placeholder: "Comments?", :id => 'public_message_text' %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
which is where the object is getting built, and which ultimately goes to my pub_messages#create. if i do re-initialize all the variables, do i need to move all the templates in a shared folder as well because its complaining about that.
or is there a better way to do this? maybe like rendering 'new', and then redirecting to the users#show?