Search code examples
ruby-on-railsrubyruby-on-rails-3formscontrollers

Change parameters in controller


I create an form and i want my description for exemple has a text add by me , not by the user who send the form. But i dont know how i can pass my parameters in the controller .

My form view

<%= form_for @issue, :url => contact_path do |form| %>
 <%= form.label :description %>
      <%= form.text_area :description  %>

My controller :

def create
 @issue = Issue.create(params[:issue])
 @issue.description = "The user write" + params[:issue][:description]

   if @issue.save
    flash[:succes] = "good"
    redirect_to root_path
  else
    flash[:avertissement] = "try again"
    redirect_to contact_path
       end
  end

But when i send my form i can see : the text write by the user but not "User write" before the user text. And i want when the user send the form i can will see : "User write : text write by user"

Thank's for your help.


Solution

  • You can set params[:issue][:description] before you create the issue (in the controller):

    def create
      original_description = params[:issue] && params[:issue][:description]
      params[:issue][:description] = "The user write #{original_description}"
      @issue = Issue.new(params[:issue])
    
      if @issue.save
        flash[:succes] = "good"
        redirect_to root_path
      else
        @issue.description = original_description
        flash[:avertissement] = "try again"
        redirect_to contact_path
      end
    end
    

    Edited to show full code and avoid errors if it didn't pass validations