Use Variable in Parameter Ruby on Rails

Ruby/Rails. Passing variable into parameters

Right here in your original code:

def posts_for_all_pages(category)
@posts = Posts.where(category: true)
end

The category in Posts.where(category: true) will not a variable, it will be a hard coded symbol :category, so it won't work. Instead, write this:

def posts_for_all_pages(category)
@posts = Posts.where(category => true)
end

a small change, but definitely has a different meaning.

Then when you call the method, you pass a symbol to it:

posts_for_all_pages(:photos)

Ruby on Rails: params[] Variable in Controller

You're referencing params[:name], but the parameters are all bundled up together under params[:s3_destination], so you need to reference params[:s3_destination][:name], etc.

The nice thing about this is you don't need to call out each attribute you want to update. In simple cases, you can just

@s3_destination.update_attributes(params[:s3_destination])

For safety (eg, so I can't edit the form to submit attributes you don't want me editing), you should use strong parameters:

s3_params = params.require(:s3_destination)
.permit(:name, :bucket, :region, :credentials_id, :credentials_secret)
@s3_destination.update_attributes(s3_params)

And then for your credentials_secret logic, you can just delete the blank value from this hash:

s3_params = params.require(:s3_destination)
.permit(:name, :bucket, :region, :credentials_id, :credentials_secret)
s3_params.delete(:credentials_secret) if s3_params[:credentials_secret].blank?
@s3_destination.update_attributes(s3_params)

blank? will catch both nil and "" the way you're currently doing with .to_s.empty?.

Ruby on Rails - Adding variable to params[]

params looks like a hash, but it really isn't. So if you need to "augment" params as you deal with the incoming data in your controller, invent a new data structure that includes either params or its members.

Added:

Maybe you're looking for

@codes=Code.get_codes
@all = []
for c in @codes
@all << params["group_#{c.name}"]
end

Why does Rails use the params variable rather than passing arguments through the function?

The point is, most of the actions in a controller handles the view REST-fully. The params comes from the user's browser when they interact with the page or send a new variable request to the page.

These requests are variable, and Rails makes it uniform by maintaining the parameters in params hash. If the following GET requests arrive:

http://localhost:3000/products?color=red&quality=best

the params hash will automatically be populated as {'color' => 'red', 'quality' => 'best'}. Rails doesn't expect your action to manually handle the parameters.

Similarly, consider you are getting a POST request from a page where a user filled a form. In that scenario, the params obtain the parameters which are composed with form helpers inside views.

Though in hyptothetical case you are dealing with general methods instead of actions, such as below, you will have to do it by passing arguments.

def show
if params['color'] == 'red'
@product = obtain_product('red')
else
#,..
end
end

def obtain_product(color)
Product.where('color = ?', color).first
end

Hope it is clear. :)

How are parameters sent when I use instance variables in form_for in rails

The form_for helper creates a form from a model object. If you've set up your Rails app in the conventional way, you will have (it appears) a file called app/models/missed_call_flow.rb that begins by defining class MissedCallFlow.

This file defines the model. It is the source of the params[:missed_call_flow] name, because it is the convention in Rails to name this data structure for the model.

As a generalization in programming, the variable name should not impact the functioning of the program. The fact that you have named your variable @flow is ignored by the inner workings of your program. It is a convenience to the programmer to be able to name your variable whatever you want.

I wonder if you are not seeing the model name you expect because missed_call_flow represents a join table between two other tables with models named missed_call and flow. You might be assigning an instance of this MissedCallFlow class to the variable @flow by mistake.

Adding a parameter to an instance variable in Rails

Based on the presence of a user_id in your Post model, you probably already have an association set up that can retrieve the username. It will probably save a lot of trouble to simply use the existing association:

@post = Post.find(params[:id])
username = @post.user.username

If you're likely to be querying more than one post at a time (e.g., on an index page, calling .includes to tell Rails to eager-load an association will help you avoid the N+1 problem:

@posts = Post.includes(:user).all

Finally, to include the associated record in your JSON output, pass the :include parameter as you serialize:

# in controller
render :json => @post.to_json(:include => :user)

This question includes a much more comprehensive discussion of serialization options. Well worth a read.

Ruby on Rails - Passing parameters to set in url

# everything in parenthesis after the path 
# helper is added as a hash to the URL
# parameters
calculator_path(number1: 80085, number2: 58008)

...will translate to

# parameters added to a URL is noted
# by the '?' followed by each parameter
# assigned and it's value
/calculator?number1=80085&number2=58008

Then you can pull the params down in your controller

first_number  = params[:number1]
second_number = params[:number2]

If you want to add a dynamic value, use a variable that gets assignment from somewhere else in your code....

calculator_path(x: @rdm_num_1, y: @rnd_num_2)

Passing parameters as path variables in ruby on rails

The cleanest way is to add a new find method in your model (or simply use the find_by_fieldname Rails gives you in your control). Then you'll have your controller use that method instead of the regular find(params[:id]) to pull your model record.

Check out Ryan B's screencast on this here. It's pretty easy, and he's a good teacher, so you shouldn't have any problems.



Related Topics



Leave a reply



Submit