I am new to ROR. I want to capture the filename of the browse button after selected the file with the extension. And i want to store the filename into the File_name column in the database. Please help me to do that. Is it possible to get the full path of the file stored in the File_name column instead of just a filename? please suggest me to do that. I dont want to upload the file. Just i want a filename to store.
View(New)
--------------
<div class="pro-data-imports">
<%= form_for #pro_data_import do %>
<div class="field">
Browse the file to upload:<br />
<%= file_field_tag :File_name %>
</div>
<div class="actions">
<%= submit_tag 'Import File' %>
</div>
<% end %>
</div>
Controller
----------
class Pro::DataImportsController < ApplicationController
before_filter :authenticate_user!
layout "layouts/enr/energy_master"
def index
#pro_data_imports = Pro::DataImport.all
end
def new
#pro_data_import = Pro::DataImport.new
#pro_data_import.updated_by = current_user
end
def create
#pro_data_import = Pro::DataImport.new(params[:pro_data_import])
#pro_data_import.updated_by = current_user
end
end
Model
-------
I have File_name, File_validate, :updated_by Columns
I don't know what you meant by that. In your comments you mentioned to want to know, how to call the store procedure with two parameters. If its your database is SQL try the following and change your field name as per your spec.
In your model, create a method
def update
parameters = [self.File_Name, File_Location]
parameters.map!{|p| ActiveRecord::Base.connection.quote p}
sql = "your store procedure name"(#{parameters.join(',')})"
ActiveRecord::Base.connection.execute(sql)
end
In your controller,
Inside your create method, call the update method.
def create
#update = Pro::DataImport.new(params[:pro_data_import])
#pro_data_import.updated_by = current_user
#update.update
.........
...
end
I hope it will help. This is just an example to follow. Do not copy and paste.. :)
Related
i built this form that generate me some chebox with value like "U6", "U8" eccc
<%= form.label "Seleziona Categorie" %>
<% TeamCategory::NAMES.each do |category| %>
<%= check_box_tag 'categories_selected[]', category -%>
<% end %>
Now i have to pass the value of selected check_box to a method in my model.
Now is:
def create_tournament_team_categories
TeamCategory::NAMES.each do |name|
team_category = TeamCategory.where(name: name).first_or_create
self.tournament_team_categories << TournamentTeamCategory.create(team_category: team_category)
end
end
I would like to replace the TeamCategory::NAMES.each do with "selected check_box each do" and TeamCategory.where(name: name) with the value selected.
Thank you in advance
I am a newbie with Rails. What I see is that you took the part of the form to create the team, right?
For your code straight forward it could be:
<%= form.label "Seleziona Categorie" %>
<% TeamCategory::NAMES.each do |name| %> #you are looping through team category NAMES constant
<%= check_box_tag 'category_names_selected[]', name %>
<% end %>
Your form as is allows more than one category to be selected.
For the method:
def create_tournament_team_categories(category_names_selected)
category_names_selected.each do |name|
team_category = name
self.tournament_team_categories << TournamentTeamCategory.create(team_category: team_category)
end
end
you will probably use this method in your teams_controller.rb. In the controller, you should be able to retrieve from params a freshly created array of selected names with something along the lines with this.
#category_names_selected = params[:category_names_selected]
I do not know how complicated your app is so it might also be nested under ["team"][:category_names_selected] or ["team"]["category_names_selected"] in your params hash.
To see the exact structure of the params hash and adjust the equation above you can add for example require 'pry' at the top of your controller file and then but the binding.pry just after the part where your method is executed. When you restart the server and the app hits this part of the controller you should be able to see the exact structure of your params hash in the terminal.
You can then pass the array to the method that you can call in the controller. Do not forget to add :category_names_selected to the strong params in the controller. I hope this helps.
Controller on line 30
def create
#tournament = Tournament.new(tournament_params)
#tournament.sport_club = current_user.sport_club
#category_names_selected = params[:category_names_selected]
if #tournament.save
redirect_to tournaments_path, notice: 'Torneo creato con successo'
end
end
Method create_tournament_team_categories in the model
after_create :create_tournament_team_categories
def create_tournament_team_categories(category_names_selected)
#category_names_selected.each do |name|
team_category = name
self.tournament_team_categories << TournamentTeamCategory.create(team_category: team_category)
end
end
I’m new to rails and I’ve a pretty simple situation to solve but I cannot figure out how to proceed with it.
I want to create a simple ‘Setting’ model with key, value attributes. ‘SettingsController’ may contain 2 public methods only index and update. Only index action will have a view file with a form whose fields will represent each record of the ‘Settings’ table.
I want to be able to define some permitted keys (may be using some private method) and I want the form to create or update the record of relevant fields on submitting the form to update action.
Now, I don’t know exactly what code should I use in controller for index and update actions and in the index view file for the form which can create/update multiple records at the same time and can show updated values all the time. How do I proceed with it?
Update # 1:
I've managed to write some controller actions as below (based on some tutorial):
class Admin::SettingsController < ApplicationController
def index
#settings = Setting.all
end
def update
setting_params.each do |key, value|
Setting.where(key: key).first.update_attribute :value, value
end
redirect_to admin_settings_path, notice: "Settings saved."
end
private
def setting_params
params.require(:settings).permit(:site_title, :site_desc)
end
end
The form code in index view template is given below:
<h1>Settings</h1>
<%= form_tag admin_settings_path, method: "put" do %>
<p>
<label>Site Title:</label>
<%= text_field_tag "settings[site_title]" %>
</p>
<p>
<label>Site Description:</label>
<%= text_field_tag "settings[site_desc]" %>
</p>
<p>
<%= submit_tag "Save settings" %>
</p>
<% end %>
This forms saves the values correctly in the database but the saved values doesn't persist in form fields.
Maybe the problem is in the index action. Setting.all returns an array of Setting record, not a hash like { key1: value, key2: value } which I think you are trying to achieve. The form, therefore, displays data improperly. You can try this:
def index
#setting = {}
pairs = Setting.pluck(:key, :value)
pairs.each { |key, value| #setting[key] = value }
#setting
end
I am trying to create associated models with Rails. My models are Financial which have many documents and Document which belongs to Financial. A working code when creating the associated model could be
def create
#financial = Financial.find(2)
#document = #financial.documents.create(document_params)
...
end
In my view I have a form which looks like this to select the right Financial
<%= form_for Document.new do |f| %>
<%= f.collection_select :financial_id, Financial.all, :id, :description %>
<%= f.submit %>
I do see the right parameters being transferred in the log when I submit the form
"financial_id"=>"3"
So I figured I would just need to change the initial code to:
def create
#financial = Financial.find(params[:financial_id])
#document = #financial.documents.create(document_params)
...
end
but I get a "Couldn't find Financial with 'id'=". I have tried other things including:
#financial = Financial.find_by(id: params[:financial_id])
Without much success. Could anyone give me the appropriate syntax please? Thanks.
Couldn't find Financial with 'id'=
Because, the params that are submitted are actually inside document hash. So params[:financial_id] won't work. Instead you need to use params[:document][:financial_id]
def create
#financial = Financial.find(params[:dcument][:financial_id])
#document = #financial.documents.create(document_params)
...
end
I have difficulties with ruby on rails syntax.
I got this error
First argument in form cannot contain nil or be empty
class PersonalsController
def index
end
def create
#personal = Personal.new
end
def new
#personal = Personal.new
end
def show
#personal = Personal.find([:id])
end
end
index.html.erb
<%= form_for #personal do |f| %>
<%= f.label :title %><br>
<%= f.text_field :title %>
<%= f.submit %>
<% end %>
the value of #personal is nil that's why you are getting error.
Change you code like this
def index
#personal= Personal.all
end
form_for is helper method
check with this link form_helper
The error is generated since #personal was not set in the controller. So either you add a #personal = Personal.new to the index method, or set it to a specific database entry, e.g., #personal = Personal.find(1)
However, it seems strange that you have a form displaying a single record in the index view.
More likely, you want to have the form in your new or edit views (in the former case you typically use new, while in the latter case you would use the find method to find a specific record and let the user edit it).
In the index method, you usually use the controller to select a group of records (e.g., #ps = Personal.all to get all the records) and iterate over them in the view (#ps.each do |person| .... end)
P.S. The show method should probably use Personal.find(params[:id]) instead of Personal.find([:id])
I got error when using controller to read text file:
#my_page.html.erb
<input id="test_input" >
#controller.rb
def my_page
File.open($directory+'\test.TXT', 'r') do |f1|
$line = f1.readlines
f1.close
end
respond_to do |format|
format.js
end
end
#my_page.js.erb
el = document.getElementById(test_input);
el.innerHTML='<%=$line$>';
It load blank page without input and I don't know why, Please correct me if I'm wrong.
I think I understand what you are doing. Here is what you can do.
#controller.rb
def my_page
#text= File.open('your_file.txt').read
end
#my_page.html.erb
<input id="test_input" value="<%= #text %>" />
When you refresh your page, the text should show up on your page. No need to use Javascript now. You should use it when you are requesting the resource using AJAX.
If you would like to update the value of the text box, you need to add a new action in your controller and update your routes.rb accordingly. Also, it's always a good idea to use form helpers that Rails provide to utilize security feature (CSFR token), as shown below. If you have a model for the text field, you can use a form_for helper. You can read about them in
http://guides.rubyonrails.org/form_helpers.html
Here is the code, nonetheless.
#my_page.html.erb
<%= form_tag save_text_path do %>
<%= text_field_tag :test_input, #text %>
<%= submit_tag "Update" %>
<% end %>
# in your controller
def save_text
updated_text = params[:test_input]
# do something with the text
end
# in your routes.rb
post "save_text" => "your_controller_name#save_text", as: "save_text"