So I made a seperate migration file to change the name of a column in my database table. The problem is when I try to go the page with the table I get an undefined method error on the column name I tried to change.
My professor told me I have to change the View/controller before The columns will work but I can not find out what I have to do.. any help/suggestions?
The way I'm changing the columns is like this:
class FixColumnName < ActiveRecord::Migration
def change
rename_column :suppliers, :sName, :"first_name"
add_column :suppliers, :"last_name"
remove_column :suppliers, :Snum
remove_column :parts, :Ptnum
end
end
If you've chaned the name of the database column, then you'll also need to change the name of the associated Model's attribute in your application code.
That is, if you previously had #supplier.sName (or similar), you'll now need #supplier.first_name (or something similar - using the new column names).
sNum not longer exist.
remove
<%= #supplier.sNumb %>
you will change
<%= #supplier.sName %>
to
<%= #supplier.first_name %>
you will also want to add
<%= #supplier.last_name %>
Related
i have two tables in Ruby on Rails, Movies and Directors. Movies has belongs_to association to director, and director has has_many association to movies. I can create both just fine, but when i try to edit a movie to include its director via a dropdown(using form.collection_select) and click update i get this message:
1 error prohibited this movie from being saved:
Director must exist
This is the code for the dropdown (its labeled in my native language, sorry about that)
"
<%= form.label :director_id, "Režisér", style: "display: block" %>
<%= form.collection_select(:director_id, Director.all, :id, :first_name, {:prompt => 'Vyberte režiséra'}, :selected => #movie.director_id ) %>
"
I'm a newbie to Ruby on Rails and my search on this issue has so far been unsuccessful and i have no idea how to ask Mr Google correctly
solved thanks to max's comment below, i simply needed to add the director_id reference parameter to my movies_controller.rb like so:
def movie_params
params.require(:movie).permit(:name, :release_date, :description, :director_id)
end
I had a model named b_page I wanted to create another column , so I ran a migration:
rails g migration add_status_to_b_page status:string
so migration was successful. Users should be able to update their status so I put this on the _form.html.erb:
<div class="field">
<%= f.label :status %><br>
<%= f.text_field :status %>
</div>
was successful but then i added it to the show.html.erb
<%= #b_page.status %>
but everytime i make a new b_page or edit the current one I dont see it on show.html.erb
Without seeing your code I guess you have to whitelist the new parameter (status) in your BPageController (at the very bottom, in something like def bh_pages_params).
You can check the logs whether the parameter that comes into your controller (via the form) actually arrives at the data (ActiveRecord Model), this whitelisting approach (called Strong Parameters) is in place to safe guard your data.
thx I fixed my problem I forgot to add status in
params.require(:b_page).permit(:Bpage_name, :banner_img, :profile_img, :status) in my controller
I want to add a column in a mysql table from a controller. The user completes a form, and when he sends it, it creates a new column (not row) with the information in the form. How can I do this?
it create new column
Don't.
Your database is sacrosanct, dynamically altering it is like dynamically changing a car based on some user's request. A car has four wheels, engine and seats. You can change the colour, tyres, etc... but not the fundamentals.
It's the same with web apps - you should not be changing the fundamental structure of your system. Sure, you'll be able to change various aspects of it (User Avatar etc), but the underlying basis of the system (the db schema) should be kept above any changes.
What you should be doing is maintaining your database fidelity through a tight set of Models, allowing you to create a dynamic experience around the data you've been provided.
For example...
The user complete a form and when he send it, it create new column
A better way to explain this will be to use a user story.
I'll surmise the following in your case:
A user wants to add a new project to his portfolio. He fills out the form to explain what the project will be and adds a number of extra fields specific for that project.
I think you're asking about the "extra fields" part...
You have to remember Rails is built on top of a relational database:
This means that you have the flexibility provided by your models to grant your users the capacity to add and manipulate as many pieces of associated data as they need.
The data they add to the system can have any name & any structure, so long as you provide that functionality within the system itself...
#app/models/user.rb
class User < ActiveRecord::Base
has_many :projects
has_many :specialized_fields, through: :projects
end
#app/models/project.rb
class Project < ActiveRecord::Base
belongs_to :user
belongs_to :specialized_field
accepts_nested_attributes_for :specialized_field
end
#app/models/specialized_field.rb
class SpecializedField < ActiveRecord::Base
has_many :projects
has_many :users, through: :projects
end
According to my example above,
User can make a Project
Project can have specialized fields (above the standard fields)
User can add specialized fields to the model
Thus you can do the following:
#app/controllers/projects_controller.rb
class ProjectsController < ApplicationController
def new
#project = current_user.projects.new #-> assuming you're using Devise
#specialized_field = #project.build_specialized_field
end
def create
#project = Project.save project_params
#project.save
end
private
def project_params
params.require(:project).permit(:name, :start_time, :end_time, specialized_field_attributes: [:name, :value])
end
end
The form could be as follows:
#app/views/projects/new.html.erb
<%= form_for #project do |f| %>
<%= f.text_field :name %>
<%= f.fields_for :specialized_field do |s| %>
<%= s.text_field :name %>
<%= s.text_field :value %>
<% end %>
<%= f.submit %>
<% end %>
Why would you like to add new columns to the database via the controller? This shouldn't be the case - and in general I couldnt' think of a single reason why this should ever be required. It sounds unconventional and against design principles.
If you add more information of what is required and what you are tyring to do I am pretty sure we can work out an alternative solution. (I bet what you are trying to do can be achieved with a many-to-many relationship, or similar, somehow). Post more information and see what we can do.
But as answer and solution I'd say it shouldn't be required.
Why do you want to do this? Database design is part of the application development, so you are the one deciding the rows. When the user inputs data, there are a gazillion things that can go wrong (hacks, invalid values...), which can affect your entire database. It's not worth risking that.
If you want a flexible schema, you can store hashes in a specific field.
For example you have a extra_data field which is a hash. Then in your form you can have to inputs, input_name and input_value which will go to the hash. This way you will have more flexible values for the same column and you don't need to change your database schema.
I have a checkout button on my product page show view which accepts the offer. Each offer belongs_to a user. I don't want the user who created the offer to be able to accept it themselves so if it is the current user on the page I want to hide the button. I can't figure out why this code doesn't work:
<% unless current_user.id == #offer.sender_id %> #sender_id is a foreign key in the offer model that makes each offer belong_to a user.
<div id="accept_offer">
<%= button_to 'Accept Offer', etc %>
</div>
<% end %>
current_user is a devise gem method I believe.
Any help appreciated.
your code seems correct, you maybe need to look into your Offer.sender_id attribute in the model to see if it contains the right user id (of the creator of the offer). You could check that by creating a new offer throught your application (in the browser) then, in the console you type:
Offer.last.sender_id
And check if it corresponds to your current_user id
Just saw the error and got the reason.
You tried the page without sign in so unless current_user works, this means you have not signed in. Your original code doesn't considered this case.
Generally you should see an error as current_user is not defined but you may have disabled that.
Two ways to fix:
Change current_user, assign an object in any case
class ApplicationController
def current_user
super || User.new
end
end
Change the logic
<% if current_user && current_user != #obj.sender %>
# Button code
# Only signed in user with different id can see it
I'm using a TEXT column in my MySQL database. As the documentation says, it is not possible to set a default value for these columns.
I'm currently using the following code to simulate this behavior:
class Data
before_save lambda {text_column ||= ''}
end
Is there any better, more railis/active_record way to do this?
If you're happy with a HTML5 solution, have you tried a :placeholder attribute on the :text_field?
Also do you really want to stuff a text_field (which captures a small amount of text) into a "text" type column? Did you mean text_area?
If you want the "default value" to actually be stored in the database if the user doesnt input anything then i suggest the following. It's the "Factory" pattern.
Instead of calling "new" on your ActiveRecord model class, you create a "setup" method in your model
def self.setup(params = {})
new(params).tap do |v|
v.text_column = "default value"
# other defaultings
end
end
In your controller instead of calling new on the class you call setup.
Add this in your migration
add_column :table_name, :column_name, :string, :default => 'your text'
Working for me
If you have a text field in a form for new, use a default value of this text field:
#views/controller/new.html.erb
<%= f.text_field :column_name, :value => "default value" %>
It is a good usability choice, since user is aware of the default value of that column.
Don't use it in edit.html.erb though, since in this case this filed will still have the default value, regardless of its original value in database.