I want to add comment in post in simply ruby on rails application.
Resolved.
My Controller:
class CommentsController < ApplicationController
before_filter :authenticate_user!
def create
binding.pry
#adventure = Adventure.find(params[:adventure_id])
#comment = #adventure.comments.create(comment_params)
if #comment.save
flash[:notice] = "Comment has been created."
redirect_to #adventure
else
flash.now[:danger] = "error"
end
end
def comment_params
params.require(:comment).permit(:body)
end
end
class Comment < ActiveRecord::Base
belongs_to :adventure
end
Related
I have the following code:
class UrlsController < ApplicationController
def new
#shortened_url = Url.new
end
def create
#shortened_url = Url.new(params[:url])
if #shortened_url.save
flash[:shortened_id] = #shortened_url.id
redirect_to new_url_url
else
render :action => "new"
end
end
def show
#shortened_url = Url.find(params[:id])
redirect_to #shortened_url.url
end
end
I'm getting the error I know is related to the required parameters and permit. Any one can tell me how the method need to be written?
this is the model:
class Url < ActiveRecord::Base
validates :url, :presence => true
end
the error is when submitting the form. I get
ActiveModel::ForbiddenAttributesError in UrlsController#create
It needed the use of strong parameters
class UrlsController < ApplicationController
def new
#shortened_url = Url.new
end
def create
#shortened_url = Url.new(shortened_url)
if #shortened_url.save
flash[:shortened_id] = #shortened_url.id
redirect_to new_url_url
else
render :action => "new"
end
end
def show
#shortened_url = Url.find(params[:id])
redirect_to #shortened_url.url
end
private
def shortened_url
params.require(:url).permit!
end
end
I am trying to implement multi-select in a form in rails 4, using Semantic UI. I want users to be able to select multiple categories for each post. So far, I am able to display the dropdown select field which pulls all the categories from the database as follows:
<%= form_for #post, :html => {:class => "ui form bk-form group"} do |f| %>
<label>Post title:</label><br />
<%= f.text_field :title, autofocus: true %>
<label>Choose a category:</label><br />
<%= f.select(:category_ids,options_for_select(Category.all.collect{|x| [x.name,x.id,class:'item']}),{prompt:'Select categories'}, multiple: true, class:'ui fluid dropdown selection multiple')%>
<% end %>
With this, I am able to create and save posts and the data is inserted in the database. However, when I try to edit an article, the pre-selected categories do not show. I have tried to set the value: #post.categories option in the select field and still cannot get the existing categories to show. Thanks in advance for your thoughts.
UPDATED
Models are as follows:
class Post < ActiveRecord::Base
has_many :post_categories
has_many :categories, through: :post_categories
end
class Category < ActiveRecord::Base
has_many :post_categories
has_many :posts, through: :post_categories
end
class PostCategory < ActiveRecord::Base
belongs_to :post
belongs_to :category, :counter_cache => :posts_count
end
Then my posts_controller.rb
class PostsController < ApplicationController
before_action :set_post, only: [:edit, :update, :show]
def index
#posts = Post.all
end
def new
end
def create
#post = Post.new(post_params)
#post.user = current_user
if #post.save
flash[:notice] = "Post was successfully created"
redirect_to user_posts_path
else
flash[:alert] = "Oh Snap!! We could not save your post"
render 'new'
end
end
def edit
end
def update
if #post.update(post_params)
flash[:notice] = "Post was successfully updated"
redirect_to user_posts_path
else
flash[:alert] = "Oh Snap!! We could not update your post"
render 'edit'
end
end
private
def post_params
params.require(:post).permit(:title, :description, :published, :tag_list, category_ids: [])
end
def set_post
#post = Post.find(params[:id])
end
end
I'm having trouble getting an integration test to pass. A user creates a product, which can have many pictures. The product's new.html.erb contains file field <%= file_field_tag "images[]", type: :file, multiple: true %> The code works fine, I'm just having problems creating a working integration test.
products_creation_test.rb:
require 'test_helper'
class ProductsCreationTest < ActionDispatch::IntegrationTest
.
.
.
test "should create new product with valid info" do
log_in_as(#user)
pic1 = fixture_file_upload('test/fixtures/cat1.jpg', 'image/jpg')
pic2 = fixture_file_upload('test/fixtures/profile.png', 'image/png')
assert_difference 'Product.count', 1 do
post user_products_path(#user), product: { title: 'test',
description: 'test',
price: '5.99',
images: [pic1, pic2] }
end
assert_redirected_to #user
#user.reload
newprod = #user.products.last
pics = newprod.pictures.all
assert_equal 2, pics.count
end
end
This fails the last assertion, stating that there are no pictures associated with the new product, when there should be 2. inspecting the pics variable, i get the following error: RuntimeError: #<ActiveRecord::AssociationRelation []
What am I missing?
my model structure is as follows:
User has_many Products, Products has_many Pictures
products_controller.rb:
class ProductsController < ApplicationController
before_action :valid_user, only: [:new, :create, :edit, :update]
.
.
.
def create
#product = current_user.products.build(product_params)
if #product.save
# to handle multiple images upload on create
if params[:images]
params[:images].each { |image|
#product.pictures.create(image: image)
}
end
flash[:success] = "Product Created!"
redirect_to current_user
else
flash[:alert] = "Something went wrong."
render :new
end
end
.
.
.
end
pictures_controller.rb:
class PicturesController < ApplicationController
def create
#picture = Picture.new(picture_params)
#picture.save
end
def destroy
end
private
def picture_params
params.require(:picture).permit(:product_id, :image, :_destroy)
end
end
picture.rb:
class Picture < ActiveRecord::Base
belongs_to :product
mount_uploader :image, PicturesUploader
validates_integrity_of :image
validates_processing_of :image
validates :image, file_size: { less_than: 10.megabytes }
end
Solved as follows:
revised products_creation_test.rb:
require 'test_helper'
class ProductsCreationTest < ActionDispatch::IntegrationTest
.
.
.
test "should create new product with valid info" do
log_in_as(#user)
pic1 = fixture_file_upload('test/fixtures/cat1.jpg', 'image/jpg')
pic2 = fixture_file_upload('test/fixtures/profile.png', 'image/png')
assert_difference 'Product.count', 1 do
post user_products_path(#user), product: { title: 'test',
description: 'test',
price: '5.99',
}, images: [pic1, pic2]
end
assert_redirected_to #user
newprod = assigns(:product)
assert_equal 2, newprod.pictures.count
end
end
class User < ActiveRecord::Base
has_one :recovery
end
class Recovery < ActiveRecord::Base
attr_accessor :email
end
class RecoveriesController < ApplicationController
def create
#user=User.find(19)
#recovery=#user.create_recovery!
if #recovery.save
redirect_to :root
end
end
end
After create #recovery.user_id is nil in database
But if I remove the redirect, as here
class RecoveriesController < ApplicationController
def create
#user=User.find(19)
#recovery=#user.create_recovery!
end
end
user_id take its value.
Whats wrong? Rails 4
Here is what should work:
In Your RecoveriesController:
class RecoveriesController < ApplicationController
def create
#user=User.find(19)
#recovery = Recovery.new(user:#user)
if #recovery.save
redirect_to :root
end
end
end
And, I am sure You have it, but that is not listed in Your Recovery model:
belongs_to :user
Each of my businesses belongs_to a category, so I'm trying to figure out how to show related businesses.
I'd like to limit it to three related businesses.
Thank you!
class BusinessesController < ApplicationController
respond_to :html, :xml, :json
before_filter :restrict_access, :only => [:edit, :update]
def index
#businesses = Business.all
respond_with(#businesses)
end
def show
#business = Business.friendly.find(params[:id])
if request.path != business_path(#business)
redirect_to #business, status: :moved_permanently
end
end
def new
#business = Business.new
3.times { #business.assets.build }
respond_with(#business)
end
def edit
#business = get_business(business_params)
#avatar = #business.assets.count
#avatar = 3-#avatar
#avatar.times {#business.assets.build}
end
def create
#business = Business.new(business_params)
if #business.save
redirect_to #business, notice: 'Business was successfully created.'
else
3.times { #business.assets.build }
render 'new'
end
end
Update the show action as below:
def show
#business = Business.friendly.find(params[:id])
#others = #business.category.businesses.where.not(id: #business.id).limit(3)
if request.path != business_path(#business)
redirect_to #business, status: :moved_permanently
end
end
#others will have <=3 businesses other than the current business belonging to the same category of current business. You can then use #others in the show view easily.