views:

56

answers:

2

The embedded ruby code in my view is...

<p id="notice"><%= notice %></p>

but when the URL reads...

http://0.0.0.0:3000/projects/3?notice=Project+was+successfully+created

the generated HTML is...

<p id="notice"></p>

I'm expecting it to read...

<p id="notice">Project was successfully created</p>

What gives?

EDIT: Here's my controller code just to ease any curiosity..

def create
    @project = Project.new(params[:project])

    respond_to do |format|
      if @project.save
        format.html { redirect_to(:action => "show", :id => @project, :notice => 'Project was successfully created.') }
        format.xml  { render :xml => @project, :status => :created, :location => @project }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
      end
    end
  end

Thanks so much!

+2  A: 

Try assigning the value in the controller with something like

@notice_text = params[:notice]

and then you can use something like

<p id="notice><%= @notice_text %></p>

in your view.

You may wish to read the answer to this question which deals more explicitly with passing variables through URL query strings.

Joe
I don't understand, what's the point of the 'notice' convenience method then? If I set the notice using :notice => 'Article was successfully created.' in my controller, shouldn't the '<%= notice %>' display it?
BeachRunnerJoe
The flash[] hash is actually something different. You can assign to it from the controller as well. You may want to take a look at this post on "Useful flash messages in Rails" -- http://rubypond.com/blog/useful-flash-messages-in-rails
Joe
Thanks, Joe, I appreciate your help. I guess what I'm asking is, shouldn't my code work for what I'm trying to do?
BeachRunnerJoe
Well -- it depends. In the view, 'notice' doesn't have any meaning, even if you try to assign something to it from the controller. The flash hash provided to the view is special in this sense. Other things you pass into the view need to use the '@' prefix; hence the '@notice_text'. So, as long as you assign the URL querystring value from the params[] hash to something in your controller (whether it's the flash[] hash or a variable like '@notice_text' in the example), you can grab it in the view. We didn't see your controller code, so I couldn't say whether it's exactly right or not...
Joe
+1  A: 

Your code is not working because the "notice" convenience method uses the flash, whereas you want to display the params, not the flash. Try instead of doing a redirect and assigning the notice in the params, to first assign the notice to a flash variable then redirect.

if @project.save
  flash[:notice] = 'Project was successfully created'
  redirect_to project_path(@project)
end
Faisal
I just updated my question with my controller code, shouldn't that work? thanks again!
BeachRunnerJoe
No, that won't work, because you are passing the `:notice => 'Project ...'` in the redirect call, which passes the notice variable in the params hash (as a get variable in the url). You need to specify the notice variable in the flash instead. try this: `format.html { flash[:notice] = 'project success' and redirect_to(:action => :show, :id => @project.id) }`
Faisal
wonderful, thank you!
BeachRunnerJoe