Rails passing value from view to controller - ruby-on-rails-5

I tried to generate some random no of string. But the number of generated string is decided by user.
User will enter the number of string in view page and it will be received by controller and Faraday will generate the number of paragraph user want, But i am not able to do this.
ApplicationController
class ApplicationController < ActionController::Base
private
def baconipsum
#baconipsum ||= Faraday.new("https://baconipsum.com/") do |f|
f.response :json
end
end
end
ArticlesController:
# frozen_string_literal: true
class ArticlesController < ApplicationController
http_basic_authenticate_with name: 'deba', password: '12345', except: %i[index show]
before_action :set_value
def index
#articles = Article.all
end
def show
#bacon = baconipsum.get("api/", type: 'all-meat',paras: #value).body
#article = Article.find(params[:id])
end
def showData
#value = params[:value] #tried to get value from user and using set_value function tried to make it global, so that show method will get the new value of value from showData method.
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
def update
#article = Article.find(params[:id])
if #article.update(article_params)
redirect_to #article
else
render 'edit'
end
end
def delete
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text)
end
def set_value
#value = 0
end
end
views from getting user input:
<%= form_for:article do %>
<label for="value">Value</label>
<%= text_field_tag :value %>
<%= submit_tag "Submit" %>
<% end %>
routes.rb
# frozen_string_literal: true
Rails.application.routes.draw do
root 'articles#index'
delete 'articles/:id', to: 'articles#delete'
resources :articles do
resources :comments
end
end

Related

Rails error: ArgumentError in ArticlesController#create wrong number of arguments (2 for 1)

i have tutorial on Rails from here:
Rails5
and wanted to add a field "attachment" to the form.
articles_controller.rb is as follows:
class ArticlesController < ApplicationController
http_basic_authenticate_with name: "gerrit", password: "Ifb6K54WVs7U", except: [:index, :show]
def index
#articles = Article.all
end
def show
#article = Article.find(params[:id])
end
def new
#article = Article.new
end
def edit
#article = Article.find(params[:id])
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
def update
#article = Article.find(params[:id])
if #article.update(article_params)
redirect_to #article
else
render 'edit'
end
end
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text, :attachment)
end
end
For this i added paperclip and turbolink5.
I rerun all configuring steps in the tutorial but I always get the error in the title:
Extracted source (around line #24):
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
I do not use single params here, but article_params!?
Any help appreciated!
As said in the last comment:
I updated paperclip to v5.1.0 and the error was gone.

Rails File Upload with Nested Attribute, "file_content" is blank when file gets saved

I've been following this tutorial to upload file to db Rails File Upload
.
The problem is file_contents field is blank when file gets saved and I'm uploading file with nested_attributes.
subject.rb //model
class Subject < ApplicationRecord
has_many :documents
accepts_nested_attributes_for :documents
end
document.rb //model
class Document < ApplicationRecord
belongs_to :subject
def initialize(params = {})
#file = params.delete(:file)
super
if #file
self.filename = sanitize_filename(#file.original_filename)
self.content_type = #file.content_type
self.file_contents = #file.read
end
end
private
def sanitize_filename(filename)
return File.basename(filename)
end
end
_form.html.erb //view
<%= form_for(subject) do |f| %>
.......
<div class="uk-form-row">
<%= f.fields_for :documents do |d| %>
<%= d.file_field :file, class:"uk-width-1-1 uk-form-large", placeholder:"upload document" %>
<% end %>
</div>
............
<% end %>
subjects_controller.rb
def new
#subject = Subject.new
#subject.documents.build
end
def create
#subject = Subject.new(subject_params)
respond_to do |format|
if #subject.save
format.html { redirect_to #subject, notice: 'Lesson was successfully created.' }
else
format.html { render :new }
end
end
end
private
def subject_params
params.require(:subject).permit(:title, documents_attributes:[:file])
end
Am I in the right way ? What am I missing ? I need your thoughts.
Thanks,

undefined local variable or method `employee_params' for #<EmployeesController:0x37b7f68>

Controller
class EmployeesController < ApplicationController
def new
#employee = Employee.new
end
def create
#employee = Employee.new(employee_params)
if #employee.save
format.html { redirect_to #employee, notice: 'Status was successfully created.' }
else
render "new"
end
private
def employee_params
params.require(:employee).permit(:firstname, :lastname)
end
end
end
models/employee.rb
class Employee < ActiveRecord::Base
attr_accessor :firstname, :lastname
end
new.html.erb
Enter new information
<hr>
<%= form_for #employee do |f| -%>
FirstName:<%= f.text_field :firstname%><br />
LastName :<%= f.text_field :lastname%><br />
<%= f.submit "submit"%>
<% end %>
my output:
http://grab.by/DA3A
this is my screenshot..please help me to figure out this problem.
You mistyped it (in your controller) bro. It should be like:
class EmployeesController < ApplicationController
def new
#employee = Employee.new
end
def create
#employee = Employee.new(employee_params)
if #employee.save
format.html { redirect_to #employee, notice: 'Status was successfully created.' }
else
render "new"
end
end
private
def employee_params
params.require(:employee).permit(:firstname, :lastname)
end
end
I don't think I need to elaborate this :)

Polymorphic Comments with Ancestry Problems

I am trying to roll together two Railscasts: http://railscasts.com/episodes/262-trees-with-ancestry and http://railscasts.com/episodes/154-polymorphic-association on my app.
My Models:
class Location < ActiveRecord::Base
has_many :comments, :as => :commentable, :dependent => :destroy
end
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
My Controllers:
class LocationsController < ApplicationController
def show
#location = Location.find(params[:id])
#comments = #location.comments.arrange(:order => :created_at)
respond_to do |format|
format.html # show.html.erb
format.json { render json: #location }
end
end
end
class CommentsController < InheritedResources::Base
def index
#commentable = find_commentable
#comments = #commentable.comments.where(:company_id => session[:company_id])
end
def create
#commentable = find_commentable
#comment = #commentable.comments.build(params[:comment])
#comment.user_id = session[:user_id]
#comment.company_id = session[:company_id]
if #comment.save
flash[:notice] = "Successfully created comment."
redirect_to :id => nil
else
render :action => 'new'
end
end
private
def find_commentable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
end
In my locations show view I have this code:
<%= render #comments %>
<%= render "comments/form" %>
Which outputs properly. I have a _comment.html.erb file that renders each comment etc. and a _form.html.erb file that creates the form for a new comment.
The problem I have is that when I try <%= nested_comments #comments %> I get undefined method 'arrange'.
I did some Googling and the common solution to this was to add subtree before the arrange but that throws and undefined error also. I am guessing the polymorphic association is the problem here but I am at a loss as to how to fix it.
Dumb mistake... forgot to add the ancestry gem and required migration which I thought I had already done. The last place I checked was my model where I eventually discovered my error.

fields_for not working on update

I think this is a stupid mistake… When i create a record, my "resources" and "page_settings" tables getting populated.
But my "page_setting" does nothing when i try to update the record.
My models:
class Resource < ActiveRecord::Base
has_one :page_setting
accepts_nested_attributes_for :page_setting
end
class PageSetting < ActiveRecord::Base
belongs_to :resource
end
Here is the resources controller:
class ResourcesController < ApplicationController
# Initialize resource and belonging type model
before_filter :build_resource_and_type, :only => [:new, :create]
before_filter :get_resource_and_type, :only => [:edit, :update]
def new
end
def create
if #resource.save
flash[:notice] = "Resource wurde erstellt"
redirect_to root_url
else
flash[:error] = "Resource konnte nicht erstellt werden"
render :action => 'new'
end
end
def edit
end
def update
if #resource.update_attributes(params[:resource])
flash[:notice] = "#{#type_name} #{#resource.title} wurde aktualisiert"
redirect_to root_url
else
flash[:error] = "#{#type_name} #{#resource.title} konnte nicht aktualisiert werden"
render :action => 'edit'
end
end
private
def build_resource_and_type
# Get type from URL param (new action) or hidden field param (create action)
type = params[:type_name] || params[:resource][:type_name]
#resource = current_user.microsite.resources.new(params[:resource])
#resource.type_name = type
# Build belonging model depending on type param
case type
when 'page'
#resource.build_page_setting(params[:page_setting])
#type_name = 'page'
end
end
def get_resource_and_type
#resource = current_user.microsite.resources.find(params[:id])
#type_name = #resource.type_name
end
end
And the essential part of my resource form:
<%= form_for #resource do |resource_form| %>
<%= resource_form.hidden_field :type_name, :value => #type_name %>
…
<%= fields_for #resource.page_setting do |page_form| %>
<%= page_form.label :content, "Text" %>
<%= page_form.text_area :content %>
<% end %>
<% end %>
You have to make a small change in Your resource form:
<%= f.fields_for :page_setting, #resource.page_setting do |page_form| %>
Then it should work, like You want to.