Ability to store Name/Email in a session - ruby-on-rails-3

I have a form that I want a user to put in their name and email, which get stored in a session. They can then post text in a chat box.
In my view, to create the initial session:
<%= simple_form_for(#comments, :url => guest_login_order_path(#order)) do |f| %>
<input name="comment[new_user_comment_name]" />
<input name="comment[new_user_comment_email]" />
<%= f.button :submit, :value => 'Guest Signin', :class => '' %>
<% end %>
This goes to my controller:
def guest_login
#order = Order.where(:public_hash => params[:public_hash]).first
session[:new_user_account] = params[:new_user]
respond_to do |format|
if session[:new_user_account]
format.html { redirect_to #order, notice: 'Your account has been created.' }
format.json { render json: #order, status: :created, location: #order }
else
format.html { render action: "invoice" }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
The params get passed correctly but I'm not quite sure if the cookie is being created. Is there way to specify a name so I can see if it was created? Also in the view, would I have a conditional then to see if there is a session present?

Rails creates a session for you so you don't need to check if it's present. If you'd like to easily retrieve the user from the session you can create a helper method in your application controller:
class ApplicationController < ActionController::Base
def current_user
#current_user ||= session[:new_user_account]
end
helper_method :current_user
end
This method will be available to other controllers and views in your app.

Related

Unpermitted parameters although controller has them whitelisted when creating user

In migrating a rails application from 3.2 to 4.1, I am hitting some issues with user creation. As there is a need to distinguish the current_user form a local_user. The controller create action
def create
#local_user = User.new(user_params)
respond_to do |format|
if #local_user.save
if params[:user][:avatar].present?
format.html { render :crop }
else
format.html { redirect_to(admin_user_path(#local_user), :notice => 'User was successfully created.') }
end
else
format.html { render :action => "new" }
end
Generates a console error: Unpermitted parameters: name, surname, pen_name[...], yet the User controller defines them:
def user_params
params.require(:user).permit(:name, :surname, :pen_name, [...])
end
The form call is:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :method => :post, :validate => true) do |f| %>
This is necessarily a devise issue for a user can be created with the scaffolding for the User class with the exact same variables. There is some logic that devise is going through which does not pull all the controller logic ; what am I missing?
Needs a devise specific initializer, as per this demo, where all variables can be entered as an array.

undefined method 'random_generate' for #<Generator:0x4cce798>

i have a method which i wan to call from my controller. What i've read in stacks are most of them said to include the method in applicationController.Therefore i did. But still i got the following error. Can some1 help me ? New to rails
APPLICATION CONTROLLER :
helper_method :random_generate
def random_generate
length = #generator.primer_length
chars = 'ATGC'
seq = ''
length.times { seq << chars[rand(chars.size)] }
#generator.random_primer_generated= seq
end
CONTROLLER :
class GeneratorsController < ApplicationController
before_action :set_generator, only: [:show, :edit, :update, :destroy]
after_action :generate_option, only: [:create,:edit]
helper_method :random_primer_generated
# GET /generators
# GET /generators.json
def index
#generators = Generator.all
end
# GET /generators/1
# GET /generators/1.json
def show
end
# GET /generators/new
def new
#generator = Generator.new
end
# GET /generators/1/edit
def edit
end
# POST /generators
# POST /generators.json
def create
#generator = Generator.new(generator_params)
respond_to do |format|
if #generator.save
format.html { redirect_to #generator, notice: 'Generator was successfully created.' }
format.json { render action: 'show', status: :created, location: #generator }
else
format.html { render action: 'new' }
format.json { render json: #generator.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /generators/1
# PATCH/PUT /generators/1.json
def update
respond_to do |format|
if #generator.update(generator_params)
format.html { redirect_to #generator, notice: 'Generator was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #generator.errors, status: :unprocessable_entity }
end
end
end
# DELETE /generators/1
# DELETE /generators/1.json
def destroy
#generator.destroy
respond_to do |format|
format.html { redirect_to generators_url }
format.json { head :no_content }
end
end
def generate_option
if params[:choice] == 'Randomly'
#generator.random_generate
end
#generator.save!
end
private
# Use callbacks to share common setup or constraints between actions.
def set_generator
#generator = Generator.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def generator_params
params.require(:generator).permit(:primer_length, :choice, :random_primer_generated)
end
end
VIEW:
<%= form_for (#generator ) do |f| %>
<% if #generator.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#generator.errors.count, "error") %> prohibited this generator from being saved:</h2>
<ul>
<% #generator.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<label>Primer Length</label><br>
<%= f.number_field :primer_length %>
</div>
<label>Selection :</label><br>
<label>Randomly</label>
<%= radio_button_tag(:choice, 'Randomly')%>
<%= button_to('Generate',generate_option_generator_path(#generator))%>
<% end %>
If you want to call random_generate on the #generator, you have to implement that method in the Generator class. What you have done is implemented it in the ApplicationController, so you would have to call it without referencing to #generator.
Also, I think calling helper_method :random_generate is only required if you want to call random_generate as a controller method inside of a view.

Syntax error, but correct syntax? unexpected ':', expecting ')'

I have a nested form (see below)
Model Artist (artist is a user)
has_many :art_works
has_many :canvases
accepts_nested_attributes_for :art_works //artworks is what im currently working on
accepts_nested_attributes_for :canvases
Controller art_works
def new
#artist = Artist.find(params[:artist_id])
#artwork = #artist.art_works.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: #artwork }
end
end
def create
#artwork = ArtWork.new(params[:artwork])
respond_to do |format|
if #artwork.save
format.html { redirect_to #artwork, notice: 'artwork was successfully created.' }
format.json { render json: #artwork, status: :created, location: #artwork }
else
format.html { render action: "new" }
format.json { render json: #artwork.errors, status: :unprocessable_entity }
end
end
end
artworks views _form
<%= form_for(#artwork, :url => artist_art_works_path(current_artist) :multipart => true) do |f| %>
<p>
<%= f.file_field :art %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
I was pretty positive that this would work, but i'm assuming my :url is incorrect? I'm not really sure what else it would be though. below are my routes for artworks the reason why I am nesting these things is because an artist can upload art into an artwork model the idea is to have several pieces of art in one artwork (like an album has many images inside of it)
artist_art_works GET /artists/:artist_id/art_works(.:format) art_works#index
POST /artists/:artist_id/art_works(.:format) art_works#create
new_artist_art_work GET /artists/:artist_id/art_works/new(.:format) art_works#new
edit_artist_art_work GET /artists/:artist_id/art_works/:id/edit(.:format) art_works#edit
artist_art_work GET /artists/:artist_id/art_works/:id(.:format) art_works#show
PUT /artists/:artist_id/art_works/:id(.:format) art_works#update
DELETE /artists/:artist_id/art_works/:id(.:format) art_works#destroy
Thank you very much in advance for your help. (sorry for the noobness)
You're missing a comma. Yeah the error message isn't that helpful.
#artwork, :url => artist_art_works_path(current_artist) :multipart => true
vs
#artwork, :url => artist_art_works_path(current_artist), :multipart => true

Passing an object through a link_to command to be referenced in the model#new page

I'm creating a basic loyalty card application, with model Merchant and Loyaltycard. In the merchant#show view I have this line
<%=link_to 'New Loyalty card', new_loyaltycard_path(:merchant_id=>1) %>
I'm trying to pass the merchant ID to the loyaltycard#new view so it will be automatically selected as the merchant for that card. In loyaltycard#_form (which gets called by loyaltycard#new) I have the lines
<%if #merchant%>
<%= f.hidden_field :merchant_id, :value => #merchant.id %>
<%else%>
<div class="field">
<%= f.label :merchant_id %><br />
<%= f.text_field :merchant_id %>
</div>
<%end%>
But I keep getting and error that says can't call id for class Nil. Is there a better way of doing this?
Here is the controller code for loyaltycard
def new
#loyaltycard = Loyaltycard.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #loyaltycard }
end
end
# GET /loyaltycards/1/edit
def edit
#loyaltycard = Loyaltycard.find(params[:id])
end
# POST /loyaltycards
# POST /loyaltycards.json
def create
#loyaltycard = Loyaltycard.new(params[:loyaltycard])
respond_to do |format|
if #loyaltycard.save
format.html { redirect_to #loyaltycard, notice: 'Loyaltycard was successfully created.' }
format.json { render json: #loyaltycard, status: :created, location: #loyaltycard }
else
format.html { render action: "new" }
format.json { render json: #loyaltycard.errors, status: :unprocessable_entity }
end
end
end
The error is
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
What you need to do in your new action is using the params[:merchant_id] to look up and set #merchant
#merchant = Merchant.find(params[:merchant_id])
Then your code should work, without that, #merchant is nil, and you can't call the method :id on nil
You're not setting variable #merchant anywhere in your controller, but you use it the view.

Rendering a like/unlike button in rails with jQuery

I'm using jquery to render a like button on a story. I have two models story and like
Here's the likes controller code:
def create
#like = Like.new(params[:like])
#story = Story.find(params[:story])
#like.story = #story
if #like.save
respond_to do |format|
format.html
format.js
end
end
end
def destroy
#like = Like.find(params[:id])
##story = #like.story
#like.destroy
respond_to do |format|
format.html { redirect_to stories_url }
format.js
format.json { head :ok }
end
end
This is the button partial (stories/like_button):
<% unless user_likes_story?(#story, current_user) %>
<%= button_to 'like', "/likes?story=#{#story.id}", :id => 'like_button', :remote => true %>
<% else %>
<%= button_to 'liked', #liked, :class => 'like_button unlike', :id => 'unlike_button', :remote => true, method: :delete %>
<% end %>
The problem is that my create.js.erb, when rendering the 'unlike' button, doesn't properly load the #liked instance variable, because it's being set in the #show action of the stories controller before the user has liked the story, so I can't figure out how or where to set it so the JS will render the unlike properly. I'm probably making this harder than it has to be, but...
create.js.erb
$('.button_to').replaceWith('<%=j render 'stories/like_button' %>');
$('#story_likes_count').replaceWith('<%=j render 'stories/likes_count' %>');
Here's how I tried setting #liked, in stories#show
def show
#like = Like.new
#story = Story.find(params[:id])
if current_user
#liked = Like.find_by_user_id_and_story_id(current_user,#story)
end
respond_to do |format|
format.html # show.html.erb
format.json { render json: #story }
end
end
Any idea how to do this properly? Should I just move the partial out of the stories folder?
The solution was pretty simple. In the controller, doing this:
format.js {#liked = #like}
Passes the variable and makes it available to the partial when it's rendered.