简体   繁体   中英

Passing data from view to controller in rails

I have one question, in my rails app I have a controller who stock the information deals with a form and in this controller I write the title and the project_id because this form create a redmine ticket.

Now I want in an other view ( admin view ) can change the title and when the ticket is create, the title is the same title who was present in the admin view.

I send you my controller and an exemple what I want :

class RedmineController < ApplicationController
# Creating an issue

def new
@issue = Issue.new


end

def create
  #stock le issue description.
  original_redmine = params[:issue] && params[:issue][:nom_redmine]
  original_admin = params[:issue] && params[:issue][:nom_admin]
  original_email = params[:issue] && params[:issue][:email]
  # le modifie
  #Titre du ticket
  @admin_title = params[:issue][:admin_title]
  params[:issue][:subject] = @admin_title
  #Project_id :
  params[:issue][:project_id] = "1"
  #Description :
  params[:issue][:description] = "

   Nom de l'instance Redmine : http://#{original_redmine}.projet.appli.i2

   Nom de l'admin : #{original_admin}

   Email : #{original_email}"

 @issue = Issue.new(params[:issue])


   if @issue.save
    flash[:succes] = "Ticket cree"
    redirect_to root_path
  else
    flash[:avertissement] = "Tout les champs sont obligatoires"
    redirect_to redmine_path
       end
  end





end

So that's is my controller and now I would like the issue title was the title write in my admin form

My form view for the controller

<div class="section-index">
  <h2 class="title-edit-new">Creation d'instance Redmine</h2>
   <%flash.each do |key,msg| %>
<%= content_tag(:div, content_tag(:p,msg), :class =>"message #{key}") %>
<% end %>
<%= form_for @issue, :url => redmine_path do |form| %>
  <fieldset class="fields">

     <div class="php_form">
      <%= form.label :nom_redmine, "Nom de l'instance redmine : ", :class => 'label-php-redmine' %>
      <div class="input-php">
    htpp://<%= form.text_field :nom_redmine %>.projet.appli.i2
    </div>
    </div>

       <div class="php_form">
      <%= form.label :nom_admin, "Nom de l'admin redmine : ", :class => 'label-php-admin'%>
      <div class="input-php">
            <%= form.text_field :nom_admin %>
          </div>
    </div>
    <div class="php_form">
      <%= form.label :email, "Email :", :class =>'label-php-email' %>
      <div class="input-php">
        <%= form.email_field :email %>
      </div>
      </div>
     <div class="php_form">
      <%= form.label :priority_id, 'Priorité : ', :class => 'label-php-priorite' %>
      <div class="input-php">
      <%= form.select :priority_id, options_for_select({ "Bas" => "1", "Normal" => "2", "Haut" => "3", "Urgent" => "4", "Immediat" => "5" }, "2") %>
      </div>
    </div>
  </fieldset>

  <fieldset class="actions">
    <%= form.submit "Envoyé", :class => 'btn btn-primary submit-input submit-php ' %>
  </fieldset>
<% end %>
</div>

But know I want create an other view where are stock the value off @admin_title , but how I named this view and where I create it ? In new folder and in the same folder ?

I hope I was clear in my question.

EDIT

I add in my controller :

before_filter :set_admin_title, :only => [:create,admin_function]

and

def set_admin_title
@admin_title = params[:issue][:admin_title]
end

def admin_function
puts @admin_title
end

now how I create my view for set the admin_title with an form.text_field. I need to stock in the Database my title. And use him in my method create.

I can suggest you that you can pass parameters from view to controller <%= form.text_field :admin_title %>

In controller

you can access the subject

@admin_title = params[:issue][:admin_title]
params[:issue][:subject] = @admin_title

Hope I am able to understand your problem

you can then create a way like a hook it will set @admin_title

before_filter :set_admin_title, :only => [:create,your_other_ function]

def set_admin_title
@admin_title = params[:issue][:admin_title]
end

now you can can access it

def your_other_function
puts @admin_title
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM