rails render missing variables -


for rails app, error messages not displaying correctly. think reason because on failed validation, controller did redirect opposed render. however, im having trouble rendering. variables seem missing. example in pub_messages#create 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 side note, im maually saving each of attributes because of security issue didn't make :to_id attr_accessible)

but on point, when render 'users/show', seems can't find of variables. goes users show view, complains about...

undefined method `name' nil:nilclass 1: <% provide(:title, @user.name) %> 

however, if went users#show action, 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 missing or doing wrong? thank you

update: seems doesn't go show action. how resolve error messages not displaying? if redirect, doesn't cause browser request new page? , hence error messages never come up?

update2: so...im not coming 'new' page of model, instead im coming users show template. , in template, have following

<%= form_for([current_user, @pub_message])  |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 object getting built, , goes pub_messages#create. if re-initialize variables, need move templates in shared folder because complaining that.

or there better way this? maybe rendering 'new', , redirecting users#show?

redirect show flash messages won't show validation errors. since aren't setting flash error message, i'm guessing want display validation errors model. you'll need stick render, not redirect.

in order render, you'll need initialize variables required show view (although i'm unclear on why don't render 'new', ostensibly user navigating from). cleanly, can move content of show method method, , call method show , failed create.

def initialize_show_vars    @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  def show   initialize_show_vars end  def create   <stuff ...>   if @pub_message.save     <stuff ...>   else     flash[:error] = "didn't work"     initialize_show_vars     render 'users/show'   end end 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -