Creating a form in a different controller - ruby-on-rails

I have a movies controller and a searches controller. In my searches controller I have the ability to search for a movie from IMDB. I am trying to make a form where I can click Add movie to library and trying to make a new entry in my movies table.
In my searches controller i have
def new
#search = Search.new
#movie = Movie.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #search }
end
end
In my searches index view I have
<%= simple_form_for(#movie) do |f| %>
<%= f.input :title, :as => :hidden, :input_html => { :value => "Skyfall" } %>
<%= f.input :year, :as => :hidden, :input_html => { :value => "2012" } %>
<%= f.input :description, :as => :hidden, :input_html => { :value => "James Bond" } %>
<%= f.association :genres, include_blank: false, :as => :hidden, :input_html => { :value => "some value" } %>
<%= f.button :submit, class: "btn btn-primary" %>
I get a NoMethodError in Searches#index followed by undefined method `model_name' for NilClass:Class.

Your code is ok, I mean, you are putting a new object in #movie on the controller, which is totally fine.
Just make sure you add the new instance of the object in Search#index, like this:
def index
#movie = Movie.new
end
You are getting the error because when the view form is evaluated on this line:
<%= simple_form_for(#movie) do |f| %>
#movie is nil, so model_name is not a method of the object nil and you get the exception.

Related

param is missing or the value is empty

I am new to Rails. Getting the error below. I understand what the issue is, but not sure how to fix it?
Error - param is missing or the value is empty: customer
def customer_params
params.require(:customer).permit(
:first_name,
:last_name,
:email,
:password,
:password_confirmation)
end
end
DetailsController.rb
class My::Account::DetailsController < MyController
def show
#customer = current_user
end
def update
#customer = current_user
#customer.update_attributes(customer_params)
if #customer.errors.any?
render :action => :show
else
redirect_to my_account_details_path, :notice => 'Account details updated'
end
end
private
def customer_params
params.require(:customer).permit(
:first_name,
:last_name,
:email,
:password,
:password_confirmation)
end
end
View
.account.container
.row
= render :partial => '/my/account/sidebar'
.section
%h2 Your account details
= simple_form_for #customer, :url => my_account_details_path, :method => :put, :html => { :class => 'form-horizontal'} do |form|
.field
= form.input :first_name
.field
= form.input :last_name
.field
= form.input :email, :as => :email
%hr
%h4 Leave password fields blank to keep the existing password
%hr
.field
= form.input :password, :input_html => { :value => '' }
.field
= form.input :password_confirmation, :input_html => { :value => '' }
.field-actions
%button.btn.btn-primary{type: "submit"} Save
It is because it is coming through as user and not customer. Which I believe is because you are using current_user which is a User and not a Customer (guessing from the code). Change it to be params.require(:user).permit(blah)

Nested Resources - No route matches

I'm having trouble with nested resources in my Rails4 application. What am I missing here?
I can view the participations/new.html.erb form well but when I submit it's returning "No route matches [POST] "/examinations/1-ales/participations/new" error.
When I'm trying to edit a participation for example this url: localhost:3000/examinations/1-ales/participations/15/edit is returning "ActionController::ParameterMissing in ParticipationsController#edit" and "param not found: participation".
Edit and destroy links in the index.html.erb file causing error:
ActionController::UrlGenerationError in Participations#index
No route matches {:action=>"edit", :controller=>"participations",
:examination_id=>#<Participation id: 12, user_id: 1, examination_id: 1,
payment_status: false, language_preference: "English">,
id=>nil, :format=>nil} missing required keys: [:id]"
routes.rb
resources :examinations do
resources :participations
end
examination.rb
class Examination < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
end
participation.rb
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :examination
end
participations_controller.rb
class ParticipationsController < ApplicationController
before_filter :authenticate_user!
before_action :set_participation, only: [:show, :edit, :update, :destroy]
def index
#participations = Participation.all
end
def show
end
def new
#participation = Participation.new
#examination = params[:examination_id]
end
def edit
#examination = params[:examination_id]
#participation = Participation.new(participation_params)
end
def create
#participation = Participation.new(participation_params)
#participation.user = current_user
respond_to do |format|
if #participation.save
redirect_to(#examination)
format.html { redirect_to #participation, notice: 'Sınav Katılımınız Oluşturuldu!' }
else
render 'new'
format.html { render action: 'new' }
end
end
end
def update
respond_to do |format|
if #participation.update(participation_params)
format.html { redirect_to #participation, notice: 'Participation was successfully updated.' }
else
format.html { render action: 'edit' }
end
end
end
def destroy
#participation.destroy
respond_to do |format|
format.html { redirect_to participations_url }
end
end
private
def set_participation
#participation = Participation.find(params[:id])
end
def participation_params
params.require(:participation).permit(:user_id, :examination_id, :payment_status, :language_preference, :exam_center_preference, :disability)
end
end
app/views/participations/new.html.erb
<%= simple_form_for #participation, as: :participation, url: new_examination_participation_path(#examination) do |f| %>
<%= f.input :user_id, :as => :hidden, :input_html => { :value => current_user.id } %>
<%= f.input :examination_id, as: :hidden %>
<%= f.input :language_preference, collection: ["English", "German"] %>
<%= f.button :submit, "Register to Exam" %>
<% end %>
app/views/participations/edit.html.erb
<%= simple_form_for #participation, as: :participation, url: edit_examination_participation_path(#examination) do |f| %>
<%= f.input :user_id, :as => :hidden, :input_html => { :value => current_user.id } %>
<%= f.input :examination_id, as: :hidden %>
<%= f.input :language_preference, collection: ["English", "German"] %>
<%= f.button :submit, "Register to Exam" %>
<% end %>
app/views/participations/index.html.erb
** Links, which are causing error in the application.
<%= link_to 'Edit', edit_examination_participation_path(participation), :class => 'btn btn-small' %>
<%= link_to 'Delete', participation, method: :delete, data: { confirm: 'bla bla' }, :class => 'btn btn-small btn-danger' %>
Create a Participation:
In app/views/participations/new.html.erb,
Replace
<%= simple_form_for #participation, as: :participation, url: new_examination_participation_path(#examination) do |f| %>
With
<%= simple_form_for #participation, as: :participation, url: examination_participations_path(#examination, #participation) do |f| %>
examination_participations_path would direct the the request to ParticipationsController #create upon form submission. That is what I think you are trying to achieve => Create a Participation
Update a Participation:
In app/views/participations/edit.html.erb,
Replace
<%= simple_form_for #participation, as: :participation, url: edit_examination_participation_path(#examination) do |f| %>
With
<%= simple_form_for #participation, as: :participation, url: examination_participation_path(#examination, #participation) do |f| %>
examination_participation_path would direct the the request to ParticipationsController #update upon form submission.
List all Participations:
In app/views/participations/index.html.erb,
Replace
<%= link_to 'Edit', edit_examination_participation_path(participation), :class => 'btn btn-small' %>
<%= link_to 'Delete', participation, method: :delete, data: { confirm: 'bla bla' }, :class => 'btn btn-small btn-danger' %>
With
<%= link_to 'Edit', edit_examination_participation_path(#examination, participation), :class => 'btn btn-small' %>
<%= link_to 'Delete', examination_participation_path(#examination, participation), method: :delete, data: { confirm: 'bla bla' }, :class => 'btn btn-small btn-danger' %>
NOTE: I would highly recommend you to read about Nested Resources in Rails guides.
You can always do rake routes and see the list of routes that are available to you.

using acts_as_commentable comment_threads method in view for nesting comments in public_activity activities

I am currently trying to add comments via the acts_as_commentable_with_threading gem for all activities via public_activity and I am having trouble capturing each activity to use the the method comment_threads on in order to get each activity's comments. I know as much logic as possible should be in the controllers or models but if I iterate through #activities in the view, how do I take each activity back to the controller to run .comment_threads on? I added helper method :comment_threads to the controller but that doesn't seem to work.
Note: also in general I'm having difficulty using the acts_as_commentable_with_threading with public activity for an activity feed so if anyone knows a better ways to have comments on activity feed items please let me know.
the error I'm getting
comments_controller.rb
class CommentsController < ApplicationController
before_action :get_master
def create
#commentable = params[:comment][:commentable_type].constantize.find(params[:comment][:commentable_id])
#comment = Comment.build_from( #commentable, #master.id, params[:comment][:body] )
if #comment.save
render :partial => "comments/comment", :locals => { comment: #comment }, :layout => false, :status => :created
else
render :js => "alert('error saving comment');"
end
end
private
def get_master
#master = Master.find(current_master.id) if master_signed_in?
end
end
dogs_controller.rb (activities and comments are on the dog show page)
class DogsController < ApplicationController
before_action :get_master
helper_method :comment_threads
def show
#dog = #master.dogs.find(params[:id])
#activities = PublicActivity::Activity.order("created_at desc").where(owner_id: #dog.id, owner_type: "Dog")
#post = #dog.posts.build if signed_in?
#photo = #dog.post_pics.build
#new_comment = Comment.new
end
end
_activity.html.haml partial for dog show page (this is where the comment_threads method is used which is causing the error and which I would somehow like to take the activity back to the controller to use the method there)
%section
= render 'post_form'
- #activities.each do |activity|
.activity
= render_activity activity
.comments
%p Comments
- #comments = activity.trackable_type.constantize.find(activity.trackable_id).comment_threads
= render :partial => 'comments/comment', collection: #comments, as: :comment
= simple_form_for Comment.new, :remote => true do |f|
= f.input :body, :input_html => { :rows => "2" }, :label => false
= f.input :commentable_id, :as => :hidden, :input_html => { :value => activity.trackable_id }
= f.input :commentable_type, :as => :hidden, :input_html => { :value => activity.trackable_type }
= f.button :submit, :class => "btn btn-primary", :disable_with => "Submitting…"
Got it working with following code. I let comments belong to an activity instead of the model an activity was following. Now I just have to get the AJAX working.
_activity.html.haml
%section
= render 'post_form'
- #activities.each do |activity|
.activity
= render_activity activity
.comments
%p Comments
- #comments = activity.comment_threads
= render :partial => 'comments/comment', collection: #comments, as: :comment
= simple_form_for Comment.new, :remote => true do |f|
= f.input :body, :input_html => { :rows => "2" }, :label => false
= f.input :commentable_id, :as => :hidden, :input_html => { :value => activity.id }
= f.input :commentable_type, :as => :hidden, :input_html => { :value => activity }
= f.button :submit, :class => "btn btn-primary", :disable_with => "Submitting…"
comments_controller.rb
class CommentsController < ApplicationController
before_action :get_master
def create
#commentable = PublicActivity::Activity.find(params[:comment][:commentable_id])
#comment = Comment.build_from( #commentable, #master.id, params[:comment][:body] )
if #comment.save
render :partial => "comments/comment", :locals => { comment: #comment }, :layout => false, :status => :created
else
render :js => "alert('error saving comment');"
end
end
private
def get_master
#master = Master.find(current_master.id) if master_signed_in?
end

Instance Variable string collection lost after render => 'new'

When I get an error creating a quote, it renders the same page it was just on and displays the errors. Unfortunately two inputs are drop down menus of strings, and they disappear when the refresh happens.
I've looked at Rail 3: instance variable not available after redirection which talks about sessions, which looks like it could be the right way to go but I'm not sure. Any help would be appreciated.
quotes controller
def new
#quote = Quote.new
#quote.items.build
#types = ["T-Shirt", "Hoodie", "Sweatpants"]
#colors = ["White", "Black", "Red", "Blue", "Green"]
end
def create
#quote = Quote.new(params[:quote])
respond_to do |format|
if #quote.save
format.html { redirect_to root_url }
flash[:success] = "Your quote is being approved. You will recieve an email shortly!"
format.json { render json: #quote, status: :created, location: #quote }
else
format.html { render :action => 'new' }
format.json { render :json => #quote.errors, :status => :unprocessable_entry }
flash[:error] = "Quote failed to create! Try again!"
end
end
end
form partial
<!-- item form -->
<%= f.input :make, collection: #types, label: 'Thread Type' %>
<%= f.input :amount, label: 'How Many' %>
<%= f.input :color, collection: #colors %>
<!-- nested form for creating a design of an item -->
<%= f.simple_fields_for :designs, :html => { :multipart => true } do |designform| %>
<%= render "customdesign", g: designform %>
<% end %>
<!-- add/remove another design -->
<%= f.link_to_add "Add Design", :designs %>
<%= f.input :note, :input_html => { :cols => 50, :rows => 3 }, label: 'Special Notes or Requests' %>
<%= f.link_to_remove "Remove" %>
#colors and #types are only set in the new action, and not the create action. Rendering a template does not automatically call the action method in the controller; it goes straight to the view.
A possible solution is to define helper methods for these lists:
# app/helpers/quote_helper.rb
module QuoteHelper
def possible_types
["T-Shirt", "Hoodie", "Sweatpants"]
end
end
And in your view:
<%= f.input :make, collection: possible_types, label: 'Thread Type' %>

Don't save record after install sorcery or formtastic

I try to rails console for me table object but when I install sorcery or formtastic (i don't know) my record don't save.
Model
class Depositi < ActiveRecord::Base
attr_accessible :code, :description
validates_presence_of :code, :message => 'Code required!'
end
View
<%= semantic_form_for(#depositi) do |f| %>
<%= f.inputs 'Dati Deposito' do %>
<%= f.input :code, :label => 'Codice Deposito:' , :wrapper_html => { :class => 'fl' }, :required => true %>
<%= f.input :description, :label => 'Descrizione:', :wrapper_html => { :class => 'fl'}, :input_html => {:size => '15'} %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :button, :label => 'Save' %>
<% end %>
<% end %>
this is my debug:
utf8 ✓
authenticity_token kVQ5OOkmGxqoZgbvteJ80M+lLHwopWEY0SI4YetQYsg=
depositi {"code":"001","description":"Test this is test"}
button
action new
Controller
def create
#depositi = Depositi.new(params[:depositi])
respond_to do |format|
if #depositi.save
format.html { redirect_to #depositi, notice: 'Record salvato con successo!' }
format.json { render json: #depositi, status: :created, location: #depositi }
else
format.html { render action: "new" }
format.json { render json: #depositi.errors, status: :unprocessable_entity }
end
end
end
This record don't save! I'm property login, i don't have error..why don't save?
RESOLVED: The problem was in my view up of <%= semantic_form_for(#depositi) do |f| %>
I write that was in conflict with formtastic render form.

Resources