ruby - Embed a Rails form partial into another page -
i'm building rails 4.2.0 app contact page (this page have semi-empty controller). i'm trying embed form partial controller.
here code (minus text):
<% if user_signed_in? %> <% render 'enquiries/form' %> <% end %>
when run error 'first argument in form cannot contain nil or empty'.
my enquiries form looks basic rails form:
<%= form_for @enquiry |f| %> <% if @enquiry.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@enquiry.errors.count, "error") %> prohibited enquiry being saved:</h2> <ul> <% @enquiry.errors.full_messages.each |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :subject, "subject:" %><br> <%= f.text_field :subject %> </div> <div class="field"> <%= f.label :e_description, "description:" %><br> <%= f.text_area :e_description %> </div> <div class="actions"> <%= f.submit %> </div>
what possible reason error? or there better way of embedding view another?
update/edit:
here's routes:
devise_for :users resources :rooms resources :viewings end resources :rmcats resources :extras resources :extracats resources :enquiries root :to => redirect('/pages/home') 'pages/home' 'pages/contactus'
and enquiry controller:
class enquiriescontroller < applicationcontroller before_action :set_enquiry, only: [:show, :edit, :update, :destroy] # /enquiries def index @enquiries = enquiry.all end # /enquiries/1 def show end # /enquiries/new def new @enquiry = enquiry.new end # /enquiries/1/edit def edit end # post /enquiries def create @enquiry = enquiry.new(enquiry_params) if @enquiry.save redirect_to @enquiry, notice: 'enquiry created.' else render :new end end # patch/put /enquiries/1 def update if @enquiry.update(enquiry_params) redirect_to @enquiry, notice: 'enquiry updated.' else render :edit end end # delete /enquiries/1 def destroy @enquiry.destroy redirect_to enquiries_url, notice: 'enquiry destroyed.' end private # use callbacks share common setup or constraints between actions. def set_enquiry @enquiry = enquiry.find(params[:id]) end # allow trusted parameter "white list" through. def enquiry_params params.require(:enquiry).permit(:subject, :e_description) end end
this pages controller:
class pagescontroller < applicationcontroller around_filter :resource_not_found # def home # end private # if resource not found redirect root , flash error. # => pages needed should 404. def resource_not_found yield rescue activerecord::recordnotfound redirect_to root_url, :notice => "page not found." end end
edit:
log:
started "/pages/contactus" ::1 @ 2015-03-21 01:05:25 +0000 processing enquiriescontroller#new html [1m[35muser load (0.0ms)[0m select "users".* "users" "users"."id" = ? order "users"."id" asc limit 1 [["id", 1]] rendered enquiries/_form.html.erb (0.0ms) rendered pages/contactus.html.erb within layouts/application (0.0ms) completed 200 ok in 235ms (views: 234.6ms | activerecord: 0.0ms)
it telling @enquiry
nil @ time trying render form. need call new
action create @enqiury
form represent.
you change route to:
get 'pages/contactus' => 'enquiries#new'
then in enquiry controller:
def new @enquiry = enquiry.new render 'pages/contactus' end
edit:
ok, combine friends systems put in answer:
<% if user_signed_in? %> <%= render 'enquiries/form' enquiry: @enquiry %> <% end %>
and change instance of @enquiry
in form enquiry
this because need pass variable partial.
Comments
Post a Comment