I need to create new Event from User's guest page and the Event.visitor_id should be User.id
Event.rb
def create
#event = current_user.owner_events.new(event_params)
end
protected
def event_params
params.require(:event).permit(:visitor_id)
end
I need to correct the view below which is not working:
<%= link_to "Create event with this user", events_path(visitor_id: #user.id), method: :post %>
I get: ActionController::ParameterMissing in EventsController#create
Try that (you forgot to add event key):
<%= link_to "Create event with this user", events_path(event: { visitor_id: #user.id }), method: :post %>
For more details read 'strong parameters' gem documentation.
I just had to add .to_i to #user.id
<%= link_to "Create event", events_path(:event =>{:visitor_id => #user.id.to_i} ), :method => :post %>
Related
I have two links ( could be buttons if needed ) that say accept and decline and I need to send true or false parameters to my controller action by clicking one of those links. I don't want my parameters to be visible in the url so I need to use put method. I have tried with the link_to with defined method:
<%= link_to 'accept', { action: 'accept_offer', accept: true }, method: :put %>
<%= link_to 'decline', { action: 'accept_offer', accept: false }, method: :put %>
but my params are still visible.
I've tried using button_to but then my parameters are not passed.
What is the best way to determine which option has been chosen (accept or decline) without showing parameters in url?
my route has be defined like this:
put 'offers', to: 'offers#accept_offer'
i'll recommend to make a form instead of link_to and pass params in name. Use a form and POST the information.This might require additional code in source pages, but should not require logic changes in the target pages.
<% form_for #offer, :url => {:action => 'accept_offer'} do |f|%>
<%= submit_tag "", :value => "Accept", :name => "accept" %>
<%= submit_tag "", :value => "Decline", :name => "decline" %>
<% end %>
in your action you'll get params[:accept] or params[:decline] based on the link you clicked.
Edited to include commas and spaces with keyword arguments on submit tag.]
Start with just the conventional routes:
resources :offers
Then lets use button_to to create a discrete form:
<%= button_to 'accept', #offer, method: :patch, params: { "offer[accept]" => true } %>
<%= button_to 'decline', #offer, method: :patch, params: { "offer[accept]" => false } %>
The params option creates hidden inputs inside the form.
Then make sure you whitelist the correct attribute:
class OffersController < ApplicationController
def update
#offer = Offer.find(params[:id])
if #offer.update(offer_params)
redirect_to #offer, success: 'Offer updated'
else
render :new
end
end
def offer_params
params.require(:offer).permit(:accept, :foo, :bar)
end
end
If you need to have separate logic from the regular update then create two additional verbs:
resources :offers do
member do
patch :accept
patch :decline
end
end
<%= button_to 'accept', accept_offer_path(#offer), method: :patch %>
<%= button_to 'decline', decline_offer_path(#offer), method: :patch %>
This keeps your API restful and decriptive.
So I am trying to implement the password_reset functionality into my site using bcrypt. An issue I am having is the POST is going to my new action rather to my create action.
My View
<%= form_for password_resets_path, method: 'post' do %>
<div>
<h3>Please enter your email address</h3>
<%= text_field_tag :email, params[:email] %>
</div>
<div>
<%= submit_tag "Reset Password" %>
</div>
My Controller
class PasswordResetsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:email])
user.send_password_reset if user
redirect_to root_url, :notice => 'Email sent with password reset instructions.'
end
end
My Routes
resources :password_resets
And I am getting this error
ActionController::RoutingError (No route matches [POST] "/password_resets/new"):
I looked at different solutions already, and since I do not have a model the #object, would not work for me. Since I am simply just trying to call to an action.
I feel like I am missing something so very simple but for the life of me I have been unable to figure it out. Many thanks in advance to whomever is the one to help me.
Problem: <%= form_for password_resets_path, method: 'post' do %>
form_for needs an object. If you don't want an object, just use the form_tag helper:
<%= form_tag password_resets_path do %>
<%= text_field_tag :email, params[:email], placeholder: "Please enter your email address" %>
<%= submit_tag "Reset Password" %>
<% end %>
This should work for you.
I have two models shop and user. Shop has_many Users and User has_one Shop. I'm trying to create a button on shop/show that allows a user to choose its shop. The button should send the parameter shop_id to Users_Controller#update_shop which then changes User.shop_id to the id of the shop on the page.
The current method in Users_Controller#update_shop is the following:
def update_shop
#user = User.find(params[:id])
#user.update_attributes(shop_id: params[:shop][:id])
flash[:success] = "Added Shop!"
end
And the button on show/shop is this:
<%= form_for User.update_shop, remote: true do |f| %>
<%= f.hidden_field :shop_id, value: #shop.id %>
<%= f.submit 'Add As Your Shop', :class => 'button blue-button' %>
<% end %>
I'm getting the error NoMethodError in ShopsController#show undefined methodupdate_shop' for #`. The method is defined in the Users controller though.
I understand there is probably a more efficient way to update User.shop_id through the association, so tips on doing that or getting this to work are greatly appreciated.
I think it's better to have a link with POST method in place of form:
<%= link_to 'Add As Your Shop', update_shop_url(id: #user.id, shop_id: #shop.id, class: 'button blue-button', data: { method: 'post' } %>
With this setup you should change controller method as well:
def update_shop
...
#user.update_attributes(shop_id: params[:shop_id])
...
end
Combo: Rails4, Mongoid
I have an Admin console dashboard: /admin/moderator-slug/dashboard
that lists all users with a button to go manage that user:
<%= link_to "Manage", { controller: 'admin', action: 'manage', user: user} %>
As you can see, I pass that user in the params.
In my Admin controller, my manage action is as follows:
def manage
#user = User.find(params[:user])
if params[:follow_up].present?
#user.follow_up = Date.strptime(params[:follow_up], "%m/%d/%Y")
end
end
It successfully leads to the management page: /admin/moderator-slug/manage?user=managed-user-slug and finds the #user through the params I pass with the button.
In my User model I created a Time field "follow_up".
I want to update that field with a certain time selected with datepicker, so I created the following form_tag:
<%= form_tag manage_path, method: "get", remote: true do %>
Follow up: <%= text_field_tag "follow_up", nil, autocomplete: "off" %>
<%= submit_tag "Create" %>
<% end %>
When I press the submit button I get Mongoid::Errors::InvalidFind
When I pass on a hidden field <%= hidden_field_tag :user , #user %> , I get 404. And besides, I have a feeling my set up is not the healthiest, so passing on that hidden field is really a workaround that could be avoided.
my routes:
authenticate :user, -> (u) { u.is_moderator? } do
match 'admin/:slug/dashboard', to: 'admin/admin#dashboard', via: 'get', as: :dashboard
match 'admin/:slug/manage', to: 'admin/admin#manage', via: 'get', as: :manage
end
Any suggestions?
Looks like you should add user slug to your form URL:
<%= form_tag manage_path(user: #user), method: "get", remote: true do %>
I have a form on the chords/:id/show page (shown below). I am able to input a :note_id into the form, which then creates a ChordNote using the :chord_id from :params and the :note_id from the form. This works well. However, when I try to delete a ChordNote using the same form, I get an error that says:
NoMethodError in ChordNotesController#destroy, undefined method '[]' for nil:nilClass
This is the controller for 'ChordNote', which joins chords and notes in a many-to-many relationship.
def create
#chord = Chord.find(params[:chordnote][:chord_id])
#note = Note.find(params[:chordnote][:note_id])
if #chord.hasnote?(#note)
# Add error message here, have it not redirect
redirect_to #chord
else
#chord.addnote!(#note)
redirect_to #chord
end
end
def destroy
#chord = Chord.find(params[:chordnote][:chord_id])
#note = Note.find(params[:chordnote][:note_id])
Chordnote.find_by(note_id: #note.id, chord_id: #chord.id).destroy
redirect_to chord_path(#chord)
end
This is the form (that appears on chords/:id/show):
<%= form_for(#chord.chordnotes.build(chord_id: #chord.id)) do |f| %>
<div><%= f.hidden_field :chord_id, value: #chord.id %></div>
<div class="field">
<%= f.text_field :note_id, placeholder: "Enter the note's id" %>
</div>
<%= f.submit "Add Note", class: "btn btn-large" %>
<%= link_to "Remove Note", Chordnote.find_by(note_id: 1), method: :delete, title: "test title", class: "btn btn-large" %>
<% end %>
Any thoughts on why destroy is not working? Thanks!
undefined method `[]' for nil:NilClass
def destroy
####The error is on the following line####
#chord = Chord.find(params[:chordnote][:chord_id])
#note = Note.find(params[:chordnote][:note_id])
Chordnote.find_by(note_id: #note.id, chord_id: #chord.id).destroy
redirect_to chord_path(#chord)
Rails.root: /Users/mydocs/myprojects/rails_projects/what_key_v002
Application Trace | Framework Trace | Full Trace
app/controllers/chordnotes_controller.rb:23:in `destroy'
Request
Posted parameters:
{"_method"=>"delete",
"id"=>"10"}
In your destroy action, you're trying to make a lookup off params[:chordnote][:note_id], but the only available parameters are {"_method"=>"delete", "id"=>"10"}. You need to add both note_id and chord_id as arguments in your link_to helper:
<%= link_to "Remove Note", chordnote_path(:chord_id => #chord.id, :note_id => 1), :method => :delete %>
# => Remove Note
Then, in your destroy action, make your lookups off params[:chord_id] and params[:note_id]:
def destroy
Chordnote.find_by(note_id: params[:note_id], chord_id: params[:chord_id]).destroy
redirect_to chord_path(params[:chord_id])
end
Chordnote.find_by(note_id: #note.id, chord_id: #chord.id)
cant find a record, so its nil and nil doesnt have a method called destroy.
Are you sure you are passing the correct params to the action?
Update:
params[:chordnote] #seems to be nil, so
params[:chordnote][:note_id] => exception
Check the params which are posted to the action. You can see it in your console logs.
Update:
Your link should maybe be something like this:
<%= link_to "Remove Note", chord_notes_path(note_id: 1, chord_id: #chord.id), method: :delete, title: "test title", class: "btn btn-large" %>
And in your delete action
#chord = Chord.find(params[:chord_id])
#note = Note.find(params[:note_id])