Strong_parameters in view with mixed models - ruby-on-rails

I am using strong parameters and when I try to save the following form I get the following message.
undefined method `household_params' for # '<'VisitsController:0x007fa88deec428'>'
I am confused because I am using the visits controller and not the households controller - The visit is associated with a household as shown below and I call the view with the following code:
= link_to 'New Visit', {controller: 'visits', action: 'new',
household_id: household.id, method: 'post'}, class: 'btn btn-primary'
The Form is:
%h3 Household: #{household.name}
%h4 Household Members: #{household.neighbors.count}
%h4 Visits: #{household.visits.count}
= simple_form_for visit do |f|
= f.input :visited_on, :label => 'Visited On', as: :date_picker, input_html: { class: 'span2' }
= f.input :starch, :label => false, collection: ['Beans','Rice','Potatoes'],selected: 'Beans'
= f.input :cereal, :label => false, collection: ['Cereal','Grits','Oatmeal']
= f.input :option1, :label => false, collection: ['Peanut Butter Jelly', 'Deserts','Baby Fromula'], prompt: 'Options'
= f.input :items_received, :label => 'Special Needs',input_html: {rows: 4, class: 'span9' }
= f.input :notes, :label => 'Notes',input_html: {rows: 4, class: 'span9' }
= f.button :submit, :class => 'btn-primary', :label=> 'Save'
The form works fine without the three lines that display information about the household
I am thinking strong_parameters is getting confused

Related

Ruby on Rails - Can't access the updated model from Slim View

I have a view for editing my model. I am using Slim views.
My model_name is 'cupcake'
= simple_form_for [:admin, :order, record] do |f|
.row
.col-md-6
label for="_kind" Kind
= f.select :kind, [['Clipart', :clipart], ['Image', :image], ['Message', :message]], { selected: record[:kind] }, value: record[:kind], class: 'form-control', :input_html => { :name => "cupcake[:kind]" }
- if #record[:kind] == 'image'
.col-xs-12
= f.input :image, as: :shrine_file
- elsif #record[:kind] == 'clipart'
.col-md-6
label for="_clipart" Clipart
= f.select :clipart_id, options_for_select(#cliparts.map { |c| [c['name'], c['id'], {'value' => c['id']}] }, selected_key = selected_clipart(record).id), { selected: selected_clipart(record).name }, class: 'form-control', :input_html => { :name => "cupcake[:clipart_id]" }
- else
.col-md-6
= f.input :text_line_style, collection: ["Three Line Text","Single Line Text"], checked: record['first_line'] ? 'Three Line Text' : "Single Line Text", as: :radio_buttons, item_wrapper_class: "inline"
.row
.col-xs-12
hr
button.btn.btn-success.btn-lg
= fa_icon('check')
| Save
- redirect_location = [:admin, :cliparts]
= link_to redirect_location, class: 'btn btn-primary btn-lg'
= fa_icon('chevron-left')
| Back
.col-xs-12
- if f.object.created_at.present?
p.help-block Created at: #{f.object.created_at}
- if f.object.updated_at.present?
p.help-block Updated at: #{f.object.updated_at}
I want to update the view based on the kind selected. Currently, it is not updating the view if I select a different kind. When I try to access cupcake[:kind], it is showing undefined error. Any way to access the cupcake object? I looked for it in the f object. But, I couldn't find it.
Anyone, please help?

One simple_form_for both New and Edit methods

Trying to find a way to use one simple_form_for for two functions- new and edit.
= simple_form_for #news_item, :url => url_for(:action => 'create', :controller => 'news_items'),
:method => 'post' do |f|
= f.input :title, input_html: { class: 'title-input' }
= f.input :contents, input_html: { class: 'contents-input' }
.actions
= f.submit 'Save', class: 'btn btn-success'
Currently, this form will create new form every time hit submit button but can I send this to :url => url_for(:action => 'update') as well?
Try this:
= simple_form_for #news_item, url: (#news_item.new_record? ? news_items_index_path(#news_item) : news_items_path(#news_item)), method: (#news_item.new_record? ? :post : :patch) do |f|
Or something like that, you get the picture...

Simple_Form: Make two forms work with eachother (dropdown and search)

I have two forms, one containing a dropdown where the user can choose how a list is beeing sorted, the other one containing a searchfield, where the user can search through that list. Now if a user searches for "test" and ten results show up, I want the user to be able to choose from the dropdown, how the results are beeing sorted. Accordingly if he sorts the whole list, I want himto be able to search through the list, with the results showing up in the sorted way he choose before. Due to code restrictions I have to keep those two inputs in different forms.
Here is the sort-dropdown:
= simple_form_for path, :method => "get", html: {id: "sortform"} do |f|
= f.input :sort, :collection => [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :label => false, :required => false, :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')
And here is the search:
= simple_form_for path, :method => 'get', :label => t(:'videos.search'), html: {id: "search-form"} do |f|
= f.input :q, { input_html: { class: 'form-control searchbar', :name => :q, id: "search", :value => params[:q]}, :placeholder => t(:'videos.search'), :required => false, :label => false}
Is it possible to keep the two inputs seperate or would it be way easier to use just one form?
You can use separate forms if you need to, just store the other parameter in a hidden field in each of the forms.
= simple_form_for path, :method => "get", html: {id: "sortform"} do |f|
= f.input :sort, :collection => [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :label => false, :required => false, :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')
= f.input :q, as: :hidden, input_html: { :name => :q, :value => params[:q] }
= simple_form_for path, :method => 'get', :label => t(:'videos.search'), html: {id: "search-form"} do |f|
= f.input :q, { input_html: { class: 'form-control searchbar', :name => :q, id: "search", :value => params[:q]}, :placeholder => t(:'videos.search'), :required => false, :label => false}
= f.input :sort, as: :hidden, collection: [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')

Devise multiple sign up pages Rails 4

I want to be able to create different sign up pages for each event in my web app.
I'm using Devise, Cancan and Rolify.
Below is my signup form at the moment:
.container
%h2
Sign up
.tab-content
.tab-pane.active{:id => "signup"}
= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'form-signin'}) do |f|
= f.error_notification
= f.input :email, :required => true, :autofocus => true, input_html: { class: "form-control"}, :placeholder => "Email"
- if f.object.password_required?
= f.input :password, :required => true, input_html: { class: "form-control"}, :placeholder => "Password"
= f.input :password_confirmation, :required => true, input_html: { class: "form-control"}, :placeholder => "Password Confirm"
= f.input :first_name, :required => true, :autofocus => true, input_html: { class: "form-control"}, :placeholder => "First name"
= f.input :last_name, :required => true, :autofocus => true, input_html: { class: "form-control"}, :placeholder => "Last name"
= f.button :submit, "Sign up", :class => "btn btn-lg btn-primary btn-block"
= render "devise/shared/links"
Is there a way we can add in fields from events table so those fields are populated with different urls so each one of them have dedicated signup page for visitors?

Limit simple_form_for associated records number in Ruby on Rails

I have a large psychological test of 251 question. Each user can complete that test many times. So I created Summary model to represent each completion. Each Summary has many Answers. For each Summary I created a form that represents collection of answers, using Slim templater and simple_form gem:
= simple_form_for(#summary) do |f|
= f.simple_fields_for :answers do |a|
.question
= a.input :question_id, as: :hidden
div= a.object.question.title
- if a.object.question.kind_of? SpiritualityQuestion
ol class="sortable"
- a.object.question.sortable_variants.each do |sortable_variant|
= content_tag_for :li, sortable_variant
= sortable_variant.title
= a.input :text_data, as: :hidden, input_html: { class: 'sortable_data' }
- elsif a.object.question.kind_of? MultilineQuestion
div Time remaining: <span class="time">60</span> s.
= button_tag 'Start', type: 'button', class: 'start_button btn btn-primary'
= a.input :text_data, label: false, input_html: { class: 'span8 timed_text', cols: '60', rows: '20', disabled: true }
- else
= a.association :variant, collection: a.object.question.variants, as: :radio, label: false
br
= f.input :user_id, as: :hidden
= f.input :psy_test_id, as: :hidden
.actions
= f.button :submit, value: 'Save', class: 'btn btn-large btn-success'
And I have related controller action:
#summary = Summary.where(completed: false, user: current_user, psy_test: PsyTest.first)
.includes(:answers => { :question => :variants })
.first_or_initialize
#summary.build_answers if #summary.new_record?
Summary.build_answers:
def build_answers
# Creating answers for questions
psy_test.questions.includes(:variants).each do |q|
answers.new(question: q)
end
end
Now I'm trying to make the test to be paginated, because it's very large and the form generates very slowly. So I want to add limit and offset to answers. Something like that:
= f.simple_fields_for answers.limit(#limit).offset(#offset) do |a|
How it can be made?
I've looked to do field_for source and found simple answer, which I wasn't able find in any guide:
= f.simple_fields_for :answers, #summary.answers.limit(#limit).offset(#offset) do |a|

Resources