Rails unedited form fields if db column is NULL - ruby-on-rails-4

Let me try to present a simple example here:
I have db table Orders and a column delivery_address.
<%= form_for #order do |f| %>
<%= f.text_field :delivery_address %>
<% end %>
If no change is made on the form, when the form is submitted the value of Orders.delivery_address changes from NULL to empty value.
and I set a alert notice which looks like:
test#gmail.com changed delivery_address to .
Any suggestion how to prevent updating db fields with NULL values to empty by default with rails update action.

You could do something like this in the model:
# In the Order model
before_validation do
self.delivery_address = nil if delivery_address.blank?
end
I also really don't like that this happens, but the other alternative is to do it on the controller level

Related

Rails 4.2+: Mapping PostgreSQL JSONB nested values to form fields

I have a users table with a settings field of type JSONB (using PostgreSQL 9.5).
I'm trying to create a form on a settings page to update user.settings["notifications"][...] values.
class User < ActiveRecord::Base
end
user = User.create(settings: { notifications: { post_created: true } })
user.settings["notifications"]["post_created"] # => true
To map the nested JSONB values to a form, however, I have to do this:
# views/form.html.erb
<input type="check" name="user[settings][notifications][post_created]" checked="<%= current_user.settings['notifications']['post_created']" %>
class SettingsController
def update
current_user.settings["notifications"]["post_created"] = params["user"]["settings"]["notifications"]["post_created"]
current_user.save
end
end
Is there anyway to utilize the power of Rails form builders such that I can do:
# will not work, undefined attribute settings['notifications']['post_created']...
<%= form_for current_user, url: settings_path, method: "PUT" do |f| %>
<%= f.check_box "settings['notifications']['post_created']" %>
<% end %>
I understand that Rails is trying to map an attribute from the current_user object, and there isn't really an "attribute" named settings['notifications']['post_created'].
But how does one go about mapping nested JSONB values to a form field for CRUD activity?
A workable (but not really feasible) approach is to created virtual attributes for every single nested value I want to work with:
class User
def settings_notifications_post_created
settings["notifications"]["post_created"]
end
def settings_notifications_post_created=(value)
settings["notifications"]["post_created"] = value
end
end
# view...
<%= f.check_box :settings_notifications_post_created %>
But this loses any benefit of a conventional system since I'm manually typing out every attribute. May as well write raw HTML fields and all the getter/setter methods myself...Googling and Stack Overflow haven't been very helpful so far, it seems there aren't very many with experience doing this kind of stuff yet...

Rails 4: chaining associated objects in Views

Ok, I searched all over the web and found no answer.
I am looking for a way to display a name of a 'category' in the show view of a post (I have to mention I'm rookie in Rails).
I have....
a model "Post"
class Post < ActiveRecord::Base
has_many :categories
belongs_to :user
end
a model "Category"
class Category < ActiveRecord::Base
belongs_to :post
end
and the "show" view of the "post" has a line like this
<%= #post.category.name %>
The error message as screen shot:
NoMethodError in Posts#Show - undefined method `category' for #
The "show" action in "Posts" controller:
def show
#post = Post.find(params[:id])
end
I'm building this app along a little outdated training video on udemy. In this video there's in the category model a line with "attr_accessible"
class Category < ActiveRecord::Base
attr_accessible :name <---------------------- this
has_many :posts
end
...but this does no longer exists since Rails 4.0. Is there another way to retrieve the category name of the post?
Thank you in advance :-)
I got the answer. I found out, that every way, to get the data out of the table categories in the view for products won't work. I than thougt, showing the category in the category show view is simple working. With this idea in mind I took the same code, this one:
app/views/categories/show.html.erb
<p>
<strong>Name:</strong>
<%= #category.category_name %> <--------this line
</p>
...from the category show view and put it into the post show view. I than got again an error but different:
--> NoMethodError in Posts#show
Ok, this says the instance variable "#category" isn't available for the post show view. To change that was easy. I copied the object from the categories controller's show action into the posts controller's the show action. Like this:
class PostsController < ApllicationController
.
.
.
def show
#post = Post.find(params[:id])
#category = Category.find(params[:id])
end
And: it works!!!!
But now, is there something wrong to do it like this?
Cheers.
The method category not exists because the Post model has many "categories" not one "category". The Post should have the method "categories". Then if you want to show a first "category" of post in the view:
<%= #post.categories.first.name %>
If you want to show all "categories" of post, then you iterate the collection:
<% #post.categories.each do |category| %>
<%= category.name %>
<% end %>
I tried again and found the real error in my code. the foreign key was not set by Rails. I had to do manually. The table which has a "belongs_to" (in my case the posts table) needs a foreign key added (like category_id).
First creating the migration file:
rails g migration add_foreign_key_to_posts_table
Second adding the migration code to the migration file:
class AddForeignKeyToPostsTable < ActiveRecord::Migration
def change
add_foreign_key :posts, :categories
end
end
Third raking the db migration with:
rake db:migrate
Fourth adding the foreign key for other resources by following steps one to three. Now everything works fine.

Using the Friendly ID gem with Rails, how do you regenerate the slug field?

I'm building a rails app that hosts games. Games belong to categories and thus each category can have many games.
I'm using the Friendly_id gem to generate URL slugs and have the following setup:
Category.rb
class Category < ActiveRecord::Base
extend FriendlyId
friendly_id :name_and_games, use: [:slugged]
has_many :games
def name_and_games
"#{name}-games"
end
end
Games_Controller.rb
...
def category
#category = Category.friendly.find(params[:id])
#categories = Category.all
#games = #category.games.page(params[:page])
render 'games/index'
end
...
Routes.rb
get ':friendly_id', to: "games#category", as: :category
Category_View.html.erb
<% categories.each do |category| %>
<%= link_to category_path(category) do %>
<span><%= pluralize(category.name.capitalize, "Game") %> (<%= category.games.count %>)</span>
<% end %>
<% end %>
The problem is that when I use Friendly_id's slugged module, Rails is generating my urls WITHOUT the "-games" suffix so I end up with URLS like this:
http://localhost:3000/action
http://localhost:3000/adventure
Is there some way to have rails keep the "-games" in my URLs and have it all play nicely such that the "-games" are dropped when it comes time for processing in the model?
Thank you.
So it's:
User.find_each{ |i| i.slug = nil; i.save!; }
Turns out the issue wasn't that Friendly_id wouldn't make the update but that the slug was not getting refreshed.
I had to drop into the Rails console and manually set the "slug" column to nil for each category. Once I did this, Friendly_id reset the "slug" column to reflect my additional text.

dynamic fields in redmine based upon input

I am using custom fields in redmine. I need a set of custom fields to populate based upon how a user answers a question. for instance, if a user chooses "a" they get a series of 3 custom fields that pertain to "a" ..if a user chooses "b" they get a series of custom fields that pertain to "b" is this possible? any help would be great!
Are You search ready plugin or want to develop your own with neccesary functionality?
In case of develop You may pass selected field from view to controller as parameter. Then check which field was selected and set value to it from other parameter.
I think that somethng like this:
view
<%= form_tag ... do %>
<%= label_tag :selected_field %>
<% select_tag :selected_field, options_for_select(['field1', 'field2',...])
<%= label_tag :value %>
<% text_box_tag :value, value %>
<%= submit_tag 'save' %>
<% end %>
and controller method
def update
obj = SomeClass.find_by... # get your instance
case params[:selected_field]
when 'field1'
obj.field1 = params[:value]
when 'field2'
obj.field2 = params[:value]
end
obj.save
end

Selecting Associated records with Rails and simple form

I have a project with partials associated with it. I am trying to create a selection box so that the user can select a parcel from the associated parcels to add to another model associated with the project. What I have below is my attempt that displays the parcels but simply returns the id ie I get
undefined method `each' for "64":String
With 64 being the parcel's ID.
I would also like to allow the user to select multiple parcels or no parcels.
<% #pro_par = #project.parcels %>
<%= f.input :parcels, :collection => #pro_par, :label_method =>:tax_parcel %>
How can I fixe this?
If you already have a associated model like
class Article < ActiveRecord::Base
has_many :parcels
# rest of the code
end
then you can directly call the associating models in the view using simple form
<%= simple_form_for #project do |f| %>
<!-- remaining codes -->
<%= f.association :parcels %>
<%= f.button :submit %>
<% end %>
it automatically lets you select multiple parcels.
Reference: https://github.com/plataformatec/simple_form#associations