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.

2
  • 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. Commented Jun 7, 2013 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 Commented Jun 7, 2013 at 15:08

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

when i put params[:issue][:description] = "The user write #{params[:issue][:description]}" in my controller ?
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.
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.

11 Comments

I try this but when i show where my description is post i see juste the text write by the user and not "The user write"
Did you create a new issue AFTER making this change to your create method? Or are you trying to view an issue that you previously created?
the problem is not the user , the problem is just when i show my post i just see blablabla and not The user write blablabla
Sorry, I meant did you create a new issue?
Yeah , i use my form. for post a new issue
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.