Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: params in format.html { render ... }

I'm trying to pass params in a controller method's format.html like this:

def create
  @doc = Doc.new(params[:doc])
  respond_to do |format|
    if @doc.save
      format.html { redirect_to share_url(@doc.user.ftp, @doc) }
    else
      format.html { render "new", :locals => { :template => @doc.template_id } }
    end
  end
end

I get a no method error which tells me that I'm sending the right parameter to :template in the local variables:

local_assigns {:template=>4}

Is there something I'm missing to get that to work? It should redirect to the doc#new action but it's going to the doc#index instead. Any ideas?

like image 836
t56k Avatar asked Feb 16 '23 00:02

t56k


1 Answers

Using this syntax within a controller:

render "new", :locals => { :template => @doc.template_id }

means that you're rendering the new.html.erb template, not a partial. You are unable to pass local variables along to a non-partial view. What you should be doing is calling render "new", and referring to @doc in that view as you require it.

like image 171
Ryan Bigg Avatar answered Feb 28 '23 01:02

Ryan Bigg