Hello I am trying to change a string value from "no" to "yes" in my Rails Postgresql db based on if a user clicks a yes or no button using the link_to method in my view. I have designated the default string value to "no" so I would like the value to change to yes if a user clicks the yes button . I would like assign the 'link_to method' to the yesmarried action in my controller.I =m not sure how to locate the the newcase in the yesmarried action and I keep getting the error Couldn't find Newcase without an ID
here is my controller
class NewcaseController < ApplicationController
def new
end
def create
#newcase = NewCase.new(newcase_params)
end
def yesmarried
#newcase=Newcase.find(params[:id])
self.married = "yes"
end
private
def newcase_params
params.require(:newcase).permit(:state,:first_name,:last_name, :dob, :email, :telephone_number, :addr,:respondent_fname, :respondent_lname,:respondent_addr, :marriage_date, :state_of_marriage, :date_of_seperation, :number_of_children, :children_addr, :occupation , :work_addr, :net_monthly, :married)
end
end
here is my view:
<%=link_to 'yes',new_path(#newcase), class:"btn btn-lg rounded btn-warning", action: "yesmarried",method: "put", "data-toggle" => "modal", 'data-target' => '#married-question', "data-bs-dismiss"=>"modal" %>
routes file:
get '/new', to:'newcase#new'
post '/new', to:'newcase#create'
patch '/new', to:'newcase#yesmarried'
put '/new', to:'newcase#yesmarried'
Your code is a little unrestful and non-idiomatic but your yesmarried method should read:
def yes_married
#newcase = Newcase.find(params[:id])
#newcase.update_attribute(:married, 'yes')
end
Add the route:
get '/newcase_yes_married`, to: 'newcase#yes_married'
You'd need to change new_path to newcase_yes_married_path (or whatever path rails routes returns in the terminal).
However a better solution would be to piggyback an update method to keep it RESTful.
def update
#newcase = Newcase.find(params[:id])
#newcase.update_attributes(newcase_params)
end
<%= link_to "Yes", [#newcase, { newcase: { married: 'yes' }}], method: 'patch' %>
Note: code not tested.
Related
I have a problem with Rails 4 and deleting multiple items in a database.
I am working on creating an email inbox and I would like to move an item into the "trash folder" and after that I want to redirect the user to the trash page, where he can delete the item from the database.
My code:
routes.rb
namespace :admin do
resources :inboxes do
collection do
match 'destroy_multiple' => 'inboxes#destroy_multiple', via: ['post','delete']
get 'sent'
get 'trash'
end
end
end
controller :
def destroy_multiple
if params[:action] == 'trash'
#del = Inbox.where(:id => params[:delete]).destroy_all
else
#del = Inbox.where(:id => params[:delete]).update_all(:folder =>'trash')
end
redirect_to admin_inboxes_path
end
And finally, my form (slim) :
= form_tag destroy_multiple_admin_inboxes_path, method: :delete, class: "multiple_delete" do
input type="hidden" name="action" value = controller.action_name
- #inboxes.each do |msg|
- #username = msg.email.gsub(/([^.]+)#.+/, '\1').gsub(/[^0-9A-Za-z]/, ' ').split.map(&:capitalize).join(' ')
tr class=(msg.read == 1 ? nil : 'unread')
td.inbox-small-cells
label.checkbox-custom.check-success
= check_box_tag "delete[]", msg.id, false, class: "for_del", id: "delete_#{msg.id}"
label for="delete_#{msg.id}"
td.inbox-small-cells
i.fa.fa-star.inbox-started
td
a.avatar href="/fr/admin/inboxes/#{msg.id}"
span.bg-primary =#username[0]
td.view-message.dont-show = #username
td.view-message = msg.subject
td.view-message.inbox-small-cells
td.view-message.text-right = msg.created_at.strftime("%d/%m")
I can with this code successfully move mail to the "trash", but I cannot delete them from the database.
Thank you!
You need to add the authenticity_token to your form. Try:
form_tag destroy_multiple_admin_inboxes_path, method: :delete, class: "multiple_delete", authenticity_token: true do
If you're working with a specific active record object, use form_for instead of form_tag, and the csrf field will automatically be built into the form by rails. However, this is a good time to be using form_tag.
The alternative solution is to change the config config.action_view.embed_authenticity_token_in_remote_forms to false, but this will make your site less secure, and it's better to just add authenticity_token: true to your form_tag.
Here is the documentation on form_tag
In routes:
resources :users do
resources :service_exps
end
user model:
has_many :service_exps
service_exps model:
belongs_to :user
In service_exps controller new action:
def new
user = User.find(params[:user_id])
#service_exp = user.service_exps.build
render :layout => false
end
In service_exps form:
= form_for ([#service_exp.user, #service_exp]), :remote => true do |s|
.modal-body
.row
.span
= s.label :org_name
= s.text_field :org_name, :class => "span3"
.row
.span
= s.label :position
= s.text_field :position, :class => "span3"
.actions
= s.submit 'Save',:class => "btn btn-info"
It give error
undefined method `user' for nil:NilClass
Please give any suggestion to solve this. Thanks!
These type of errors are pretty easy to debug. The error msg says everything. You are calling a method (in this case 'user') on a nil object.
Follow these steps:
Find the file and line no from the error msg.
Find the object which is nil.
Confirm that the object is initialized properly
Firstly:#service_exp = user.service_exps.build will assign an array of objects to #service_exp - due to a user who has_many :service_exps.
In your view, you doing #service_exp.user - calling user on an array of objects which wont work. Something like #service_exp.first.user would though, when calling user on a specific object in the array. Or remove the .user from form_for
In Controller
class FeedEntriesController < ApplicationController
def index
#search = FeedEntry.search(params[:is_star])
#feed_entries = #search.page(params[:page])
#app_keys = AppKey.all
end
end
In View - feed_entries/index.html.erb
<%= link_to "Stared", {:controller => "feed_entries", :action => "index", :is_star => false } %>
feed_entries table contain is_star:boolean attribute. So, I just want to pass the parameter is_star == true into the params[:is_star].
But the above code is not working. Please some one help me.
If, I click the Stared link. I am getting error like below in the browser:
undefined method `stringify_keys!' for "false":String
It looks like your FeedEntry#search method expects an argument that responds to #stringify_keys! methods, so I assume you should pass a Hash :
#search = FeedEntry.search(is_star: params[:is_star])
BTW link_to with 'controller' and 'action' options is old and verbose syntax, you should use a resourceful style:
link_to 'Stared', feed_entries_path
read more here: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
So, I'm using activescaffold and have the following code:
config.actions = [:create, :delete, :list]
config.delete.link.confirm "Are you sure you want to delete this tag?"
which according to what I've googled should make the delete link confirmation box display that custom text... but it doesn't. It still uses some default question text. How can I customize the confirmation text?
It should work!
I have started a git rep to share code with fixes related to activescaffold.
You can see my controller here:
https://github.com/whizcreed/activescaffold-answers-to-stackoverflow/blob/master/app/controllers/notes_controller.rb
And here is the code that works correctly:
class NotesController < ApplicationController
active_scaffold :notes do |config|
config.columns = [:title, :content]
config.delete.link.confirm = "Shows that you can change the confirm text for delete!"
end
end
I am using rails 2.3.10
and activescaffold branch: https://github.com/activescaffold/active_scaffold/tree/rails-2.3
I hope this helps.
you are missing the =
it should be:
config.delete.link.confirm = "Are you sure you want to delete this tag?"
Why not approach this problem using the view layer?
<%= button_to "Delete Foo", { :action => "delete", :id => #foo.id },
:confirm => "For Real?!", :method => :delete %>
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html
I have this ruby function in a controller
def updateSession value
case value
when 1,2
session[:value]=1
when 3,4
session[:value]=2
end
end
And I have different links that redirect to different pages.
I would like to change the session value when clicking on those links by calling the updateSession function
Is it possible and if so, how?
You could add a route for:
map.update_session 'update_session', :controller => "session", :action => "update"
Then I assume you're trying to get "value" from user parameters, so you could use the following to generate your links:
<%= link_to "Update Session - 1", update_session_path(:value => 1) %>
Then add the following to your controller to act based on the params provided:
def update_session
value = params[:value].to_i
case value
when 1,2
session[:value] = 1
when 3,4
session[:value] = 2
end
end