Ruby on Rails/ActionController/Parameters

From Wikibooks, open books for an open world
< Ruby on Rails‎ | ActionController
Jump to navigation Jump to search


Remember the "rendering and redirecting" example you've seen when learning about the view?

def update
    @product= Product.find(params[:id])

    if @product.update_attributes(params[:name])
      redirect_to :action => 'index'
      render :edit

You know what the differences between render and redirect_to is. Now we want to take a look at WHAT gets updates. This is determined by given the "update_attributes" method what we want to update: params[:name]. In this example, the data is likely to come from an HTML-Form (therefore a POST request). You can also work with GET requests in the same manner. Rails will handle both requests by using the params command.

When using e.g. check boxes you are likely to get more then one set of data for the same attribute. For example option[]=1&option[]=2&option[]=3. As with the example above, you can work with params[:ids] to get access to these settings. Your options will look like options => {'1','2','3'}