0

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.

  • Do you want it to actually save "User write" into the :description? If you just want it to show "User write", then that is better handled in the view. – Julian G. Jun 07 '13 at 15:06
  • No because my form is put in Redmine for create a ticket so i want in the description see The user write – Florian Dano Clement Jun 07 '13 at 15:08

2 Answers2

1

I am surprised you are not receiving the following error.

TypeError in IssuesController#create
can't convert nil into String

I say this because it seems you are not using the correct params hash key on this line.

@issue.description = "The user write" + params[:description]

Instead it should be as follows.

@issue.description = "The user write " + params[:issue][:description]

Making only this change, your code should work as intended.

Also, try using Issue.new instead of Issue.create. Like this.

def create
  @issue = Issue.new(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

Using .create will create a new Issue, validate it and save it to the database. Using .new will simply create a new Issue, but will not save it to the database until you call .save. The way you are using .create now, your issue is being saved to the database before you change the description. Then when you try to save it again, it is not able to be saved for some reason.

1

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

Josh
  • 8,329
  • 4
  • 36
  • 33
  • when i put params[:issue][:description] = "The user write #{params[:issue][:description]}" in my controller ? – Florian Dano Clement Jun 07 '13 at 15:04
  • edited my answer. Only issue is if it fails validation, you'll get "The user write" in the description on the user's machine. If the rest of it works, I can address that issue. – Josh Jun 07 '13 at 15:28