One simple_form_for both New and Edit methods - ruby-on-rails

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...

Related

Ruby on Rails: render form on a shared folder

I want to put the form on a shared folder for DRY the code and render this form for [:admin, :posts] and :posts.
So I create a folder and put the form on app/views/shared/_form.html.slim
- if params[:admin][:posts]
post = [:admin, #post]
- if params[:posts]
post = #post
= simple_form_for(post, :html => { :multipart => true }) do |f|
= f.association :categories, label: "Select the Categories:", as: :check_boxes , collection: #categories.map{|c| [c.name, c.id]}, include_hidden: false
= f.input :title
= f.input :subtitle
- if #post.attachment.present?
.attachment
p
= image_tag(#post.attachment.thumb.url, alt: 'Image', class: "img-responsive img-thumbnail")
= f.check_box :remove_attachment
| Remove image
br
.text-center
small
sample
= "File_size #{number_to_human_size(#post.attachment.size)}"
= f.input :attachment, as: :file, label: "File"
= f.input :attachment_cache, as: :hidden
= f.input :remote_attachment_url, label: "Enter URL to an image"
= f.input :content, size: "150x150"
= f.button :submit, class: 'btn-primary'
and then on
app/views/admin/post/new.html.slim and app/views/posts/edit.html.slim
I added the render:
== render 'shared/form', post: #post
So I tried to run and I have this error:
ActionView::Template::Error (undefined method `[]' for nil:NilClass):
1: - if params[:admin][:posts]
2: post = [:admin, #post]
3: - if params[:posts]
4: post = #post
This is just an idea that I have. it's ok for do this in this case or forget to do this?
routes:
rake routes | grep post
Running via Spring preloader in process 2187
admin_posts POST /admin/posts(.:format) admin/posts#create
new_admin_post GET /admin/posts/new(.:format) admin/posts#new
admin_post DELETE /admin/posts/:id(.:format) admin/posts#destroy
nullify_posts_admin_user PATCH /admin/users/:id/nullify_posts(.:format) admin/users#nullify_posts
root GET / posts#index
published_posts GET /posts/published(.:format) posts#published
draft_posts GET /posts/draft(.:format) posts#draft
recent_posts GET /posts/recent(.:format) posts#recent
publish_post PATCH /posts/:id/publish(.:format) posts#publish
unpublish_post PATCH /posts/:id/unpublish(.:format) posts#unpublish
posts GET /posts(.:format) posts#index
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
I see that what you're trying to do is figure out whether you're in the admin section or the standard post section and change the url of the form that way.
This is a bit of a non-standard way of doing it.
Generally speaking, what I've seen is that only the body of the form is in the shared form section, but the action is saved in the outside partial so eg:
app/views/admin/post/new.html.slim:
= simple_form_for(post, :html => { :multipart => true }) do |f|
== render 'shared/post_form', f: f, post: post
app/views/shared/_post_form.html.slim:
= f.association :categories, label: "Select the Categories:", as: :check_boxes , collection: #categories.map{|c| [c.name, c.id]}, include_hidden: false
= f.input :title
= f.input :subtitle
- if post.attachment.present?
.attachment
p
= image_tag(post.attachment.thumb.url, alt: 'Image', class: "img-responsive img-thumbnail")
= f.check_box :remove_attachment
| Remove image
br
.text-center
small
sample
= "File_size #{number_to_human_size(post.attachment.size)}"
= f.input :attachment, as: :file, label: "File"
= f.input :attachment_cache, as: :hidden
= f.input :remote_attachment_url, label: "Enter URL to an image"
= f.input :content, size: "150x150"
= f.button :submit, class: 'btn-primary'
Note: if you pass post as a local variable into a form, you should use it with post not #post (because #post completely ignores the local variable you passed in and goes back to whatever came from the controller, in which case why bother passing in the local variable?)
You should also probably never call another variable post inside the template... as it overrides the old post variable and is then gone. Name it something different eg in this case, if you REALLY wanted to use the template the way you have been, you could call it post_url_params
So following the idea of the #Taryn East, I put this and work's:
app/views/admin/posts/new.html.slim
= title("New Post")
.header
h1 New Post
== render 'form', post: #post
app/views/admin/posts/edit.html.slim
= title("Edit Post", #post.title)
.header
h1 Edit Post
== render "form", post: #post
app/views/admin/posts/_form.html.slim
= simple_form_for([:admin, post], :html => { :multipart => true }) do |f|
== render "shared/post_form", f: f, post: post
app/views/posts/_form.html.slim
= simple_form_for(post, :html => { :multipart => true }) do |f|
== render "shared/post_form", f: f, post: post
app/shared/_post_form.html.slim
= f.association :categories, label: "Select the Categories:", as: :check_boxes , collection: #categories.map{|c| [c.name, c.id]}, include_hidden: false
= f.input :title
= f.input :subtitle
- if post.attachment.present?
.attachment
p
= image_tag(post.attachment.thumb.url, alt: 'Image', class: "img-responsive img-thumbnail")
= f.check_box :remove_attachment
| Remove image
br
.text-center
small
sample
= "File_size #{number_to_human_size(post.attachment.size)}"
= f.input :attachment, as: :file, label: "File"
= f.input :attachment_cache, as: :hidden
= f.input :remote_attachment_url, label: "Enter URL to an image"
= f.input :content, input_html: { rows: '15' }
= f.button :submit, class: 'btn-primary'

Devise edit form in modal, not populating fields unless it's rendered from the normal edit view

I'm trying to have Devise users be able to edit from a modal. The problem is, when the modal loads, it doesn't populate the forms with any information. But if I open the modal from /users/edit the form does populate. Anyone know how to go about this?
_edit.html.haml
.row
.small-2.large-4.columns
.small-4.large-4.columns
%h2
Edit #{resource_name.to_s.humanize}
= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { :multipart => true, method: :put }) do |f|
= devise_error_messages!
%div
= f.label :email
%br/
= f.text_field :email
%div
= f.label :company_name
%br/
= f.text_field :company_name
%div
= f.label :branding
%br/
= f.file_field :branding
%div= f.submit "Update"
%h3 Cancel my account
%p
Unhappy? #{button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete}
= link_to "Back", :back
.small-6.large-4.columns
It's rendered from my application/layout like this:
#editProfile.reveal-modal{"data-reveal" => ""}
= render "devise/registrations/edit"
%a.close-reveal-modal ×

Hidden values show up as blank

I have a form using rails and being submitted by angularjs. The problem I'm having is that when I try to access the form data from angular, the input that is not hidden will show up in the form newTask object, but the fields that are hidden won't. Is there something I am missing here?
= form_for [#patient, Task.new], html: {action: nil, name: 'newTaskForm', 'ng-submit' => 'taskCreate($event, newTask)'} do |f|
= f.hidden_field :taskable_id, :value => #patient.id,
'ng-model' => 'newTask.taskable_id'
= f.hidden_field :taskable_type, :value => #patient.class.to_s,
'ng-model' => 'newTask.taskable_type'
= f.hidden_field :creator_id, :value => current_user.id,
'ng-model' => 'newTask.creator_id'
= f.text_field :text, required: true, placeholder: 'Add a task...',
'ng-model' => 'newTask.text'
= f.submit 'Add task', 'ng-disabled' => 'newTaskForm.$invalid'

Strong_parameters in view with mixed models

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

Rails 3: Text_field_tag default value if params[:something].blank?

How would one do something like:
= f.input_field :age,
:collection => 18..60,
:id => 'age',
:selected => params[:age].blank? or "20"
Above doesen't work. I want to be able to set a default value if there is no param available for that attribute.
Any smart way to do this thx!
EDIT 1:
Full form:
= simple_form_for :people, :url => request.fullpath, :method => :get, :html => { :class => 'form-search' } do |f|
#container_search_small.form-search
= f.input_field :age,
:collection => 18..60,
:id => 'age',
:selected => params[:people][:age_from] || "20"
= f.submit "Go »"
You're using helpers that are taking their values from the object you're building the form on.
So in your controller, you should preset the values on the object.
def some_action
#people = People.new
#people.age = params[:age] || 20
end
Then in the form, remove the :selected option and it should be fine. Make sure you build the form for #people :
= simple_form_for #people, :url => request.fullpath, :method => :get, :html => { :class => 'form-search' } do |f|
#container_search_small.form-search
= f.input_field :age,
:collection => 18..60,
:id => 'age'
= f.submit "Go »"

Resources