i added function federatedid in my user model by passing session to hide password in the form.html.haml it is giving this error,how to resolve error - ruby-on-rails

I added this function in user.model
def is_federated?(session)
session[:federatedIdHeader].present?
end
My form.html.haml file is this, i am trying to edit the user if it federated by passing the session in 7th line , this is basically in another repo, i am using it as a gem
.user-edit-form
= simple_form_for(editable_user,html: { autocomplete: "off" }) do |f|
%section
%header
%h3
= precede "User Information" do
-if editable_user.is_federated?(session)
= link_to "Change Password",main_app.try(:new_password_path)
.inputs
= f.input :needs_setup,as: :hidden,input_html: { value: false }
= f.input :name,required: true
= f.input :email,disabled: true
= f.input :phone,label: "Phone Number"
= f.input :title
- if editable_user_is_authorized?(editable_user)
%section
%header
%h3
= precede "Company Information" do
%small (Shared by all users in your company)
.inputs
= f.fields_for :account do |a|
= a.input :name,required: true
= a.input :url,label: "Web Address",required: true
.actions
= f.button :submit, "Save Profile", class: "button little green"
i am getting this error while deploying
users/edit/_form.html.haml:7: syntax error, unexpected ')' ));}\n", 0, false);if
editable_user.is_federated?(session)
/edit/_form.html.haml:9: syntax error, unexpected ')', expecting keyword_end ));}\n", 0,
false);end;
How to make it work

%header
%h3
= precede "User Information" do
- if editable_user.is_federated?(session)
= link_to "Change Password",main_app.try(:new_password_path)
This should pretty much fix your issue..
The issue being : the lines after where you open the precede block, the block contents needs to be indented to be within the block.
I hope that makes sense.

Related

Proper syntax for controller action with slim

Rails 3.2
I have a view views/tickets/show.html.slim with a number of sections. I want to have different controllers for each section, and have actions like New Save Edit
So in my views/tickets/show.html.slim, I have:
- #customer_info = customer_info #ticket
h4.form-header Customer Information
.form-section.attachments
- if #customer_info.nil?
= render partial: 'tickets/sections/customer_info', locals: {ticket: #ticket }
In my view, I have:
= form_for CustomerInfo.new do |f|
- f.hidden_field :ticket_id, :value => ticket.id
.form-horizontal-column.customer-info
.form-group
= f.label :first
= f.text_field :first, maxlength: 50
.form-group
= f.label :last
= f.text_field :last, maxlength: 50
- logger.info("Marker 1")
.actions = link_to "Save", :controller => :customer_infos, :action => :create
- logger.info("Marker 2")
.clear
When I run the application in test mode, and I select a ticket, I get the following response:
Incomplete response received from application
In my test.log file, I have:
CustomerInfo Load (0.1ms)[0m SELECT customer_infos``.* FROMcustomer_infosWHEREcustomer_infos.ticket_id` = '1466026127' LIMIT 1
Marker 1
Rendered tickets/sections/_customer_info.html.slim (11.6ms)
Rendered admin/tickets/show.html.slim within layouts/application (563.0ms)
There is no Marker 2
If I replace:
.actions = link_to "Save", :controller => :customer_infos, :action => :create
With:
.actions = f.submit 'Save'
Then the form renders fine.
Any idea why this is not working?
Attempted fix:
In my tickets/section/_customer_info.html.slim, I did:
.actions = link_to "Save", create_customer_info_path, method: :post
In my routes.rb, I have:
post '/customer_infos/create' => 'customer_infos#create', as: 'create_customer_info'
I am now getting the following error message:
undefined method `customer_infos_path' for #<#<Class:0x00000008bb54d8>:0x00000009df3c30>
Where is customer_infos_path coming from?
If I do rake routes, I get:
create_customer_info POST /customer_infos/create(.:format) customer_infos#create
According to the Rails docs, using the controller option is frowned upon. You should set up your route with a name, like this:
post '/customers/create' => 'customer_infos#create', as: 'create_customer'
Then your view should look something like this:
.actions
= link_to "Save", create_customer_path, method: :post
Hope this helps!

Title case for simple_form labels

The following code gives a label of "Project address":
%h2 New Project
= simple_form_for(#project, html: {class: "form-horizontal"}, wrapper: :horizontal_form) do |f|
= f.error_notification
= f.input :project_address, required: true
= f.button :submit, "Create"
Is there any way of making the label title case ("Project Address") apart from using f.label?
If you wanted to title case all labels, you can configure simple form in the initializer:
# config/initializers/simple_form.rb
# How the label text should be generated altogether with the required text.
config.label_text = lambda { |label, required, explicit_label| "#{required} #{explicit_label ? label : label.to_s.titleize}" }
This will make all your labels title case unless you specify the label explicitly with the :label option.

Values of a book saved from amazon have {:value=>""} around it

I'm making an application where the user can search Amazon (with Vacuum) through my application for books, then be able to record the data of the book to their library.
When you search for a book, it goes through every result and puts each in a thumbnail. In every thumbnail there is a button that opens a modal with a form with hidden tags. When the user clicks the submit button, the book's title is saved into a new book. The only problem is that the title is saved like {:value=>"the title of the book that was saved"}
Here is the part of new.html.erb which has the search box:
<%= form_tag({controller: "books", action: "new"}, method: "get", id: "search-form") do %>
<%= text_field_tag :keywords, params[:keywords], placeholder: "Search for a book", class: "form-control" %>
<% end %>
Here is the part of new.html.erb which has the hidden form:
<% #results.each do |result| %>
…
<%= form_for #book do |f|%>
<%= hidden_field_tag :title, class: 'form-control', value: result.name %>
<%= f.submit "Add book", class: "btn btn-default green-hover" %>
<% end %>
…
<% end %>
Here are the new and create actions in my controller:
def new
#book = current_user.books.build if logged_in?
# Search actions
if params[:keywords]
request = Vacuum.new
request.configure(
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
associate_tag: 'my associate tag is here'
)
keywords = params[:keywords]
params = {
'SearchIndex' => 'Books',
'Keywords'=> keywords,
'ResponseGroup' => "ItemAttributes,Images"
}
raw_results = request.item_search(query: params)
hashed_results = raw_results.to_h
#results = []
hashed_results['ItemSearchResponse']['Items']['Item'].each do |item|
result = OpenStruct.new
result.title = item['ItemAttributes']['Title']
result.url = item['DetailPageURL']
result.image_url = item['MediumImage']['URL']
result.author = item['ItemAttributes']['Author']
result.pages = item['ItemAttributes']['NumberOfPages']
#results << result
end
end
end
def create
#book = #list.books.build(book_params)
if #book.save
flash[:success] = #book.title + "was added to your log."
redirect_to list_path(#book.list_id)
else
render 'books/new'
end
end
I tried to use gsub within book.rb to fix it, but that only changed the text within the flash message and it still saved as {:value=>"the title of the book that was saved"}.
after_create :init
private
def init
puts "Init was called!"
self.title.gsub!('{:value=>"', " ")
self.title.gsub!('"}', " ")
end
How can I change it so that it doesn't save the title with the {:value=>} around it?
I don't think the hidden field tag is right.
<%= hidden_field_tag :title, class: 'form-control', value: result.name %>
Try
<%= hidden_field_tag :title, result.name %>
Your title is being saved as a hash not a string. Use hash accessing methods:
t = title[:value]
puts t #=> "the tile of the book that was saved"

Validate presence of attributes on edit page

This is a complicated one so I will try to explain as best as I can. I have an attachment model and I am using carrierwave_direct as the gem. Carrierwave_direct requires uploading on one page and then the attributes on another page. The other page it needs to redirect you to is the edit page, however the attributes on the edit page I cannot validate through the model, unless I want the upload to fail(because it won't create an id with validations meant for another page.). I think what I am saying is how would I validate that model attributes have a presence only on the edit page and if the user exits the page without filling in the form, it deletes the model instance. I want it to do this because for my model to work they need it to create the model instance on upload. See my code in the controller below
def edit
#attachment = Attachment.find(params[:id])
end
def report_uploader
#uploader = Attachment.new.delivery
#uploader.success_action_redirect = uploaded_attachments_url
#uploader
end
helper_method :report_uploader
def after_upload
#attachment = Attachment.new(recruiter_id: current_user.id, uploader_name: current_user.first_name + " " + current_user.last_name)
#recruiter = current_user
#attachment.url = #attachment.delivery.direct_fog_url + params[:key]
#attachment.key = params[:key]
#attachment.filename = File.basename(#attachment.key)
#attachment.save
redirect_to edit_attachment_path(id: #attachment.id, :anchor => 'reports')
end
attachment modal
.head
%h2 Add Attachment
.close
%a{:href=>"#",:rel=>"modal:close"} ❎
.content
= direct_upload_form_for report_uploader , :html => { :class => "standard" } do |f|
.fieldgroup.upload
.field
.upload
= link_to "#", class: "btn primary" do
= image_tag 'loader-white.gif', alt: 'Loading', class: 'loading'
Upload Attachment
= f.file_field :delivery, class: 'file_upload', id: 'upload_attachment'
edit page(User is redirected here)
.content
%h2 Add Some Additional Stuff to your Attachment!
= form_for #attachment, html: {class: "standard manage-job"} do |f|
.fieldgroup.upload
.field
Your Attachment Title
%br
%a{href: attachment_path(id: #attachment.id), target: '_blank' }
= #attachment.filename
.field
%select{name: "attachment[attachment_type]"}
%option{value: ""} Select Attachment Type
%option{value: "Type A"} Type A
%option{value: "Type B"} Type B
%option{value: "Type C"} Type C
.field
Description of your Attachment
= f.text_area :description
.actions
= f.submit "Submit Report", class: "btn primary"
= link_to 'Cancel', attachments_path

Rails: Generic form actions, cancel link losing `:back` on validation failure

I am trying to create a generic set of Submit, Cancel, and Destroy actions for forms. At this point, it appears that everything is working, except that I lose :back functionality then a form reloads due to validation errors. Is there a way to catch the fact that validation has failed, and in that case, keep the request.env['HTTP_REFERER'] or :back value the same without having to edit every controller?
= simple_form_for #announcement do |f|
= f.error_notification
= f.input :message
= f.input :starts_at
= f.input :ends_at
#submit
= f.button :submit
= "or "
= link_to("cancel", url_for(:back))
.right
- if !f.object.new_record?
- resource = (f.object.class.name).downcase
= link_to "destroy", url_for(:action => 'destroy'), :confirm => "Are you sure that you want to delete this #{resource}?", :method => :delete
.clear
.non_input
#post_back_msg
#indicator.inline
= image_tag "indicator.gif"
.inline
= "Please wait..."
.non_input
How about something like a helper method with something like this?
#previous = #previous.blank? ? request.env['HTTP_REFERRER'] : #previous
Then the Cancel button is just:
link_to('Cancel', #previous)
Does this work?
link_to "Cancel", #model.errors.any? ? request.env['HTTP_REFERRER'] : :back

Resources